"Bob Trabucco" <bo**@NOccc-softSPAM.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Public Class Class1
End Class
Public Class Class2
Inherits Class1
End Class
And I attempt to call it using:
Dim c1 As New Class1
Dim c2 As Class2 = c1
First Thought: You can't do that!
Your c1 variable can hold objects of Type Class1 or Class2,
since the one (actually the '2) derives from the other.
Your c2 variable can /only/ hold objects of Type Class2;
a Class1 object "doesn't fit".
Why? The Type of an object defines the things you can do
with it. Your Class1 Type defines some stuff; your Class2
Type defines some /different/ stuff.
*If* the compiler /allowed/ your upcast, you'd almost certainly
get a MissingMethodException as soon as you tried to call any
method on the variable c2, as in
.... c2.Property1
because the /object/ you've put into the variable simply
doesn't have this property - being a Class1 Object, it has
a Prop1 property but knows /nothing/ about the Property1
property defined in Class2. However, because you've defined
it as a Class2 variable, the compiler only allows you to do Class2
"things" to it.
I need to access the protected members
.. . . is there any way to do this?
Protected properties are available only to derived class so, to get
at them, you have to create your own class, derived from the one
you want to fiddle with, as in (air-code) :
Class Sneak
Inherits TCPListener
' This is where it gets a little vague ...
*Public* Overrides Property ProtectedThing() as ...
Get
Return MyBase.ProtectedThing
End Get
End Property
End Class
So now, you can use your derived class everywhere that you
would have used the TCPListener class (everything that wants to
treat it /as/ a TCPListener can do so because your class "is a"
TCPListener) but you can make use of your publically exposed,
previously protected property.
HTH,
Phill W.