[英]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.