Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Can someone help me solve this problem, please.

 

What I want to be able to do create variable names dynamically / at runtime

 

e.g. say I have an array 'myArray' that has 5 values in it, what I want to be able to do is loop through that array and create a variable called 'simon' & the index that I am at in the array (something like code below)

 

dim [color=Red]i[/color] as integer
for [color=Red]i[/color] = 0 to myArray.count - 1
 dim simon[color=Red]i[/color] as string
next i

 

So the output would be five new string variables called:

 

simon1

simon2

simon3

simon4

simon5

 

Is this possible to do???

 

Thanks

 

Simon

  • Leaders
Posted

You can't dynamically name variables like that. It wouldn't really work. The names often dissapear during compilation anyways. Disassemble some IL and you will find variables named Int1, Int2, Int3 instead of their original names.

 

So, if you want to access data by name, you might want to consider using a hash table. If you want to access it by index, simply use an array.

 

The easiest way to do what it looks like you want to do would be the array.

Dim simon As String()
'...
simon = New String (myArray.Count - 1) {} 'Create new array
'We now have variables: simon(0), simon(1), simon(2), simon(3)... simon(myArray.Count - 1)

Using either a hash table or array you should be able to dynamically allocate and access data the way you need to, but this is pretty basic stuff, so you might want to look into MSDN or search google for some tutorials.

[sIGPIC]e[/sIGPIC]
Posted
The closest thing you could do would be to use a HashTable or SortedList. Then you can create keys "simon1", "simon2", etc. and assign the corresponding values. You would then access the value corresponding to the key "simon1" using myHashTable("simon1").
Posted

Naming Variable name dynamically

 

You have to look in the reflection namespace and the type classes.

With there you can execute lines of code dynamically not just naming variables

Good luck

Life is the biggest school
Posted
I'll support Marble_Eater' date=' use arrays, you can't do a dynamic variable naming[/quote']

Even if you can, most of the time "Dynamic variable naming" can be avoided, though this can be easily done with script. IMO, a programming language is very different from a script.

 

 

$a = "Apple";
$b = "a";
echo $$b; // print 'Apple'

There is no spoon. <<The Matrix>>
Posted
The closest thing you could do would be to use a HashTable or SortedList. Then you can create keys "simon1"' date=' "simon2", etc. and assign the corresponding values. You would then access the value corresponding to the key "simon1" using myHashTable("simon1").[/quote']Sorry, marble_eater. Wasn't trying to steal your thunder. Just didn't read your post properly until now. :o

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...