473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Upcasting in VB.NET

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


Nov 21 '05 #1
5 5016
Dim c2 As Class2 = DirectCast(c1, Class1)

"Bob Trabucco" <bo**@NOccc-softSPAM.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #2
Thanks for the response Sean but that doesn't seem to work either. Still
get the casting error.

"Sean Hederman" <us***@blogentry.com> wrote in message
news:d1**********@ctb-nnrp2.saix.net...
Dim c2 As Class2 = DirectCast(c1, Class1)

"Bob Trabucco" <bo**@NOccc-softSPAM.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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


Nov 21 '05 #3

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.
Nov 21 '05 #4
"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.
Nov 21 '05 #5
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

"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
news:d1**********@yarrow.open.ac.uk...
"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.

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
Lets suppose I have an class called DatastoreSelect. It has a method called getNextRow(). This method is not abstract, it is fully implemented. It also has a method called setInfoToBeSought, which...
0
by: lawrence | last post by:
In Java one is supposed to upcast an object to its interface whenever appropriate: MyInterface customers = new MySpecificClass(); The idea is that if later one wants to change the specific...
2
by: Judith | last post by:
Hi there everyone I've got: class A { ... }; class B : public A { ... }
2
by: Patrick Kowalzick | last post by:
Dear all, I think I have a very common problem: How shall I upcast an object out of a heterogeneous set. Even worse, I want to avoid the built in C++ RTTI for some reasons. So I thought every...
4
by: juli jul | last post by:
Hello, I did the following with Class A which is a parent and B inherits from it: B b1=new B(); A a1=new A(); a1=b1; Is it an upcasting in c#? Thank you!
4
by: Amod | last post by:
I want to call a child class constructor using the base class instance ... whats the xact mechanism to perform this function ? Regards, Amod
5
by: Mark | last post by:
I can't seem to get this subclass to upcast to a baseclass... see comments below #include <cstdlib> #include <iostream> #include "Base.h" #include "Sub1.h" using namespace std;
3
by: mati-006 | last post by:
Hi, I think the code will be the best way to explain what I mean: #include "arglib/arg_shared.h" class base { public: base() {} virtual ~base() {} };
4
by: majsta | last post by:
Hello, I have the following code. #include <vector> #include <iostream> class Foo { public: Foo(){} virtual void print() const { std::cout << "foo" << std::endl;} };
0
by: Juha Nieminen | last post by:
If I'm not mistaken, general OOP wisdom says that upcasting should usually be avoided if possible. I have a situation, however, where I can't think of a better way than upcasting for this specific...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.