Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 制定var';这是一个循环_C#_.net_For Loop_Var - Fatal编程技术网

C# 制定var';这是一个循环

C# 制定var';这是一个循环,c#,.net,for-loop,var,C#,.net,For Loop,Var,这很难解释,但我将展示一个示例,说明我在代码中想要什么: 目前我是这样做的: var something1 = new (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet); var something2 = new (Microsoft.O

这很难解释,但我将展示一个示例,说明我在代码中想要什么: 目前我是这样做的:

var something1 = new (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);
var something2 = new (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);
var something3 = new (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);

something1.Name = "sheet1";
something2.Name = "sheet2";
something3.Name = "sheet3";
我想在for循环中制作这些变量 我认为应该是这样的:

for (int i=1;i<4;i++)
{
   var ("something" +i) = new Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet); // this (of course) doenst work
}

for(int i=1;i如果您确实知道所需的实例数量,那么创建一个数组或类实例列表就可以满足您的需要


如果您想要更复杂的东西,还可以创建一个字典,在其中为每个类实例提供名称,这可以为您提供一种按名称访问的机制。

您可以使用字典

var somethigs = new Dictionary<int, xxx.ApplicationClass>();
for (var i = 1; i < 4; i++)
{
    somethigs[i] = new xxx.ApplicationClass();
}

//access them like this

somethigs[1].Name = "sheet1";
somethigs[2].Name = "sheet2";
var somethings=newdictionary();
对于(变量i=1;i<4;i++)
{
somethings[i]=新的xxx.ApplicationClass();
}
//像这样访问它们
somethings[1].Name=“sheet1”;
somethings[2].Name=“sheet2”;
或者使用这样的数组

var somethigs = new xxx.ApplicationClass[4];
for (var i = 0; i < 4; i++)
{
    somethigs[i] = new xxx.ApplicationClass();
}

somethigs[0].Name = "sheet1";
somethigs[1].Name = "sheet2";
var somethings=new xxx.ApplicationClass[4];
对于(变量i=0;i<4;i++)
{
somethings[i]=新的xxx.ApplicationClass();
}
somethings[0]。Name=“sheet1”;
somethings[1].Name=“sheet2”;
请记住,数组具有基于零的索引。

Dictionary Dictionary=new Dictionary();
Dictionary<string, ApplicationClass> dictionary = new Dictionary<string, ApplicationClass>();
for(int i = 0; i < 4; i++) {
    dictionary.Add("something" + i, new xxx.ApplicationClass());
}

var myApplicationClass = dictionary["something1"];
对于(int i=0;i<4;i++){ Add(“something”+i,new xxx.ApplicationClass()); } var myApplicationClass=dictionary[“something1”];
您应该使用数组。在您的特定情况下

var something = new xxx.ApplicationClass[4];
for (int i = 0; i < 3; i++)
{
    something[i] = new (Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing, appExcel.Worksheets[appExcel.Worksheets.Count], 1, XlSheetType.xlWorksheet);
    something[i].Name = "sheet" + (i + 1).ToString();
}
var something=new xxx.ApplicationClass[4];
对于(int i=0;i<3;i++)
{
something[i]=new(Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing,appExcel.Worksheets[appExcel.Worksheets.Count],1,XlSheetType.xlWorksheet);
something[i].Name=“sheet”+(i+1).ToString();
}

你可能需要了解更多关于数组的信息以及它们是如何工作的。例如,参见< /p>为什么不创建一个你的代码> xxx?Apple(C++)/>代码>对象的数组?这让我想起了C++中的令牌粘贴:我不建议使用字典。冒着听起来有点屈尊俯就的风险,我想说OP对数组不是很熟悉。他肯定应该先学习数组,然后再学习字典。不管怎样,我确实觉得数组更适合这个问题。首先:非常感谢你帮助我。我对我的问题进行了一些编辑,以便更清楚地知道我到底想要什么。你能把你的密码改成它吗。那么这又和我的问题一致了?提前谢谢。如果你不想编辑它,也可以:)嘿,我试过这个:

var something=new(Microsoft.Office.Interop.Excel.Worksheet)appExcel.Worksheets.Add(Type.Missing,appExcel.Worksheets[appExcel.Worksheets.Count],1,XlSheetType.xlWorksheet)[4]