PC Review


Reply
Thread Tools Rate Thread

Casting in VB.Net and C#

 
 
Ken Allen
Guest
Posts: n/a
 
      16th Dec 2003
I am relatively new to .Net, but have been using VB and C/C++ for years.

One of the drawbacks with VB6 and earlier was the difficulty in casting a
'record' to a different 'shape' so one could perform different manipulations
on it. For example, I have a complex data structure, which I can represent
in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
length array of unsigned bytes so that I could perform a checksum
calculation on the contents. Another case is where a read a buffer from a
file and then wish to interpret the contents based on some header
information. This was also very difficult in VB6.

What facilities are available in VB.Met and C# (which does not have
pointers, which made this so easy, albeit error prone, in C and C++)?

-Ken


 
Reply With Quote
 
 
 
 
Jan Tielens
Guest
Posts: n/a
 
      16th Dec 2003
In VB.NET you can use:
- CType
- DirectCast

In C# you can use:
- (typeName)

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


"Ken Allen" <(E-Mail Removed)> wrote in message
news:ee#xkA#(E-Mail Removed)...
> I am relatively new to .Net, but have been using VB and C/C++ for years.
>
> One of the drawbacks with VB6 and earlier was the difficulty in casting a
> 'record' to a different 'shape' so one could perform different

manipulations
> on it. For example, I have a complex data structure, which I can represent
> in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
> length array of unsigned bytes so that I could perform a checksum
> calculation on the contents. Another case is where a read a buffer from a
> file and then wish to interpret the contents based on some header
> information. This was also very difficult in VB6.
>
> What facilities are available in VB.Met and C# (which does not have
> pointers, which made this so easy, albeit error prone, in C and C++)?
>
> -Ken
>
>



 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      16th Dec 2003
Just to clarify the difference between CType and DirectCast.

DirectCast can only be used on reference types
and they have to be the same type. Whereas, CType
can be used on both Value Types and Reference Types
where the casting is permissable.

DirectCast is faster at runtime than CType.

Regards - OHM#


Jan Tielens wrote:
> In VB.NET you can use:
> - CType
> - DirectCast
>
> In C# you can use:
> - (typeName)
>
>
> "Ken Allen" <(E-Mail Removed)> wrote in message
> news:ee#xkA#(E-Mail Removed)...
>> I am relatively new to .Net, but have been using VB and C/C++ for
>> years.
>>
>> One of the drawbacks with VB6 and earlier was the difficulty in
>> casting a 'record' to a different 'shape' so one could perform
>> different manipulations on it. For example, I have a complex data
>> structure, which I can represent in a VB6 TYPE declaration, but I
>> cannot easily convert that to a fixed length array of unsigned bytes
>> so that I could perform a checksum calculation on the contents.
>> Another case is where a read a buffer from a file and then wish to
>> interpret the contents based on some header information. This was
>> also very difficult in VB6.
>>
>> What facilities are available in VB.Met and C# (which does not have
>> pointers, which made this so easy, albeit error prone, in C and C++)?
>>
>> -Ken


Regards - OHM# (E-Mail Removed)


 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      16th Dec 2003
"One Handed Man [ OHM# ]"
<OneHandedMan@&REMOVE&TO%MAIL%MEBTInternet.com> schrieb
> Just to clarify the difference between CType and DirectCast.
>
> DirectCast can only be used on reference types
> and they have to be the same type. Whereas, CType
> can be used on both Value Types and Reference Types
> where the casting is permissable.


Only a remark: DirectCast can also be used with value types when the object
is boxed:

Dim o As Object
Dim i As Integer
o = 17I
i = DirectCast(o, Integer)


@Ken:
DirectCast performs type casting whereas CType can perform type casting
_and_ type conversion. Type casting only changes the type of the reference,
but it does not create a new object. Type conversion does create a new
object.

> DirectCast is faster at runtime than CType.


