The only other way I could think of to do it (if you want to get rid of the if's) would be to use reflection to get the Parse
method.
Code modified from:
http://www.c-sharpcorner.com/Code/20...Reflection.asp
Check MSDN for the MethodInfo and BindingFlags, I think you'll need another using statement (or fully specify it here in the code
itself).
MethodInfo mi;
object result = null;
object[] args = new object[] {"ABC123"};
Type t = FindMyType(); // might be int, float, double, etc
mi = t.GetMethod("Parse");
if (mi != null)
{
result = t.InvokeMember ("Parse", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, args);
}
Not sure if that would be considered any "cleaner" though.
--
Adam Clauss
ca*****@tamu.edu
"Bob Rundle" <ru****@rundle.com> wrote in message news:On**************@TK2MSFTNGP10.phx.gbl...
Yes,
That is what I am doing now....not very elegant.
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:ux**************@tk2msftngp13.phx.gbl... Hmm, only way I could think of would be something like this:
Type t = FindMyType(); // might be int, float, double, etc
object v = null;
if (t == typeof(double))
v = double.Parse(s);
else if (t == typeof(int))
v = int.Parse(s);
... etc
Maybe someone else knows a better way?
--
Adam Clauss
ca*****@tamu.edu
"Bob Rundle" <ru****@rundle.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl... I would like to get something like this to work...
Type t = FindMyType(); // might be int, float, double, etc
string s = "1233";
object v = t.Parse(s);
This doesn't work of couse, Parse is not a member of Type.
I have to think that this is possible. Is it?
Regards,
Bob Rundle