繁体   English   中英

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

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

现在我是C#开发的新手。

我有100个来自Array的数据,也有100个变量。

如何将100个数据与100个变量匹配?

例如

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

这样的事情。

或者您还有其他解决方案,请帮助我。 我不想做喜欢

手动将Var1设置为Var100。

详细信息实际上,我需要将数组值添加到CrystalReport中的文本对象。

例如,如果我想添加值

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

 txtLot1.Text = Arrays[i]

这样的事情。 因此,我尝试使用字典,但我认为它不起作用。

这是一个使用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