[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

manipulate variable names

Posted on 2010-09-01
6
Medium Priority
?
364 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
[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
6 Comments
 
LVL 9

Accepted Solution

by:
richard_hughes earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

656 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