Solved

manipulate variable names

Posted on 2010-09-01
6
334 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
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.

 
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

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

17 Experts available now in Live!

Get 1:1 Help Now