Hi mates,
How to use a string , to be a variable name, for example
suppose X="WELL"
then I want to have an array with this name -> double [] WELL = new double[10];
more specifically I want to create a set of arraies, in which the name of array is obtained by a loop:
for ( int i=0;i<20;i++)
{
string X= "WELL"+i.ToString();
double [] X = new double [10]; //???
}
Then I want to have a set of array :
double [] WELL1 =new double [10];
double [] WELL2 =new double [10];
...
double [] WELL19 = new double[10];
Thanks,
How to use a string as a name of a variable in c#
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Guest UserRank #1,422 · 1.2K reputationPosted Dec 17, 2010, 7:21 AM EST
It isn't possible to do this in C#, but you can assign a name to each array of doubles if you store the string/array pairs in a generic dictionary as in this example:
// use a generic dictionary to hold the "WELL" names and arrays of doubles doubleHolder = new Dictionary();
Dictionary
// create the dictionary elements, based on your loop
for ( int i = 0; i < 20; i++ )
{
string x = "WELL" + i.ToString();
double[] arr = new double[ 10 ];
doubleHolder.Add( x, arr );
}
// you can then reference each array of doubles by the "WELL" name
double[] w2 = doubleHolder[ "WELL2" ];
Sam HobbsRank #107 · 20.5K reputationPosted Dec 17, 2010, 6:49 PM EST