Var creation in for-loop

Difficult to explain, but I'll show an example of what I want in my code: At the moment I am doing it like this:

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

      

I want to do the creation of these var in a for-loop This is what I thought it should be:

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
}

      

Any ideas on how to do this?

I tried this but it didn't work:

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

+3


source to share


4 answers


you can use a dictionary

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

      

or use an array like this



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

      

remember that arrays are indexed zero.

+7


source


If you know exactly how many instances you will need by creating an array or a list of class instances, you will do what you want.



If you want something a little more complex, you can also create a dictionary where you provide names for each of your class instances, this can provide you with a name-based access mechanism.

+3


source


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"];

      

+1


source


You have to use an array. In your specific case

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

      

You should probably look for more information on which arrays work. See for example https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx

+1


source







All Articles