Under which circumstances? I read this also before but I could not reproduce
it. The speed was equal.


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      16th Dec 2003
Ken,
> What facilities are available in VB.Met and C# (which does not have
> pointers, which made this so easy, albeit error prone, in C and C++)?

Personally that is the key word "error prone"! .NET goes out of its way to
avoid such error prone code!

> One of the drawbacks with VB6 and earlier was the difficulty in casting a
> 'record' to a different 'shape' so one could perform different

manipulations
> on it.

Luckily VB6 & VB.NET have type safety! I consider this a major benefit not a
drawback!

Note a VB6 Type is a VB.NET Structure, however I would recommend using a
Class instead.

> I have a complex data structure, which I can represent
> in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
> length array of unsigned bytes so that I could perform a checksum
> calculation on the contents.

I would "serialize" the structure to a MemoryStream using a BinaryWriter. I
would then calculate the checksum on the MemoryStream. Both MemoryStream &
BinaryWriter are in the System.IO namespace. Alternatively you can use
System.GitConverter.GetBytes to get byte arrays for individual members
however the BinaryWriter & MemoryStream makes this significantly easier!

> Another case is where a read a buffer from a
> file and then wish to interpret the contents based on some header
> information.

I would use an BinaryReader to read the header information interpreting &
"deserializing" as I go. BinaryReader is also in the System.IO namespace.

By "serialize" and "deserialize" I mean I would implement custom routines
that would convert an object to & from a binary format using the
BinaryReader & BinaryWriter classes similar to what is described in the
following article:

http://msdn.microsoft.com/library/de...rp09182003.asp

Note the samples are C#, with your C++ background you should be able to
convert them.

An alternative to the BinaryReader & BinaryWriter is the
System.Runtime.InteropSerivces.Marshal class. Check out the StructureToPtr &
PtrToStructure methods along with the Copy or Read methods. Although the
method is StructureToPtr you can use them with either a VB.NET Class or a
Structure...

Hope this helps
Jay

"Ken Allen" <(E-Mail Removed)> wrote in message
news:ee%23xkA%(E-Mail Removed)...
> I am relatively new to .Net, but have been using VB and C/C++ for years.
>
> One of the drawbacks with VB6 and earlier was the difficulty in casting a
> 'record' to a different 'shape' so one could perform different

manipulations
> on it. For example, I have a complex data structure, which I can represent
> in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
> length array of unsigned bytes so that I could perform a checksum
> calculation on the contents. Another case is where a read a buffer from a
> file and then wish to interpret the contents based on some header
> information. This was also very difficult in VB6.
>
> What facilities are available in VB.Met and C# (which does not have
> pointers, which made this so easy, albeit error prone, in C and C++)?
>
> -Ken
>
>



 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      16th Dec 2003
But its primary Type is still Object

Regards - OHM

Armin Zingler wrote:
> "One Handed Man [ OHM# ]"
> <OneHandedMan@&REMOVE&TO%MAIL%MEBTInternet.com> schrieb
>> Just to clarify the difference between CType and DirectCast.
>>
>> DirectCast can only be used on reference types
>> and they have to be the same type. Whereas, CType
>> can be used on both Value Types and Reference Types
>> where the casting is permissable.

>
> Only a remark: DirectCast can also be used with value types when the
> object is boxed:
>
> Dim o As Object
> Dim i As Integer
> o = 17I
> i = DirectCast(o, Integer)
>
>
> @Ken:
> DirectCast performs type casting whereas CType can perform type
> casting _and_ type conversion. Type casting only changes the type of
> the reference, but it does not create a new object. Type conversion
> does create a new object.
>
>> DirectCast is faster at runtime than CType.

>
> Under which circumstances? I read this also before but I could not
> reproduce it. The speed was equal.


Regards - OHM# (E-Mail Removed)


 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      16th Dec 2003
Everytime you post, I realise how little I know.

Regards - OHM#


