Solved

manipulate variable names

Posted on 2010-09-01
6
324 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

13 Experts available now in Live!

Get 1:1 Help Now