In article <Xns9535AB438E36Danonemooseyahoocom@140.99.99.130>, Anon-E-Moose wrote:
> "Nice Chap" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> You can indeed cast a base class reference to a sub-class reference
>> and the operation would result in a 'narrowing' conversion. BUT the
>> conversion will succeed ONLY if the base class reference you are
>> trying to cast was originally obtained from an earlier widening
>> conversion from sub-class to base-class.
>
> ??? Can someone re-write this in clearer terms?
>
> Thanks.
>
What the op was saying was that, you can only cast a base class
reference to a subclass if that base class reference points to a
subclass instance.
In other words, casting a subclass to a base class is a narrowing
conversion, in that you loose access to some methods/properties of the
subclass since you'll be accessing it through the base class variable.
Casting a baseclass reference to a subclass would be a widening
conversion, since you would be picking up methods/properties. This is
not a safe operation, unless the base class reference actually points
to a subclass instance... Boy, this is confusing isn't it

Here is
some psuedo code that may help...
class Base
end class
class SubClass
inherits base
end class
' this works
dim b as base = new subclass
dim s as subclass = ctype(b, subclass)
' and this works
dim s as subclass = new subclass
dim b as base = ctype(s, base)
' this doesn't
dim b as base = new base
dim s as subclass = ctype(b, subclass)
--
Tom Shelton [MVP]