简体   繁体   English

如何通过调用字符串C#中的变量名称来设置值

[英]How to set value by call variable name from string c#

Now I'm new for c# development. 现在我是C#开发的新手。

I have 100 data from Array and also have 100 variables. 我有100个来自Array的数据,也有100个变量。

How can I match 100 data with 100 variables? 如何将100个数据与100个变量匹配?

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. 手动将Var1设置为Var100。

More Information Actually I need to add the arrays values to text object in CrystalReport 详细信息实际上,我需要将数组值添加到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. 因此,我尝试使用字典,但我认为它不起作用。

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: 这是一个使用System.Collections.Generic.Dictionary快速执行所需操作的示例,字典中的所有键都必须是唯一的,但是由于您要在循环中的每个键后面附加1,因此就足够了:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM