Solved

manipulate variable names

Posted on 2010-09-01
6
341 Views
Last Modified: 2012-05-10
I have two variables :
string variable0 = "";
string variable1="";

i want to get them populated insidde a loop

for (i = 0;i <2;i++)
{
what i want over here is what ever the loop index that variable should be populated
variable(i) = "test" + i  // how do i do this line
}
0
Comment
Question by:pratikshahse
6 Comments
 
LVL 9

Accepted Solution

by:
richard_hughes earned 500 total points
ID: 33577036
Hello pratikshahse

Try something like this:

Thanks,

Richard Hughes
string[] myStrings = new string[2];

for (int i = 0; i < 2; i++)
{
    myStrings[i] = "test" + i;
}

Open in new window

0
 
LVL 16

Expert Comment

by:kris_per
ID: 33577107

You can have the variables as properties in a class or structure and then you can use Reflection to make the property name in the loop and set value for that  property...
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33577153
Continuing above comment...sample code below:
    public class MyObject
    {
        public string Name0;
        public int Name1;
    }

///////////////////

            MyObject obj = new MyObject();
            Type mt = typeof(MyObject);
            for (int i = 0; i < 2; i++)
            {
                string propertyName = "Name" + i;
                PropertyInfo pi = mt.GetProperty(propertyName);
                pi.SetValue(obj, "test" + i, null);
            }

Open in new window

0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 16

Expert Comment

by:kris_per
ID: 33577205

This one is better....
public class MyObject
    {
        public string Name0;
        public string Name1;
    }

///////////////////

            MyObject obj = new MyObject();
            Type mt = typeof(MyObject);
            for (int i = 0; i < 2; i++)
            {
                string propertyName = "Name" + i;
                FieldInfo fi = mt.GetField(propertyName);
                fi.SetValue(obj, "test" + i);
            }

Open in new window

0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33577229
Hi,

Check code.

Regards,
VSS

using System;
using System.Collections.Generic;
using System.Text;

namespace String
{
    class Program
    {
        static void Main(string[] args)
        {
            string variable0 = "";
            string variable1 = "";

            for (int i = 0; i < 2; i++)
            {
               if(i.Equals(0))
               {
                variable0="test"+i.ToString();
                Console.WriteLine(variable0);
               }

                if(i.Equals(1))
                {
                    variable1="test"+i.ToString();
                    Console.WriteLine(variable1);
                }               
            }
            Console.ReadLine();
        }
    }
}

Open in new window

0
 

Expert Comment

by:ajran
ID: 33577736
switch (i) {
case 0:
 variable0="test0";
break;
case 1:
 variable1="test1";
break;
}
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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