Sunday, July 12, 2009

C# string recognized as variable?

In javascript there is the eval function but how would I do the following in c#?





int myInt=2;





public void changeToVar(string toConvertToVar)


{


int mySecondInt=SomeFunctionOrMethod(toConve...


Response.Write("The result is: "+mySecondInt);


}





changeToVar("myInt");





output:





The result is: 2

C# string recognized as variable?
http://www.odetocode.com/Code/80.aspx
Reply:i don't really understand what you want to do here but the line





changeToVar("myInt");





you declared myInt as integer variable but your passing it as a string. to get the result 2 you should change it to





changeToVar(myInt);





hope this helps in someway...


No comments:

Post a Comment