|
-
Aug 1st, 2007, 01:30 AM
#1
Thread Starter
Hyperactive Member
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...
-
Aug 1st, 2007, 01:45 AM
#2
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.
-
Aug 1st, 2007, 01:47 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 1st, 2007, 02:30 AM
#4
Thread Starter
Hyperactive Member
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....
-
Aug 1st, 2007, 02:50 AM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|