Print expression as is without evaluating it
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);
}
}
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.
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);
精彩评论