In C# how to name variables dynamically

]I need to create an a variable set to an object dynamically using the code below.  There can be anywhere from 0 to many so I need to set lr with a numeric value for example lr1, lr2 etc... I am realizing what I have below cannot be done with a simple concatination.  

 [embed=snippet 8250291
         foreach (int i in labreports)
            {
                MHLServices.FaxQueueRequestILabReport "lr" + i.ToString() = new MHLServices.FaxQueueRequestILabReport();
                lr + i.lr_uid = labreports.items[i];
                fax.FaxQueueRequestILabReports.Add(lr + i);
            }

Open in new window

monica73174Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
You don't.   Create a List and refer to it by index.

Why not just add it directly:




foreach (int i in labreports)
{
   fax.FaxQueueRequestILabReports.Add(labreports.items[ i ]);
}
0
 
monica73174Author Commented:
I just have no idea how many I  need to create.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Lists are dynamic.  You create as many as you need.

http://dotnetanalysis.blogspot.com/2011/04/list.html
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Use a Dictionary:
http://msdn.microsoft.com/en-us/library/xfhwa508(VS.85).aspx

The variable name will be the key.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.