Solved

manipulate variable names

Posted on 2010-09-01
6
346 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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