• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Vb.net variable name

I have 3 variables and i need make a loop like ex.:

var1, var2,var3

for i=1 to 3
2 Solutions
Éric MoreauSenior .Net ConsultantCommented:
that can't be done this way. You might use an array: http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx
Fernando SotoRetiredCommented:
Hi rflorencio;

As emoreau has pointed out what you want to do can NOT be done with modifying a variable name by concatinating another string to it. You can accomplish this by using an array as shown below.

' Create an array of type integer. Please note the (2) defines the array with
' three elements. Arrays start the index at zero so the elements are 
' 0, 1 and 2. The value in (2) tells us that the last index in the array that
' can be access is index 2.
Dim vars(2) As Integer

' Setting up the loop to access all elements of the array. Note that it starts
' at 0 and NOT 1 as in your code and goes to the last index of 2
For i As Integer = 0 To 2
    vars(i) = 1

Open in new window

Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use Reflection or CallByName() (the latter only under certain circumstances).

But without further information the other approaches given already are probably a better design...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now