Results 1 to 5 of 5

Thread: Direct Cast or CType.

  1. #1

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Direct Cast or CType.

    OK, I know this has been posted before so sorry, but anyway...

    What is best to use in this senario...
    Code:
    Sub new(byref b as button)
        AddHandler b.MouseClick, AddressOf ButtonClick
    End Sub
    
    Private sub ButtonClick(byval sender as object, byval e as eventargs)
       Dim b as button = Ctype(sender, button)
       'or
       Dim b as button = DirectCast(sender, button)
       b.text = "blahblah"
    end sub
    Is Ctype only usable when converting an object to the same type while DirectCast can be used to convert an object to the same type or a Base type?

    Thanks in advance...

    Signatures suck

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Direct Cast or CType.

    You can use either and you can use TryCast as well. If you know for a fact that the object will be type Button (or any type derived from Button) then you should use DirectCast. If you're not sure what type the object will be, i.e. the method may be used to handle events of other types too, then you should use TryCast, which will return Nothing if the cast fails rather than throwing an exception. CType performs additional type checking and will convert the object to the specified type if required and a valid conversion exists. There is no way that you want to be performing any object conversions in that code so CType should not be used. Doing so would not cause a problem but does not serve a purpose.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Direct Cast or CType.

    Also, that DirectCast is a little bit better performing since it doesnt do the type checking/validation.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: Direct Cast or CType.

    Thanks guys, I just read something that said dicerctcast performs almost twice as good as Ctype when passing by value and more than twice as good when passing by reference, also that Ctype will convert two different types (ie Integer to String) that are derived from the same base type (object in this case) if it can find a conversion method, where direct cast would fail...

    So this leads me to beleive that you shouldnt really have to use CType at all, as it will only use a known conversion method that is also available to the programmer.
    Would it be safe to assume that quality coding would avoid CType as it will be slower and will use the same conversion methods available to the user, take this for example....

    Code:
    Sub Dosomething(byval obj as object)
    
        if TypeOf obj Is String Then
             msgbox(DirectCast(Obj, String))
        ElseIf TypeOf obj is Integer then
             msgbox(Cstr(DirectCast(obj, Integer)))
        Else
             msgbox("It was an unknown object")
        End If
    End Sub
    
    'Instead of
    
    msgbox(Ctype(obj, string))
    So if I understand right, the above sub should be faster than Ctype even with the If Statement because Ctype is going to have to make that decision anyway, but it will also have to consider every other type, so if it was the last type in its list of possible conversions then it would be much slower, and this is actually better than the CType Function.... Am I getting it?
    Thanks....

    Signatures suck

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Direct Cast or CType.

    Yes, that code will be slowwer then using CType. If you are receiving an object of type "Object" in your arguments then you really should be using CType.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width