Jay B. Harlow [MVP - Outlook] wrote:
> Ken,
>> What facilities are available in VB.Met and C# (which does not have
>> pointers, which made this so easy, albeit error prone, in C and C++)?

> Personally that is the key word "error prone"! .NET goes out of its
> way to avoid such error prone code!
>
>> One of the drawbacks with VB6 and earlier was the difficulty in
>> casting a 'record' to a different 'shape' so one could perform
>> different manipulations on it.

> Luckily VB6 & VB.NET have type safety! I consider this a major
> benefit not a drawback!
>
> Note a VB6 Type is a VB.NET Structure, however I would recommend
> using a Class instead.
>
>> I have a complex data structure, which I can represent
>> in a VB6 TYPE declaration, but I cannot easily convert that to a
>> fixed length array of unsigned bytes so that I could perform a
>> checksum calculation on the contents.

> I would "serialize" the structure to a MemoryStream using a
> BinaryWriter. I would then calculate the checksum on the
> MemoryStream. Both MemoryStream & BinaryWriter are in the System.IO
> namespace. Alternatively you can use System.GitConverter.GetBytes to
> get byte arrays for individual members however the BinaryWriter &
> MemoryStream makes this significantly easier!
>
>> Another case is where a read a buffer from a
>> file and then wish to interpret the contents based on some header
>> information.

> I would use an BinaryReader to read the header information
> interpreting & "deserializing" as I go. BinaryReader is also in the
> System.IO namespace.
>
> By "serialize" and "deserialize" I mean I would implement custom
> routines that would convert an object to & from a binary format using
> the BinaryReader & BinaryWriter classes similar to what is described
> in the following article:
>
>

http://msdn.microsoft.com/library/de...rp09182003.asp
>
> Note the samples are C#, with your C++ background you should be able
> to convert them.
>
> An alternative to the BinaryReader & BinaryWriter is the
> System.Runtime.InteropSerivces.Marshal class. Check out the
> StructureToPtr & PtrToStructure methods along with the Copy or Read
> methods. Although the method is StructureToPtr you can use them with
> either a VB.NET Class or a Structure...
>
> Hope this helps
> Jay
>
> "Ken Allen" <(E-Mail Removed)> wrote in message
> news:ee%23xkA%(E-Mail Removed)...
>> I am relatively new to .Net, but have been using VB and C/C++ for
>> years.
>>
>> One of the drawbacks with VB6 and earlier was the difficulty in
>> casting a 'record' to a different 'shape' so one could perform
>> different manipulations on it. For example, I have a complex data
>> structure, which I can represent in a VB6 TYPE declaration, but I
>> cannot easily convert that to a fixed length array of unsigned bytes
>> so that I could perform a checksum calculation on the contents.
>> Another case is where a read a buffer from a file and then wish to
>> interpret the contents based on some header information. This was
>> also very difficult in VB6.
>>
>> What facilities are available in VB.Met and C# (which does not have
>> pointers, which made this so easy, albeit error prone, in C and C++)?
>>
>> -Ken


Regards - OHM# (E-Mail Removed)


 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      16th Dec 2003
"One Handed Man [ OHM# ]"
<OneHandedMan@&REMOVE&TO%MAIL%MEBTInternet.com> schrieb
> But its primary Type is still Object


Type of what? After DirectCast it is Integer.


--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

 
Reply With Quote
 
One Handed Man [ OHM# ]
Guest
Posts: n/a
 
      16th Dec 2003
Sorry, but when you assigned it 17i you already cast it. So the types are
the same by the time it gets to direct cast.

However, I am a little confused over this, because after casting occurs 'o'
still maintains the Type 'Object' in the Locals window although the Object
pointed to is of Type Integer.

Can you adequately explain this to me ?

Regards - OHM#


Armin Zingler wrote:
> "One Handed Man [ OHM# ]"
> <OneHandedMan@&REMOVE&TO%MAIL%MEBTInternet.com> schrieb
>> But its primary Type is still Object

>
> Type of what? After DirectCast it is Integer.


Regards - OHM# (E-Mail Removed)


 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      16th Dec 2003
Doh!
> System.GitConverter.GetBytes to get byte arrays for individual members

That should be System.BitConverter.GetBytes.

I'm not sure what a GitConverter does, but I'm sure it involves Gits. ;-)

