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



Not quite Armin, You don't get a conversion with CType. You may get a
duplication of data boxed or unboxed into a new type if your input and
output parametes are reference and value types or vice versa. When both of
the inputs are of the same fundamental type, you just get a cast of the
reference.

A conversion implies that the original data is transformed into something
else and CType doesn't do that. It *can* copy data and put that into a new
type, but it can't convert the data into a new type.

-Scott


"Armin Zingler" <az.nospam@xxxxxxxxxx> wrote in message
news:el6hWBduJHA.5764@xxxxxxxxxxxxxxxxxxxxxxx
John Whitworth wrote:
Thanks Armin - I got there from Patrice's message. But is there a
difference between CType and directcast?

DirectCast only casts. CType (also) converts if necessary. Even if both
lead
to the same (IL) code in this case, I prefer Directcast - personal taste
only - if I want to cast. With CType, the VB compiler generates the code
required to do the conversion in other cases.

The difference between casting and converting is that a conversion creates
a new object while casting just changes the type of the reference.


Armin



.



Relevant Pages

  • Re: Arghhh...strict, but not strict enough
    ... happens with CType. ... As for DirectCast, my description IS correct: ... before the cast of the reference is done. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Collections
    ... cast an abstract class into a derived class ... First remember that DirectCast is the Cast operator, while CType is the ... Conversion operator that will do a Cast if the cast is allowed. ... remember that Casting & Converting are separate ...
    (microsoft.public.dotnet.languages.vb)
  • 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)

Loading