Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Can some explain in laymans terms the difference between the to convertion keywords please.

 

I think I have it but just want to be sure. The help says

 

Dim Q As Object = 2.37

Dim I As Integer = CType(Q,Integer)

Dim J as Integer = DirectCast(Q,Integer)

 

I succeeds as 2.37 converts to an Integer...

 

J fails as Q was declared as an Object not an Integer so DirectCast fails.

 

Question is why use DirectCast instead of CType

 

Apologies incase this is a stupid question

My website
  • *Gurus*
Posted

DirectCast is used to cast from one class to a sub or superclass of that type. The Convert class is used to convert one type to another, for example "3.141" to 3.141.

 

The CType function is a VB helper designed to decide which one to use. I avoid it as it isn't available in any other language.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...