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



CType is not short for "convert" to a type. It's short for "cast" to type.

IMHO the term "convert" is thrown around too easily. Convert implies that a
thing is transformed into a different thing. like a person can convert their
religion, they may do that, but they remain the same person.

That's not what happens with CType. With CType, you may get a duplication
of the original data, but with a different type of reference variable
pointing to that new data.

I just think that the term "convert" is used (even by Microsoft)
incorrectly. It gives the impression that something other that what is
actually happening is taking place.

-Scott


"Cor Ligthert[MVP]" <Notmyfirstname@xxxxxxxxx> wrote in message
news:Ooaq29vuJHA.4476@xxxxxxxxxxxxxxxxxxxxxxx
Scott,

I see therefore CType is a shortcut for Convert Type. That is because it
does not convert, that is simple American logic.

They make it so difficult there in America for people outside the USA.

Although this page state something else, can it be that CType is to
convert and cast as that is possible instead.

http://msdn.microsoft.com/en-us/library/kca3w8x6.aspx

Cor


"Scott M." <s-mar@xxxxxxxxxxxxx> wrote in message
news:uVmvb7suJHA.4592@xxxxxxxxxxxxxxxxxxxxxxx
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: Replies to Upcasting vs downcasting.
    ... I take this example to be a conversion cast, although I can see how it ... could be considered a downcast. ... and "widening conversion" to encompass both reference conversions and non-reference conversions. ... Java always requires a cast for a narrowing conversion. ...
    (comp.lang.java.programmer)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >>explicit interface implementation exists is so an interface name can ... I wouldn't cast to an ... >>Improper conversion sounds like a good description, ... > variable as a sort of implicit reference, ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: 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: Arghhh...strict, but not strict enough
    ... You don't get a conversion with CType. ... difference between CType and directcast? ... only - if I want to cast. ...
    (microsoft.public.dotnet.languages.vb)