Re: Arghhh...strict, but not strict enough



Sorry Armin, I have to disagree with your assesments here.

In your first example, there is new data created for the variable i that is
the integer representation of the s data, the s data is not directly
converted or affected. In my opinion, the term "converted" means that the
data in a memory location is directly modified to new data. That's not what
happens with CType. A new memory location is created with the desired data.

As for DirectCast, my description IS correct: "DirectCast does not include
any logic to actually check for and convert an objet to the requested type",
while CType does. This is because DirectCast is meant to be used in
situations where the type to be converted is already known to be the same
type as the target type and that's why it performs better (Professional
Visual Basic 2008; Bill Evjen - WROX, page 47).

My point about CType erroring out is that although it does perform a check
to see if the types are compatible, it can still error out at runtime (as
you point out).

-Scott


"Armin Zingler" <az.nospam@xxxxxxxxxx> wrote in message
news:O3hb4StuJHA.1164@xxxxxxxxxxxxxxxxxxxxxxx
Scott M. wrote:
Not really Cor.

Neither of them convert anything.

No, CType does convert if necessary. Otherwise this wouldn't work:

Dim s As Single
Dim i As Integer
s = 3.14
i = CType(s, Integer) 'Or CInt

Variable i will contain the value 3, not a copy of the binary presentation
of the same 4 bytes as in s.

DirectCast is used when cating to the true underlying type that a
reference points to and the validity of the cast need not be verified
before the cast of the reference is done.

That's not true either. The check is done, too. Otherwise you wouldn't get
an InvalidCastException if the cast is invalid.

CType is used when you wish to *attempt* a cast of a reference from
one type to another. CType will first check to see if the cast is
possible and then perform the cast.

DirectCast does the same.

Note that even if the cast is
possible, it still may fail at runtime due to null references, etc.

Sorry, not true again. You can cast Nothing to any type. While casting,
you
will never get a NullReferenceException at runtime. Only at compile time -
when it is not yet known whether the value is Nothing - you might get an
error due to an invalid assignment.

Also, CType works with both value and reference types, but DirectCast
only works with Reference Types.

Right. :-)


In addition, don't forget the user defined CType operator.

Armin



.



Relevant Pages

  • Re: using arraylist
    ... The DirectCast will attempt to make a cast, and throw an exception if it fails. ... "Göran Andersson" schreef in bericht ... CType can be used as is told to the class that it is convertible. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: using arraylist
    ... The DirectCast will attempt to make a cast, and throw an exception if it ... CType is better, however because that they think that the DirectCast ... rather than opinions and third each of us can test the code on our ...
    (microsoft.public.dotnet.languages.vb)
  • Re: using arraylist
    ... If it can't cast it, it returns? ... that there is a C# equivalent for DirectCast. ... CType can be used as is told to the ... rather than opinions and third each of us can test the code on our ...
    (microsoft.public.dotnet.languages.vb)
  • Re: using arraylist
    ... If it can't cast it, it returns? ... that there is a C# equivalent for DirectCast. ... CType can be used as is told to the ... rather than opinions and third each of us can test the code on our ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Arghhh...strict, but not strict enough
    ... That's not what happens with CType. ... convert and cast as that is possible instead. ... output parametes are reference and value types or vice versa. ... A conversion implies that the original data is transformed into something ...
    (microsoft.public.dotnet.languages.vb)

Loading