Dynamically Creating Variables

  • Thread starter Thread starter ann
  • Start date Start date
A

ann

Is there any way to dynamically creat variables?

In other words, I want to be able to do something like that:

for(int i=0; i<size; i++){

string temp+i = i*3;

}

I want my variables to be temp0, temp1, temp2, etc.
 
Ann,

No, you can not do this. However, you can create a Hashtable or
Dictionary<string, int> which you can key using the strings "temp0",
"temp1", etc, etc.

Hope this helps.
 
ann said:
Is there any way to dynamically creat variables?

In other words, I want to be able to do something like that:

for(int i=0; i<size; i++){

string temp+i = i*3;

}

I want my variables to be temp0, temp1, temp2, etc.

Hi Ann,

Why not use an array?

///
string[] temp = new string[size];

for ( int i = 0; i < size; i++ )
temp = i * 3;
///

Then you can access each element with temp[0], temp[1] ... up to
temp[size-1].
 
Back
Top