?
Solved

In C# how to name variables dynamically

Posted on 2012-03-20
4
Medium Priority
?
320 Views
Last Modified: 2012-03-23
]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
Comment
Question by:monica73174
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 37744105
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
 

Author Comment

by:monica73174
ID: 37744113
I just have no idea how many I  need to create.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37744142
Lists are dynamic.  You create as many as you need.

http://dotnetanalysis.blogspot.com/2011/04/list.html
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37744242
Use a Dictionary:
http://msdn.microsoft.com/en-us/library/xfhwa508(VS.85).aspx

The variable name will be the key.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question