You're absolutely right, Dave.
However, if I do that, then I either have to:
- Write a bunch of specific classes for each type I want to support
or
- Always cast the return value from functions within the class
And I want to avoid that. I do have one way to do it - if I use an
object type variable within the routine, I can check the type; i.e.
T _Value;
object Temp = _Value; // temp to avoid casting
issues
if (_Value is Int32) Temp =
BitConverter.To Int32(NewValue, 0);
else if (_Value is Boolean) Temp =
BitConverter.To Boolean(NewValu e, 0);
else if (_Value is Byte) Temp = NewValue[0];
else if (_Value is Double) Temp =
BitConverter.To Double(NewValue , 0);
else if (_Value is String) Temp =
BitConverter.To String(NewValue );
else if (_Value is UInt32) Temp =
BitConverter.To UInt32(NewValue , 0);
else if (_Value is UInt16) Temp =
BitConverter.To UInt16(NewValue , 0);
else throw new InvalidTypeExce ption("Unsuppor ted type
" + _Value.GetType( ).ToString());
_Value = (T)Temp;
Kind of weird, but it works....
Thanks for the reply.
Tom
On Jan 23, 4:06 pm, "Dave Sexton" <dave@jwa[remove.this]online.com>
wrote:
Hi Tom,
Having to check "_Value is Int32" is an indication that you shouldn't use
generics here. You should just have a regular property of type Object,
which will allow assignment from any type.
--
Dave Sextonhttp://davesexton.com/bloghttp://www.codeplex.co m/DocProject(Sand castle in VS IDE)
<vtjum...@gmail .comwrote in messagenews:11* *************** ******@a75g2000 cwd.googlegroup s.com...
I'm building a C# interface to an existing messaging system. The
messaging system allows values of several types to be sent/recieved
over the interface.
What I want to do is use agenericclass to produce values in the
system. For instance I could create class
MsgGenericValue <UInt16>() which would represent an unsigned value on
the interface.
My issue is converting from byte [] values to thegenerictype T.
So if I have a class: MsgGenericValue <T>
With a member
T _Value
I want do be able to do something like
if (_Value is Int32) _Value =
BitConverter.To Int32(Bytes, 0);
But it doesn't compile - It can't convert from Int32 to 'T'
So how do I do this? Any thoughts would be greatly appreciated
Tom