PC Review


Reply
Thread Tools Rate Thread

Converting byte array to generic type

 
 
vtjumper@gmail.com
Guest
Posts: n/a
 
      23rd Jan 2007
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 a generic class 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 the generic type 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

 
Reply With Quote
 
 
 
 
Dave Sexton
Guest
Posts: n/a
 
      23rd Jan 2007
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 Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

<(E-Mail Removed)> wrote in message
news:(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 a generic class 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 the generic type 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
>



 
Reply With Quote
 
vtjumper@gmail.com
Guest
Posts: n/a
 
      29th Jan 2007
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


 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      31st Jan 2007
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

>



 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      31st Jan 2007
Hi,

Actually, the comments in my last post don't apply to your particular
situation since you're using BitConverter, which doesn't accept object - the
original problem Even if you used Object instead of a generic argument
you'd still have to implement the type checking as you have done. And
anyway, there is only a limited number of types that you have to check so
it's no big deal.

I should have looked at your code instead of jumping to conclusions.

--
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

>



 
Reply With Quote
 
vtjumper@gmail.com
Guest
Posts: n/a
 
      1st Feb 2007
Thanks again for the reply. I have this working now...

Tom

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Value of type byte cannot be converted to 1-dimentional array of byte cmdolcet69 Microsoft VB .NET 3 25th Sep 2007 10:10 PM
Converting a byte array to a ushort array fishspm@gmail.com Microsoft C# .NET 1 14th Oct 2006 11:24 AM
Converting byte array to short array? Ole Microsoft C# .NET 2 8th Jul 2006 01:28 AM
converting array of byte data type to string. shivaprasad@techie.com Microsoft C# .NET 5 8th Oct 2005 12:39 AM
Converting a double array to byte array =?Utf-8?B?UmFncw==?= Microsoft Dot NET Framework 5 10th Aug 2005 01:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 AM.