DirectCast or CType?

fkheng

Centurion
Joined
May 8, 2003
Messages
155
Location
Malaysia
Can somebody explain to me the difference between directcast and ctype? i read the msdn, but could not understand it...

wat do they mean when they say that directcast requires the run time type of the object variable whereas ctype doesn't?

could someone explain these to me?
 
From the Framework Documentation:
The difference between the two keywords 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. [...]
however, the run-time performance of DirectCast is better than that of CType.
And in the example it shows that you cannot convert a number with values in the decimals to an Integer with DirectCast, but with CType.
 
i see, but i don't get the part when they mention that DirectCast requires the run-time type of an object variable...
 
Visual Basic:
CType(1.93823, Int32)

Dim M as Object = CType(New Class1, Object)
MessageBox.Show( DirectCast(M, Class1).MyProperty )
DirectCast is a faster version of CType but it can only convert a object (or base of an inherited class) back into it's original type
 
hm......i'm still confused with the explanations...so sorry if i'm bothering with the questions...

does anyone have another wya of explaning it?
 
Visual Basic:
Public Class AClass
    Protected MyProp as String

    Public Sub New(ByVal AProp as String)
      MyProp = AProp
    End Sub

     Public ReadOnly Property MyProperty() as String
       Get
             Return MyProp
       End Get
     End Property
End Class

Public Class AClass2
     Inherits AClass
     Protected MyProp2 as Int32

     Public Sub New(ByVal AProp as String, AProp2 as Int32)
      MyProp2 = AProp2
      MyBase.New(AProp)
     End Sub

     Public ReadOnly Property MyProperty2() as Int32
       Get
             Return MyProp2
       End Get
     End Property
End Class

Module MainMod
  Public Shared Sub Main()
     Dim Cls1 as AClass
     Dim Cls2 as AClass2

     Console.Write("Creating Object")
     Cls2 = New AClass2("Hello", 123)

     Console.Write("Converting to it's base")
     Cls1 = CType(Cls2, AClass)

     Console.Write("Property MyProperty of AClass is: " & Cls1.MyProperty)

     Console.Write("Turning Cls1 back into AClass2 using DirectCast")
     Console.Write("Result of MyProperty2 is: " & [b][color=blue]DirectCast[/color][/b]([i]Cls1[/i], [u]AClass2[/u]).MyProperty2())

     Console.Read()
   End Sub
End Module
Paste the code into a Console App
Bold = DirectCast Function
Italic = Class(A Class that has been CType-ed into it's base {or an Object which is the base of everything})
Underline = What the DirectCast will turn the Object back into

The DirectCast Function converts a Class that has been turned into one of its bases(ie. AClass2 turned into AClass can then have the AClass Object turned back into AClass2 but it it wasn't an AClass2 to start with the call will fail)
 
Last edited:
Back
Top