fkheng Posted June 12, 2003 Posted June 12, 2003 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? Quote Can you bind the beautiful Pleiades, and can you loose the cords of Orion? - God to Job...
Leaders Iceplug Posted June 14, 2003 Leaders Posted June 14, 2003 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. Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
fkheng Posted June 15, 2003 Author Posted June 15, 2003 i see, but i don't get the part when they mention that DirectCast requires the run-time type of an object variable... Quote Can you bind the beautiful Pleiades, and can you loose the cords of Orion? - God to Job...
AndreRyan Posted June 15, 2003 Posted June 15, 2003 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 Quote .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?
fkheng Posted June 15, 2003 Author Posted June 15, 2003 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? Quote Can you bind the beautiful Pleiades, and can you loose the cords of Orion? - God to Job...
AndreRyan Posted June 17, 2003 Posted June 17, 2003 (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 June 17, 2003 by AndreRyan Quote .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?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.