Florian Storck

Hi,

I thought of having a function which performs a numeric conversion ( which can fail or which produces loss of digits ) instead of writing:

Code Block

double d = 23.4;

int i = (int)d;


My implementation of course doesn't works, because "as" only works with ReferenceTypes ( and as I suppose wouldn't do a numeric conversion.) and using the direct cast isn't possible in Generics ( as far as I know).


Code Block

public ValueType TrySetNumericValue<ValueType>(double dValue)
{
try

{

ValueType Value;

/* // using as, doesn't works


if(dValue as ValueType != null)
{
Value = dValue as ValueType;
return Value;
}

*/
// this of course works neither,
// I would need a kind of TryCast()...
Value = (ValueType)dValue;

}
catch (Exception e)
{

}
return default(ValueType);
}


If anyone asks himself: why I'm doing such strange stuff, I'm having here a kind of Database holding generic types, where most are Numbers (integers and doubles mixed), but not all.

Thanks for comments,
Florian




Re: Visual C# General Doing generic numeric conversion casts...

bola shokry

I just wonder,

why you dont use Convert class





Re: Visual C# General Doing generic numeric conversion casts...

Florian Storck


Hi,

I think Convert wouldn't work because the target is unknown at runtime. Below is a snippet of the member function i now implemented in my generic parameter class. It was the fastest and simpliest solution I could think of ( because I mostly use only three numeric types). If I would use Convert, I would have to do the same type determination of the underlying type, so there's no benefit. The generic "Param" class is derived from a BaseParam, which is untyped.

Code Block


public virtual bool TrySetNumericValue(double dVal)
{
try
{
Param<int> pInt = this as Param<int>;
Param<short> pShort = this as Param<short>;
Param<double> pDouble = this as Param<double>;
if (pInt != null)
pInt.Value = (int)dVal;
else if (pShort != null)
pShort.Value = (short)dVal;
else if (pDouble != null)
pDouble.Value = dVal;
else
return false;

return true;
}
catch (Exception e)
{
SysGlobal.LogSystem.ErrorFormat("Numeric cast failed: {0} / {1}", Key, e.Message);
}

return false;
}





Re: Visual C# General Doing generic numeric conversion casts...

Peter Ritchie

You target must be known in order to convert from something else to it.

Could you explain a little more what you're trying to do It's not making sense. If you're trying to conver a double value to something that will fit into your database, the target column must be defined as a certain type.






Re: Visual C# General Doing generic numeric conversion casts...

Florian Storck


Hello Peter,

yes, it's probably looking a little bit strange... first, "Database" is maybe missleading, I'm having a generic dictionary, which holds my generic Param<T> types. Most of them are int, short, double,string. If I access these types, I know the underlying type. But for some application, I wanted to have a single funtcion, which takes a double. If the underlying Type is numeric ( int, short , double, whatever) the double input value should be converted to the target numeric format, regardless of any precision loss. The easiest way (for me) looked like the standard cast operator. I'm not sure, but I think, in C++ this would have been possible, because if I try to cast a double to a string for example, this would give a compiletime error. But meanwhile I'm having some doubts about that...

The other way now is to have this if..else statement, to determine if the underlying type is a certain numeric type and the cast is peformed. This works for me, but I looked for a more generic solution restricted to numeric types.

Thanks,
Florian




Re: Visual C# General Doing generic numeric conversion casts...

Peter Ritchie

Dealing with assignment and calculations with generics has long been very complicated.

This should do what you want:

Code Block

public virtual bool TrySetValue ( double val )

{

System.ComponentModel.TypeConverter tc =

System.ComponentModel.TypeDescriptor.GetConverter(val.GetType());

if(tc == null || !tc.CanConvertTo(typeof(T)))

{

return false;

}

this.value = (T)tc.ConvertTo(val, typeof(T));

return true;

}






Re: Visual C# General Doing generic numeric conversion casts...

Florian Storck

Hi Peter,

this looks quite nice ! Thanks a lot !

Bye,
Florian