Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Can you bind the beautiful Pleiades, and can you loose the cords of Orion? - God to Job...
  • Leaders
Posted

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.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
i see, but i don't get the part when they mention that DirectCast requires the run-time type of an object variable...
Can you bind the beautiful Pleiades, and can you loose the cords of Orion? - God to Job...
Posted

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

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted

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?

Can you bind the beautiful Pleiades, and can you loose the cords of Orion? - God to Job...
Posted (edited)

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)

Edited by AndreRyan
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

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