Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Casting variables in VB.NET

Casting variables in VB.NET

Scheduled Pinned Locked Moved Visual Basic
questioncsharp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jim Taylor
    wrote on last edited by
    #1

    What is the difference between Directcast and CType? Also is it better to use CType(MyObect, Integer) than CInt(MyObject) - does it get compiled to the same MSIL or is a helper function called in the Microsoft.VisualBasic.dll? Jim

    N D 2 Replies Last reply
    0
    • J Jim Taylor

      What is the difference between Directcast and CType? Also is it better to use CType(MyObect, Integer) than CInt(MyObject) - does it get compiled to the same MSIL or is a helper function called in the Microsoft.VisualBasic.dll? Jim

      N Offline
      N Offline
      Nick Seng
      wrote on last edited by
      #2

      While DirectCast and CType are both used for casting, DirecCast can only case object to object they have a direct relationship (ie. inheritance ) with. For example, if B inherits A, and C inherits A, you can use DirectCast to cast a C object to A. OTOH, CType can be used to cast from any object to any object. The advantage of DirectCast is that it's much faster than CType, so use it whenever appropriate. Looking through the IL for CType and CInt, they're actually doing the same thing, so I assume there's no difference in using either.


      "if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler. Support Bone

      1 Reply Last reply
      0
      • J Jim Taylor

        What is the difference between Directcast and CType? Also is it better to use CType(MyObect, Integer) than CInt(MyObject) - does it get compiled to the same MSIL or is a helper function called in the Microsoft.VisualBasic.dll? Jim

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        CType is more flexible because it's not limited to converting between standard types. It can be used to convert between composite types as well as converting an object to any one of its interfaces. CType is also compiled in-line with the expression whereas the CInt is not. CInt calls a library function to convert an already evaluated expression result whereas CType code is actually compiled as part of the expression. Now, DirectCast... The difference between the two is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType Consider the following code:

        Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
        Dim I As Integer = CType(Q, Integer) ' Succeeds.
        Dim J As Integer = DirectCast(Q, Integer) ' Fails.

        The run-time type of Q is Double. CType succeeds because Double can be converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer. Clear as mud? ;) RageInTheMachine9532

        J 1 Reply Last reply
        0
        • D Dave Kreskowiak

          CType is more flexible because it's not limited to converting between standard types. It can be used to convert between composite types as well as converting an object to any one of its interfaces. CType is also compiled in-line with the expression whereas the CInt is not. CInt calls a library function to convert an already evaluated expression result whereas CType code is actually compiled as part of the expression. Now, DirectCast... The difference between the two is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType Consider the following code:

          Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
          Dim I As Integer = CType(Q, Integer) ' Succeeds.
          Dim J As Integer = DirectCast(Q, Integer) ' Fails.

          The run-time type of Q is Double. CType succeeds because Double can be converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer. Clear as mud? ;) RageInTheMachine9532

          J Offline
          J Offline
          Jim Taylor
          wrote on last edited by
          #4

          Thanks, that is a lot clearer. Jim

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups