what's the diff between cType(x,y) and DirectCast(x,y)?

  • Thread starter Thread starter mp
  • Start date Start date
M

mp

what's the diff between cType(x,y) and DirectCast(x,y)?

they appear to do the same thing

thanks

mark
 
Am 12.07.2010 00:15, schrieb mp:
what's the diff between cType(x,y) and DirectCast(x,y)?

they appear to do the same thing

DirectCast only casts. CType ("C" = convert) can do more:

i = ctype(3.14, integer) 'OK
i = directcast(3.14, integer) 'ERROR

With CType, the compiler creates conversion code if necessary
depending on the expression type and the target type.

This might be considered "more service", so why not always use CType?
Because it can hide a flaw. Example:

dim o as object = 3.14
dim i as integer

i = ctype(o, integer)

If you expect the object in 'o' to be always an Integer,
you won't even get an exception although there's the wrong
data type in 'o' (a Double). With DirectCast, this fault
(which lies somewhere else in the code) would become
obvious sooner because it would throw an InvalidCastException.

So, I recommend to use DirectCast if you really want to cast only.
 
thanks again!
I just discovered the cType function by discovering the suggested fixes for
syntax errors the ide provided when i converted a project from Option strict
off to option strict on, and fixing the many errors discovered.
I'll play with that soon to see if ctype(3.14, integer) is like int(3.14) or
fix(3.14) - truncate or round.
anyway thanks again for the info.

Mark
 
Am 12.07.2010 12:27, schrieb mp:
thanks again!
I just discovered the cType function by discovering the suggested fixes for
syntax errors the ide provided when i converted a project from Option strict
off to option strict on, and fixing the many errors discovered.
I'll play with that soon to see if ctype(3.14, integer) is like int(3.14) or
fix(3.14) - truncate or round.
anyway thanks again for the info.

For the conversion of the basic data types, CInt, CDbl etc are less to
write than CType. CInt(double) is the same as CType(double, integer).
As we've already discussed, these are keywords. Depending on the source
and destination type, the required code is generated. In the case of
CInt(double), Math.Round is called, followed by the actual conversion
to Integer.

Concerning the "real" functions (those in the MSVB library): I wouldn't
use them all. For example, Int (full name Microsoft.VisualBasic.Conversion.Int)
only calls Math.Floor. Therefore I prefer the direct call to Math.Floor.
I don't find Fix in Math.*, so MSVB.Fix is still "valid".


As to the differences, try this:

Public Class Main
Shared Sub Main()

Const fmt As String = "{0,10} {1,15} {2,10} {3,10} {4,10} {5,10}"

Debug.Print(fmt, "value", "Math.Floor", "Int", "Fix", "Cint", "Round")

For i = -30 To 30
Dim d = i / 10

Dim o1 = Math.Floor(d)
Dim o2 = Int(d)
Dim o3 = Fix(d)
Dim o4 = CInt(d)
Dim o5 = Math.Round(d)

Debug.Print(fmt, d, o1, o2, o3, o4, o5)
Next

End Sub
End Class

As you can see, CInt and Math.Round return the same value, but
CInt also converts to an Integer whereas Math.Round returns a Double.
 
Am 12.07.2010 16:21, schrieb Armin Zingler:
As to the differences, try this:

New version. ;) Including rounding "away from zero".


Public Class Main
Shared Sub Main()

Const fmt As String = "{0,10} {1,15} {2,10} {3,10} {4,10} {5,10} {6,10}"

Debug.Print(fmt, "value", "Math.Floor", "Int", "Fix", "Cint", "Round", "Round (afz)")

For i = -40 To 40
Dim d = i / 10

Dim o1 = Math.Floor(d)
Dim o2 = Int(d)
Dim o3 = Fix(d)
Dim o4 = CInt(d)
Dim o5 = Math.Round(d)
Dim o6 = Math.Round(d, MidpointRounding.AwayFromZero)

Debug.Print(fmt, d, o1, o2, o3, o4, o5, o6)
Next

End Sub
End Class
 
thanks again Armin
mark

Armin Zingler said:
Am 12.07.2010 16:21, schrieb Armin Zingler:

New version. ;) Including rounding "away from zero".


Public Class Main
Shared Sub Main()

Const fmt As String = "{0,10} {1,15} {2,10} {3,10} {4,10} {5,10}
{6,10}"

Debug.Print(fmt, "value", "Math.Floor", "Int", "Fix", "Cint",
"Round", "Round (afz)")

For i = -40 To 40
Dim d = i / 10

Dim o1 = Math.Floor(d)
Dim o2 = Int(d)
Dim o3 = Fix(d)
Dim o4 = CInt(d)
Dim o5 = Math.Round(d)
Dim o6 = Math.Round(d, MidpointRounding.AwayFromZero)

Debug.Print(fmt, d, o1, o2, o3, o4, o5, o6)
Next

End Sub
End Class
 
Back
Top