Upcasting in VB.NET

B

Bob Trabucco

Does anyone know how you can do an upcase in VB.NET?

Example:

Public Class Class1
Private s As String = "Hello World"
Protected Property Prop1() As String
Get
Return s
End Get
Set(ByVal Value As String)
s = Value
End Set
End Property
End Class

Public Class Class2
Inherits Class1
Public Property Property1()
Get
Return MyBase.Prop1
End Get
Set(ByVal Value)
MyBase.Prop1 = Value
End Set
End Property
End Class

And I attempt to call it using:

Dim c1 As New Class1
Dim c2 As Class2 = c1
Debug.WriteLine(c2.Property1)

Gives a casting error on the Dim c2 line.

So - if I have a class that was created by something not under my control
yet I need to access the protected memebers (in my specific example I have
TCPListener creating a TCPClient and I want to access the RemoteEndPoint) is
there any way to do this?

Thanks in advance...

Bob
 
B

Bob Trabucco

Thanks for the response Sean but that doesn't seem to work either. Still
get the casting error.
 
S

Samuel R. Neff

Casting is changing the declared type of a reference of one object to
a different decalred type with the same reference but the object is
still the same instance. Casting only works when the object itself is
in fact of the type being cast to. For example,

Class C1

Class C2
Inherits C1


Dim a As C1 = New C2
Dim b As C2 = DirectCast(a, C2)

Here you can cast from a to b because even though a is declared as C1
it really contains C2 (which is fine because C2 extends C1). And then
when you want to put this object with is referred to by a in variable
b typed as C2 you have to DirectCast it. This cast is possible
because the actually object really is of type C2, only the variable
that refers to it thinks it's a C1.

The example given

Dim a As C1 = New C1
Dim b As C2 = DirectCast(a, C2)

Will never work because the object itself only is of type C1 and
therefore can not be cast to C2.

In order to take a C1 object instance and put it in a C2 declared
variable you have to create a new C2 object instance and copy the data
from the old instance to the new one--you need a conversion function.

Function ConvertC1toC2(a As C1) As C2
Dim b As New C2
b.Property1 = a.Prop1
Return b
End Function

It may be possible to do a force-cast of incompatible types using
marshal by pointer functionality, but that would not be safe code and
would be very error prone.

HTH,

Sam


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 
P

Phill. W

Bob Trabucco said:
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.
 
B

Bob Trabucco

Thanks for the responses guys...

The problem with trying to derive a class from "TCPClient" and using it is
that the "Client" property is not marked as "Overridable" so the compiler
yells at me when I try to override it.



Dim listener as TcpListener(...)
....

Dim client as TCPClient = listener.AcceptTcpClient


If I try to derive a class from TcpClient and use it instead I cant use the
AcceptTcpClient to get the object and assign it. So the protected members
of the TcpClient are totally impossible to get as far as I can see if using
the TcpListener class!


Thanks for the help guys....

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top