0

Now I'm new for c# development.

I have 100 data from Array and also have 100 variables.

How can I match 100 data with 100 variables?

for example

for(int count = 0 ; count < array.lenght ; count++)
{
    Var+count = array[count];
}

Something like this.

or you guy have another solution please help me. I don't want to do like

set Var1 to Var100 by hand.

More Information Actually I need to add the arrays values to text object in CrystalReport

For example if I want to add the value

 TextObject txtLot1 = (TextObject)report.ReportDefinition.Sections["Section4"].ReportObjects["txtLot1"];

 txtLot1.Text = Arrays[i]

something like this. so , I try to use dictionary but I don't think it will work.

  • 3
    You are having an array, Then why are you going for 100 separate variables? access the values using its index, like what you are doing for assignment(`array[count]`) – sujith karivelil Mar 19 '18 at 12:11
  • 3
    If you need a specific key to retrieve a specific value, you should look at turning your array into a Dictionary – Ryan Wilson Mar 19 '18 at 12:12
  • 2
    I'm honestly not quite sure what your use case is. C# does not allow you to arbitrarily create and reference variables at runtime with names that are not pre-determined. That's why arrays/lists/dictionaries/etc exist. – Yannick Meeus Mar 19 '18 at 12:12
  • Possible duplicate of [Create dynamic variable name](https://stackoverflow.com/questions/20857773/create-dynamic-variable-name) – Drag and Drop Mar 19 '18 at 12:14
  • This is almost certainly an X/Y problem. What are you actually *trying to do* here? The code is the "how you're currently trying to do it", which is not quite the same thing (especially for X/Y) – Marc Gravell Mar 19 '18 at 12:16

1 Answers1

1

Here is an example of doing what you are asking for on the fly with a System.Collections.Generic.Dictionary, all keys in a dictionary must be unique, but since you are appending 1 to each key in your loop this will suffice:

Dictionary<string, int> myKeyValues = new Dictionary<string, int>();

for(int count = 0 ; count < array.length; count++)
{
    //Check to make sure our dictionary does not have key and if it doesn't add key
    if(!myKeyValues.ContainsKey("someKeyName" + count.ToString())
    {
         myKeyValues.Add("someKeyName" + count.ToString(), count);
    }
    else
    {
        //If we already have this key, overwrite, shouldn't happen as you are appending a new int value to key each iteration
        myKeyValues["someKeyName" + count.ToString()] = count;
    }
}
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40