Re: Dynamically Creating Variables in C#




<ann.smithry@xxxxxxxxx> wrote:

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.

This isn't possible, but you can use an array instead:

int size = 10;

string[] temp = new string[size];

for(int i = 0; i < temp.Length; i++)
{
temp[i] = (i * 3).ToString();
}

Then you can refer to the elements in the array as temp[0], temp[1],
temp[2], etc... on up to one less than the length of the array.


.



Relevant Pages

  • Dynamically Creating Variables
    ... Is there any way to dynamically creat variables? ... I want my variables to be temp0, temp1, temp2, etc. ...
    (microsoft.public.dotnet.languages.csharp)
  • Dynamically Creating Variables in C#
    ... Is there any way to dynamically creat variables? ... I want my variables to be temp0, temp1, temp2, etc. ...
    (comp.programming)