I have a windows application that allows input from pen input on tablet machine.
When required a form (A) will open another form (B) to allow user input.
In form (B) I have the following function to return the decoded value that the user inputs with pen.
private object decodedValue;
public T GetDecodedValue<T>()
{
return (T)decodedValue;
}
When the user enters a value, in form B, I store the result as follows
string sResult = myInkCollector.Ink.Strokes.ToString();
decodedValue = (object)sResult;
Now in form A there are two fields required that allow data input, one is for Quantity (int) and the other is Price (double). I want to allow form B to accommodate the different types hence that is why I am using generics.
So when form B is accepted, it will return to form A and use the following code
int qty = formB.GetDecodedValue<int>();
or
double price = formB.GetDecodedValue<double>();
This is where the error occurs, when the function 'formB.GetDecodedValue' is called it raises an InvalidCastException at
return (T)decodedValue;
Any ideas on what I am doing wrong? I am over complicating things by using generics?