I’m programming a method to query something for the user on the Console and getting his answer… Something like this:
static T query<T>(String queryTxt)
{
Console.Write("{0} = ", queryTxt);
T result;
while (true)
{
try
{
result = // here should go the type casting of Console.ReadLine();
}
catch (FormatException e)
{
Console.WriteLine("Exception: {0};\r\nSource: {1}", e.Message, e.Source);
continue;
}
break;
}
return result;
}
In short, this method should keep asking for the value of queryTxt, where T is always int or double…
Any good way to do this?
Thanks in advance!
Using type converters.