-1

I am using Visual Studio 2013 and C#

I want to be able to compare the name of a string variable with a string I create.

The string variable is called print1 (for instance)

public string print1;

I then create a string to like so:

foreach (int number in orderOfNumber)
{
    string printNumber = String.Format("print{0}", number-1);
}

I discard this if 'number' = 1 so I am not concerned with the number ever being 0

What I would like to do is print a string in a MessageBox with a few lines. One line will have the time taken between clicking two pictures on the screen. Basically formatted like this:

//Time taken:

1 and 2 -> 1.234 secs

What I have so far is this:

orderBuilder.Append(String.Format("{0} and {1}\t\t", orderOfNumber[number-1], orderOfPicturesNumber[number])).Append("" + printNumber + "\n");

No matter how I try I always end up with

1 and 2 print1

This is close to what I want as the value for print1 is actually 1.234

Is there a way to replace the 'string' print1 with the original variable print1?

smokeAndMirrors
  • 155
  • 1
  • 5
  • 18
  • 1
    Explain the original task. What you're trying to do looks like a terrible design decision. – zerkms Jan 28 '14 at 23:39
  • 1
    Create an array instead, don't use multiple variables.. – Mike Christensen Jan 28 '14 at 23:45
  • 2
    Why don't you have `string[] prints` (or other [collection](http://msdn.microsoft.com/en-us/library/ybcx56wz.aspx))? Then you can do `prints[number]` to get the expected value without worrying about "print1", "print2", .. "printN" variables. – user2864740 Jan 28 '14 at 23:45
  • @user2864740 That is quite a simple yet genius idea. I can't believe I didn't think of that. That will make it a lot easier to get the correct value. – smokeAndMirrors Jan 28 '14 at 23:47
  • @zerkms I agree, it does look like a terrible design decision. Mainly because it _is_. This is just a way to let the 'operator' know how long it took between each click. The data also gets added to a text file so that the 'operator' can view it later. This was just a requested nice-to-have. – smokeAndMirrors Jan 28 '14 at 23:50

2 Answers2

1

I would create an array of values:

public string[] prints;

You'll, of course, have to initialize it with the number of values you want to hold:

prints = new string[5]; // Holds five strings

Then, you can loop through them as such:

foreach (var number in prints) // Loop through each string in prints
{
    string printNumber = String.Format("print{0}", number);
}

If you want to be able to dynamically add or remove strings for prints, you should probably use a List<string> instead of a string[]

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
0

Try using this code:

PropertyInfo pi = this.GetType().GetProperty(printNumber);
string value = (string)(pi.GetValue(null, null));

Only thing you must do is to declare your variables as properties.

Source: http://www.karpach.com/Reflection-Property-Value.htm

-- EDIT --

I found a method I didn't know about... GetField which can return field values so that you don't need to declare your variables as properties, you can use them directly.

public class SomeClass
{
    public int a0 = 1;
    public int a1 = 2;
    public int a2 = 3;

    public void DoSomething()
    {
        int result = 1;

        for (int index = 0; index < 3; index++)
        {
            string fieldName = "a" + index;

            var field = typeof(SomeClass).GetField(fieldName);
            int fieldValue = (int)field.GetValue(this);

            result *= fieldValue;
        }

        Debug.Assert(result == 6);
    }
}
Aleksandar Toplek
  • 2,792
  • 29
  • 44
  • This is a solution .. but for a different problem. – user2864740 Jan 28 '14 at 23:46
  • I don't think this is what the OP wants to do. They want to build the name of a variable within a string, then get the value of that variable. To my knowledge this is impossible.. – Mike Christensen Jan 28 '14 at 23:47
  • @MikeChristensen sure it's possible – Aleksandar Toplek Jan 29 '14 at 16:37
  • And another thing. I tend to answer people's question, not help them (hence Q&A). Another thing is if he/she asked for advice, then I'd told them that that is not the best way to do things. You don't know what's the real problem behind the question, maybe he/she wants to build some scripting engine or whatnot. – Aleksandar Toplek Jan 29 '14 at 16:44
  • @MikeChristensen `public string print1;` is a field, not a variable. I'm not a psychic. – Aleksandar Toplek Jan 29 '14 at 17:11
  • @MikeChristensen and sure, that's possible too. It just requires more code :) MethodInfo.GetMethodBody() has LocalVariables property where you can retrieve values of local variables and using some magic, you can even retrieve names for those: http://stackoverflow.com/questions/6166767/retrieve-local-variable-name-from-pdb-files – Aleksandar Toplek Jan 29 '14 at 17:13
  • That question pertains to a PDB file, basically a database of debug info. Local variable names are gonna be optimized out of the IL though.. The compiler has no use for them. But ok, I'll concede it is *technically* possible in certain cases if it makes you happy :) – Mike Christensen Jan 29 '14 at 17:17
  • @MikeChristensen yeah I'm aware of that, It's just not right to say something is impossible because you assume he doesn't have access to *.pdb or that he uses variables instead of fields where you can't be sure really. – Aleksandar Toplek Jan 29 '14 at 17:23
  • Thank you both of you for your insight. I had a variable called print1. I also had a variable called printNumber, which was the string "print" + a number in a foreach loop. I wanted to take the printNumber combination and send that to a MessageBox. for instance: the first number is '1' so the printNumber variable would == 'print1' I then wanted to print the value associated with the original print1 string, but I was instead getting the string "print1". I hope that cleared up what I was asking for. :) – smokeAndMirrors Jan 29 '14 at 21:03