如何通过调用字符串C#中的变量名称来设置值 [英] How to set value by call variable name from string c#

查看:42
本文介绍了如何通过调用字符串C#中的变量名称来设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Now I'm new for c# development.

我从Array获得了100个数据,还拥有100个变量。

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

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

How can I match 100 data with 100 variables?

例如

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

类似这样的东西。

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

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

手动将Var1设置为Var100。

set Var1 to Var100 by hand.

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

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

例如,如果我想添加值

 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.

推荐答案

以下是做您的事的一个示例使用System.Collections.Generic.Dictionary即时请求,字典中的所有键都必须是唯一的,但是由于您要将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;
    }
}

这篇关于如何通过调用字符串C#中的变量名称来设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