create varible at runtime

H

Husam

Hi EveryBody:

I am using vb.net 2005 and the following code trying to create varible at
runtime:

For n=1 to 4
dim v+n.tostring(n) as double
Next n

the resone behaind using the previous code is to create

Dim v1(4) as double
dim v2(4) as double
dim v3(4) as double
dim v4(4) as double

but it dose not work,Can some body help me or direct me how can I do so?

regard's

Husam
 
M

Michel Posseth [MCP]

AFAIK what you try to do is not possible ( or i am sure missing something )
however you could acomplish your task by using a array of doubles or if you
need to access the values as key value pairs you could go for a hashtable
both of them would work fine .

hth

Michel
 
G

Göran Andersson

Husam said:
Hi EveryBody:

I am using vb.net 2005 and the following code trying to create varible at
runtime:

For n=1 to 4
dim v+n.tostring(n) as double
Next n

the resone behaind using the previous code is to create

Dim v1(4) as double
dim v2(4) as double
dim v3(4) as double
dim v4(4) as double

but it dose not work,Can some body help me or direct me how can I do so?

regard's

Husam

What Michael says is true, it's not possible to create variables at
runtime. It simply doesn't make sense to have dynamic variables in a
compiled language.

There are several options for accomplishing something that you can use
similar to dynamic variables. Here are some:

1. A two dimensional array:

Dim v(3, 3) As Double
v(0, 0) = 3.14
v(3, 3) = 42

2. A jagged array, i.e. an array of arrays.

3. A list or arrays:

Dim v As new List(Of Double())

4. A list of lists:

Dim v As new List(Of List(Of Double))
v.Add(new List(Of Double)())
v(0).Add(3.14)

5. A dictionary of arrays:

Dim v As new Dictionary(Of Integer, Double())

6. A Dictionary of lists:

Dim v As New Dictionary(Of Integer, List(Of Double))
v(42) = new List(Of Double)()
v(42).Add(3.14)

....and so on. It all depends on what you want to use it for.

It would perhaps be better if you explained what it is that you want to
accomplish, instead of asking about how to do it the way that you think
that it should be done...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top