1

is there a way i can do like this in vb.net

dim idx = -1
dim a = array(idx = idx + 1)
dim b = array(idx = idx + 1)
dim c = array(idx = idx + 1)
dim d = array(idx = idx + 1)

what i want is that idx keeps incrementing after each line, without incrementing it on a seperate line.

Thank you

5
  • You can use ++idx to add 1 to it before, e.g. dim a = array(++idx) Commented May 18, 2012 at 16:22
  • See this Article Commented May 18, 2012 at 16:29
  • 3
    I don't think VB.net supports ++ Commented May 18, 2012 at 16:41
  • +=1 would be the VB equivalent to ++ Commented May 18, 2012 at 17:09
  • dim a = array(idx += 1) gives syntax error Commented May 18, 2012 at 18:04

1 Answer 1

3

I don't think VB.Net has anything like that, but you can make an extension to get close to it:

Imports System.Runtime.CompilerServices

Public Module Module1

  <Extension()> _
  Public Function UpIndex(ByRef value As Integer) As Integer
    value += 1
    return value
  End Function

End Module

Note the use of ByRef in the arguments.

Then your call would look like this:

Dim a = array(idx.UpIndex)
Dim b = array(idx.UpIndex)
Sign up to request clarification or add additional context in comments.

1 Comment

great idea. clean code :) except it should be return value (byref)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.