Making var 's in a for-loop

123 views Asked by At

It is hard to explain but I will show an example of what I want in my code: At the moment I do it this way:

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 making of those var's 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];

4

There are 4 answers

3
Hamid Pourjam On BEST ANSWER

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

keep in mind that arrays have zero based indexes.

0
npinti On

If you know for sure, the amount of instances you will need, creating an array or list of class instances will do what you are after.

If you want something more sophisticated, you could also create a dictionary in which you provide names to each of your class instances, this could provide you with an access by name sort of thing mechanism.

0
stefankmitph On
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"];
0
MariusUt On

You should use an array. In your particular 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 about what arrays an how they work. See for example https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx