Hi Tom,
Yes, that'll work
It's just that you're trading flexibility within the object for flexibility
outside of the object. In other words, externally you won't have to cast
the object, but if you ever need to support another type you'll have to
modify the object itself, meaning recompilation of the entire assembly just
to support another type. That means that assemblies that reference your
assembly may also require recompilation, even though the type is generic.
I don't know the business requirements for your application or how the
object will be used, obviously, but that's certainly something to consider.
--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.ToInt32(NewValue, 0);
> else if (_Value is Boolean) Temp =
> BitConverter.ToBoolean(NewValue, 0);
> else if (_Value is Byte) Temp = NewValue[0];
> else if (_Value is Double) Temp =
> BitConverter.ToDouble(NewValue, 0);
> else if (_Value is String) Temp =
> BitConverter.ToString(NewValue);
> else if (_Value is UInt32) Temp =
> BitConverter.ToUInt32(NewValue, 0);
> else if (_Value is UInt16) Temp =
> BitConverter.ToUInt16(NewValue, 0);
> else throw new InvalidTypeException("Unsupported 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.com/DocProject(Sandcastle
>> in VS IDE)
>>
>> <vtjum...@gmail.com> wrote in
>> messagenews:(E-Mail Removed)...
>>
>> > 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.ToInt32(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
>