Re: Arghhh...strict, but not strict enough
- From: "Scott M." <s-mar@xxxxxxxxxxxxx>
- Date: Sun, 12 Apr 2009 10:46:07 -0400
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
.
- Follow-Ups:
- Re: Arghhh...strict, but not strict enough
- From: Armin Zingler
- Re: Arghhh...strict, but not strict enough
- References:
- Arghhh...strict, but not strict enough
- From: John Whitworth
- Re: Arghhh...strict, but not strict enough
- From: Armin Zingler
- Re: Arghhh...strict, but not strict enough
- From: John Whitworth
- Re: Arghhh...strict, but not strict enough
- From: Cor Ligthert[MVP]
- Re: Arghhh...strict, but not strict enough
- From: Scott M.
- Re: Arghhh...strict, but not strict enough
- From: Armin Zingler
- Arghhh...strict, but not strict enough
- Prev by Date: Re: Arghhh...strict, but not strict enough
- Next by Date: Re: Arghhh...strict, but not strict enough
- Previous by thread: Re: Arghhh...strict, but not strict enough
- Next by thread: Re: Arghhh...strict, but not strict enough
- Index(es):
Relevant Pages
|
Loading