lidds Posted June 23, 2005 Posted June 23, 2005 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 Quote
Leaders snarfblam Posted June 23, 2005 Leaders Posted June 23, 2005 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. Quote [sIGPIC]e[/sIGPIC]
jmcilhinney Posted June 23, 2005 Posted June 23, 2005 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"). Quote
Khaledinho Posted June 23, 2005 Posted June 23, 2005 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 Quote Life is the biggest school
iebidan Posted June 24, 2005 Posted June 24, 2005 I'll support Marble_Eater, use arrays, you can't do a dynamic variable naming Quote Fat kids are harder to kidnap
michael_hk Posted June 25, 2005 Posted June 25, 2005 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' Quote There is no spoon. <<The Matrix>>
jmcilhinney Posted June 25, 2005 Posted June 25, 2005 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.