David:
Not sure how the nomenclature could get confusing... I don't want a new
object, but I end up creating one because casting an object from the base
class to a sub class is not supported.
Public Class A
End Class
Public Class B
Inherits A
End Class
Dim objA As New A
Dim objB As New B
You can write
objA = objB
objA = DirectCast(objB, A)
objA = CType(objB, A)
but not
B = A
objB = DirectCast(objA, B)
objB = CType(objA, B)
Note that no compiler warnings occur, but there are runtime exceptions
thrown (InvalidCastException). So, yes, it *is* true that you can't cast an
object from a base class to an object of a subclass of that base class.
In the end, that makes sense, because you could get around any restrictions
on required property values that have been imposed on objects of the
subclass type of course... Still, it would be nice to be able to just cast.
--
Sven.
"David" <df*****@woofix.local.dom> wrote in message
news:slrnd6kp5l.8c7.df*****@woofix.local.dom...
On 2005-04-23, SA <in*********@freemail.nl> wrote: David:
DirectCast (or CType) doesn't work... That only works from subclass to
base class, but not the other way around.
No, that's not true, which is why I said you should understand the
nomenclature before trying to understand the options.
Like I said, if what you really want is a new object, then forget any
kind of analogy to "casting" and the problem gets a lot easier.