• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

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

0
monica73174
Asked:
monica73174
  • 2
1 Solution
 
Kyle AbrahamsSenior .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.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now