Quote:
Originally Posted by Old Pedant
Well, I'm a little curious as to why the author discourages the use of CSTR(), CDBL(), et al.
They *ARE* part of the VB.NET language/library [see post below]. And under the covers they do exactly the same thing that CTYPE( ) does. So why not use the "shorthand"??
So does the book explain *WHY* " continued use of these methods is not considered best practice"????
|
Page 45 "The disadvantage of these methods is that they have been designed to support any object. This means that if primative type is used, then the method automatically boxes the parameter prior to getting the new value. The result is a loss of performance. Finally, although these are available as methods within the
VB language, the are actually implemented in a class (as with everything in the .NET Framework.) Because the class uses a series of type-specific overloaded methods, the conversions fun faster when the members of the Convert class are called explicitly."
Boxing (Page 34)
Normally, when a converion (implicit or explicit) occurs, the original value is read from its current memory location, and then the new value is assigned. For example, to convert a short to a Long, the system reads the two bytes of Short data and writes them to the appropriate bytes for the Long variable. However, under Visual Basic, if a vaule type needs to be managed as an object, then the system performs an intermediate step. This intermediate step involves taking the value on the stack and copying it to the heap, a process referred to as boxing. As noted earlier, the object class is implimented as a referrence type, so the system needs to convert value types into reference types for them to be objects. This doesn't cause any problems or require any special programming, because boxing isn;t something to declare or directly control, but it does affect performance."
Over loading methods (Not Quoted) means you have more then one version of the same method but they accept different parameters resulting in unique method signatures. Example your class can have both:
Code:
Function RetString(ByVal input as Integer) As string
Return Ctype(input,String)
End Function
Function RetString(ByVal input as Long) As string
Return Ctype(input,String)
End Function
I believe the speed differences between CInt() and CType are directly related to LateBinding vs. Early Binding
CInt(param) would be late binding because the compiler does not know in advance the type of param.
CType(param,Integer) would be an early binging referrence to the Integer class.
Page 56 "Early binding means that code directly interacts with an object by directly calling its methods. Because the Visual Basic compiler knows the object's data type ahead of time, it can directly compile code to invoke the methods on the object....."
"Late binding means your code interacts with an object dynamically at runtime."
Besides the obvious speed advantages of early binding, you need to provide exeception handling when you work with late binding code.
To make the topic even more complex we discuss DirectCast and TryCast which they say are both faster then CType.
Here is a simple test that runs two identical For Loops 1 uses CInt() the other uses CType()
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DebugWatch As New Stopwatch
Dim Counter As Object
Dim CounterMin As Integer = 0
Dim CounterMax As Integer = 10000
Dim ObjectOut As Object
DebugWatch.Reset()
DebugWatch.Start()
For Counter = CounterMin To CounterMax
ObjectOut = CInt(Counter)
Next
DebugWatch.Stop()
Debug.Print("CInt Loop: " & DebugWatch.Elapsed.ToString)
DebugWatch.Reset()
DebugWatch.Start()
For Counter2 = CounterMin To CounterMax
ObjectOut = CType(Counter, Integer)
Next
DebugWatch.Stop()
Debug.Print("CType Loop: " & DebugWatch.Elapsed.ToString)
End Sub
My results:
CInt Loop: 00:00:00.0018095
CType Loop: 00:00:00.0002892
CInt Loop: 00:00:00.0014591
CType Loop: 00:00:00.0009015
CInt Loop: 00:00:00.0013791
CType Loop: 00:00:00.0002828
CInt Loop: 00:00:00.0023131
CType Loop: 00:00:00.0003122
CInt Loop: 00:00:00.0013799
CType Loop: 00:00:00.0007345
That is an insane difference.
Imagine running that loop over a million times say is you wanted to loop through the pixels of a 10 Megal Pixel photo and create an integer value based on the color of each pixel.
But if your writing apps to convert Dim x as Integer = 1 to to a Long once or twice I guess it doesnt matter.