Jay

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:OOMUQZ$(E-Mail Removed)...
> Ken,
> > What facilities are available in VB.Met and C# (which does not have
> > pointers, which made this so easy, albeit error prone, in C and C++)?

> Personally that is the key word "error prone"! .NET goes out of its way to
> avoid such error prone code!
>
> > One of the drawbacks with VB6 and earlier was the difficulty in casting

a
> > 'record' to a different 'shape' so one could perform different

> manipulations
> > on it.

> Luckily VB6 & VB.NET have type safety! I consider this a major benefit not

a
> drawback!
>
> Note a VB6 Type is a VB.NET Structure, however I would recommend using a
> Class instead.
>
> > I have a complex data structure, which I can represent
> > in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
> > length array of unsigned bytes so that I could perform a checksum
> > calculation on the contents.

> I would "serialize" the structure to a MemoryStream using a BinaryWriter.

I
> would then calculate the checksum on the MemoryStream. Both MemoryStream &
> BinaryWriter are in the System.IO namespace. Alternatively you can use
> System.GitConverter.GetBytes to get byte arrays for individual members
> however the BinaryWriter & MemoryStream makes this significantly easier!
>
> > Another case is where a read a buffer from a
> > file and then wish to interpret the contents based on some header
> > information.

> I would use an BinaryReader to read the header information interpreting &
> "deserializing" as I go. BinaryReader is also in the System.IO namespace.
>
> By "serialize" and "deserialize" I mean I would implement custom routines
> that would convert an object to & from a binary format using the
> BinaryReader & BinaryWriter classes similar to what is described in the
> following article:
>
>

http://msdn.microsoft.com/library/de...rp09182003.asp
>
> Note the samples are C#, with your C++ background you should be able to
> convert them.
>
> An alternative to the BinaryReader & BinaryWriter is the
> System.Runtime.InteropSerivces.Marshal class. Check out the StructureToPtr

&
> PtrToStructure methods along with the Copy or Read methods. Although the
> method is StructureToPtr you can use them with either a VB.NET Class or a
> Structure...
>
> Hope this helps
> Jay
>
> "Ken Allen" <(E-Mail Removed)> wrote in message
> news:ee%23xkA%(E-Mail Removed)...
> > I am relatively new to .Net, but have been using VB and C/C++ for years.
> >
> > One of the drawbacks with VB6 and earlier was the difficulty in casting

a
> > 'record' to a different 'shape' so one could perform different

> manipulations
> > on it. For example, I have a complex data structure, which I can

represent
> > in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
> > length array of unsigned bytes so that I could perform a checksum
> > calculation on the contents. Another case is where a read a buffer from

a
> > file and then wish to interpret the contents based on some header
> > information. This was also very difficult in VB6.
> >
> > What facilities are available in VB.Met and C# (which does not have
> > pointers, which made this so easy, albeit error prone, in C and C++)?
> >
> > -Ken
> >
> >

>
>



 
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
Down-casting of Typed Collection (Casting Generic Types?) conchur Microsoft C# .NET 1 3rd Jul 2006 11:45 AM
casting juli jul Microsoft C# .NET 3 19th Oct 2005 06:08 PM
Need help with casting Keith Selbee Microsoft C# .NET 6 7th May 2004 08:12 AM
Casting!!!?!?!?!? André Almeida Maldonado Microsoft ASP .NET 1 14th Jan 2004 06:44 PM
Re: Casting Gregory Persson Microsoft C# .NET 0 7th Aug 2003 03:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:37 AM.