views:

46

answers:

2

i want to print the expression Xmin and Ymin as is without calculating the final value . i,e with the values of I and J as 1,2,3,4,5

example when I=1

Xmin= Xmin ((1 - 1)*10 + (1 - 1)*1) 

is there a way to do it .. I tried the following code, but no luck:

int a, g;
a = 10;
g = 1;
for (int J=1; J<=5; J++)
{
    for (int I = 1; I <= 5; I++)
    {
        string Xmin = Convert.ToString((I - 1)*a + (I - 1)*g);
        string Ymin = Convert.ToString((J - 1) * a);
        Debug.WriteLine("X=" + Xmin + "Y=" + Ymin);
    }
}
+2  A: 

You must use String.Format:

string Xmin = String.Format("({0} - 1)*{1} + ({0} - 1)*{2}", I, a, g);

Also, in .NET 3.5 you can use expression trees, but I daresay that would be a much more complicated solution than just using String.Format.

Lasse V. Karlsen
Thank You very much and have a nice day Raj
Raj
I'm tempted to throw in an `Expression` version, just for chuckles... but it would probably be a hundred lines (or so), compared to your one-liner ;-p
Marc Gravell
+1  A: 

You need to put the expression in a string in order to do that, perhaps using String.Format

string Xmin = String.Format("Xmin=({0} - 1)*{1} + ({0} - 1)*{2}", I, a, g);
string Ymin = String.Format("Ymin=({0} - 1) * {1}", J, a);

Debug.WriteLine("X=" + Xmin + "Y=" + Ymin);
Zach Johnson