I am trying to create a Generic class to handle float and doubles. I need to perform computation according to the type of the variable (float or double), but I am not sure whether the following implementation is the right way to do it. Need some suggestions on it.
// computeFloat is a method of some other class which actually computes and returns a float value
float computeFloat()
{
float a;
....
return a;
}
// setFloat is a method of some other class which actually sets a float value
void setFloat(float val)
{
.....
}
TestClass<T> : IDisposable
{
public void getValue(ref T val)
{
if(val is float)
{
object retVal = computeFloat();
val = (float)retVal;
}
else
{
throw new Exception(" Not implemented");
}
}
public void setValue(T val)
{
if(val is float)
{
object obj = val as object;
float retVal = (float)obj;
setFloat(retVal);
}
else
{
throw new Exception(" Not implemented");
}
}
}