Solved

In C# how to name variables dynamically

Posted on 2012-03-20
4
299 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
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why, and when, to use Windows Workflow 1 32
Icons and Colors for Terms 3 24
C# Offline Apllication 5 55
Angular JS Route 3 54
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now