Chad,
I wouldn't call VB the king language of anti type safety. At least, not
anymore. And, of course, only if you set
Option Strict True
at the beginning of each source file or in the project settings.With this
setting, you can only cast those types implicitly, which are implicitly
castable in C#, too.
To provide more background: VB internally uses a special flag to recognize a
type for casting implicitly. When you write a static operator implicit
function with C#, C# generates a procedure called op_implicit, if I remember
correctly. You can use this function from VB, too, but only by this name (so
it's not implicitly usable, obviously...)
However, you can't set the flag from C# (and, unfortunately, neither from
VB), so a type conversion could be done implicitly from VB.
But DirectCast works fine in VB:
Dim var as mySpecialObject =DirectCast(spe cialObjectBoxed InWhatEverType,
mySpecialObject )
Keep in mind, that DirectCast only does the same as the cast operator does
in C#: (In this example specialObjectBo xedInWhatEverTy pe boxed an object of
type mySpecialObject ). It just unboxes a type. If you really want to do a
type conversion from one type to another, you should provide a constructor
with a parameter for your class, you could use like this:
Dim var as new mySpecialObject (OtherSpecialOb jectToCastFrom) .
In the constructor for mySpecialObject provide the code for the actual
conversion.
Klaus.
"Chad Z. Hower aka Kudzu" <cp**@hower.org > schrieb im Newsbeitrag
news:Xn******** **********@127. 0.0.1...
"Klaus Löffelmann" <fo***********@ loeffelmann.de> wrote in
news:40******** **************@ news.freenet.de : Whidbey will support that (and hopefully it will be produce code that is
compatible with the code generated by C# when using the implicit
operator); in the meanwhile, you have to use the DirectCast keyword to
unbox an object.
Ack. :(
I understand that there are differences between VB and C#. But how can MS
leave someting like this out? Esp from VB, the king language of anti type
safety. Where you could string = integer. :(
Can you give me an example of DirectCast?
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com