5

I have been trying to figure this out for some time now and can't seem to figure out an answer to it. I don't see why this would be impossible. I am coding in VB.NET.

Here is my problem: I need to dynamically create variables and be able to reference them later on in the code.

More Details: The number of variables comes from some math run against user defined values. In this specific case I would like to just create integers, although I foresee down the road needing to be able to do this with any type of variable. It seems that my biggest problem is being able to name them in a unique way so that I would be able to reference them later on.

Simple Example: Let's say I have a value of 10, of which I need to make variables for. I would like to run a loop to create these 10 integers. Later on in the code I will be referencing these 10 integers.

It seems simple to me, and yet I can't figure it out. Any help would be greatly appreciated. Thanks in advance.

3 Answers 3

15

The best way to do something like this is with the Dictionary(T) class. It is generic, so you can use it to store any type of objects. It allows you to easily store and retrieve code/value pairs. In your case, the "key" would be the variable name and the "value" would be the variable value. So for instance:

Dim variables As New Dictionary(Of String, Integer)()
variables("MyDynamicVariable") = 10  ' Set the value of the "variable"
Dim value As Integer = variables("MyDynamicVariable")  ' Retrieve the value of the variable
Sign up to request clarification or add additional context in comments.

4 Comments

This is the closest you're likely to get but you'll need a dictionary for each data type
@Basic Yes, you would, unless you want to forgo the type-checking and just make it a Dictionary(Of String, Object) in which case the value could be any type of object.
This looks like it will do the trick. I've never used the Dictionary(T) class until this. Thanks for the quick answer and the commented code snippet.
@StevenDoggart Don't even suggest it ;) May as well turn off strict mode too... shudder Also, can you imagine how messy it would get with all those CInt, CDbl, etc to actually use it?
2

You want to use a List

Dim Numbers As New List(Of Integer)

For i As Integer = 0 To 9 
    Numbers.Add(0)
Next

The idea of creating a bunch of named variables on the fly is not something you are likely to see in any VB.Net program. If you have multiple items, you just store them in a list, array, or some other type of collection.

Comments

0
'Dim an Array
Dim xCount as Integer
Dim myVar(xCount) as String

AddButton Event . . .
xCount += 1
myVar(xCount) = "String Value"

You will have to keep Track of what xCount Value is equal to to use.
Typically could be an ID in A DataTable, with a string Meaning

Comments

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.