473,396 Members | 1,942 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,396 software developers and data experts.

Polymorphism question

HI,

How can I access the fields and methods of a subclass in a variable that was
declared as a parent-class and instantiated as a subclass.

Public Class Communication
Public Response As Message
some more stuff
End Class
Public Class Message
some fields, methods etc.
End Class

Public Class Letter
Inherits Message
Public Stamp As String
End Class

Public Class Email
Inherits Message
Public Emailaddress As String
End Class
Now in the main procedure, say pageload, I'd like to easily use code like
this:

Dim Communication1 As New Communication
Communication1.Response.EmailAddress = "bla"

But of course it's not possible, I need to use:
CType(Communication1.Response, Email).EmailAddress = "bla"

Is there a way to accomplish this? I have also tried by providing a
Enum-type variable in the constructor of the Communication class and then to
instantiate "Response" with the chosen subclass but to no avail.

Thank you for your time.
--
Kees
Mar 21 '07 #1
5 916
First of all, the Response property of the Communication class is not an
instance of the Email class, any more than a vehicle is a truck. Let me put
it this way: You're talking about inheritance here. A car is a vehicle; it
inherits vehicle. A truck is a vehicle; it inherits vehicle. But a car is
not a truck. Let's substitute the words "vehicle," "car" and "truck" for
your problem. The Communication class has a public field that is a
"vehicle." You want to be able to treat it as a truck before you define what
type of vehicle it is. Since you don't know what it is, you can only treat
it like a vehicle, that is, access the properties of it which belong to the
vehicle class, both cars and trucks.

In fact, you haven't even instantiated it, that is, created an instance of
the class to work with. You are assigning values to properties of something
that doesn't exist yet. When you create a field, you only create storage
space for something of a certain type. If it is a parent type, any class
which inherits that type is storable there. However, unless the specific
type of the class is known, your app cannot work with the properties of that
specific class. You have to define an instance of the class you want it to
be, assign it to the field of the class ity resides in, and then treat it
like the specific class you have assigned.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Kees de Winter" <no****@nospam.orgwrote in message
news:46***********************@text.nova.planet.nl ...
HI,

How can I access the fields and methods of a subclass in a variable that
was
declared as a parent-class and instantiated as a subclass.

Public Class Communication
Public Response As Message
some more stuff
End Class
Public Class Message
some fields, methods etc.
End Class

Public Class Letter
Inherits Message
Public Stamp As String
End Class

Public Class Email
Inherits Message
Public Emailaddress As String
End Class
Now in the main procedure, say pageload, I'd like to easily use code like
this:

Dim Communication1 As New Communication
Communication1.Response.EmailAddress = "bla"

But of course it's not possible, I need to use:
CType(Communication1.Response, Email).EmailAddress = "bla"

Is there a way to accomplish this? I have also tried by providing a
Enum-type variable in the constructor of the Communication class and then
to
instantiate "Response" with the chosen subclass but to no avail.

Thank you for your time.
--
Kees


Mar 21 '07 #2
Oops in my pseudo-code I forgot the constructor :

Public Class Communication
Public Sub New(ByVal enumType1 as enumType)
if enumType1 = enumType.Email Then Response = New Email
[..]
End Sub
Public Response As Message
End Class

Then the declaration is:
Dim Communication1 As New Communication(enumType.Email)
Communication1.Response.EmailAddress = "bla" << not possible
Thank you Kevin however with this constructor added, I thought the Response
field could know that it should be an Email because it has been instantiated
as such. However apparently it doesn't look at that and sticks with how it
was declared. I don't want to fill my pages with CType's, is there a more
elegant way of coding this?

--
Kees
"Kevin Spencer" <un**********@nothinks.comwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
First of all, the Response property of the Communication class is not an
instance of the Email class, any more than a vehicle is a truck. Let me
put
it this way: You're talking about inheritance here. A car is a vehicle; it
inherits vehicle. A truck is a vehicle; it inherits vehicle. But a car is
not a truck. Let's substitute the words "vehicle," "car" and "truck" for
your problem. The Communication class has a public field that is a
"vehicle." You want to be able to treat it as a truck before you define
what
type of vehicle it is. Since you don't know what it is, you can only treat
it like a vehicle, that is, access the properties of it which belong to
the
vehicle class, both cars and trucks.

In fact, you haven't even instantiated it, that is, created an instance of
the class to work with. You are assigning values to properties of
something
that doesn't exist yet. When you create a field, you only create storage
space for something of a certain type. If it is a parent type, any class
which inherits that type is storable there. However, unless the specific
type of the class is known, your app cannot work with the properties of
that
specific class. You have to define an instance of the class you want it to
be, assign it to the field of the class ity resides in, and then treat it
like the specific class you have assigned.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Kees de Winter" <no****@nospam.orgwrote in message
news:46***********************@text.nova.planet.nl ...
HI,

How can I access the fields and methods of a subclass in a variable that
was
declared as a parent-class and instantiated as a subclass.

Public Class Communication
Public Response As Message
some more stuff
End Class
Public Class Message
some fields, methods etc.
End Class

Public Class Letter
Inherits Message
Public Stamp As String
End Class

Public Class Email
Inherits Message
Public Emailaddress As String
End Class
Now in the main procedure, say pageload, I'd like to easily use code
like
this:

Dim Communication1 As New Communication
Communication1.Response.EmailAddress = "bla"

But of course it's not possible, I need to use:
CType(Communication1.Response, Email).EmailAddress = "bla"

Is there a way to accomplish this? I have also tried by providing a
Enum-type variable in the constructor of the Communication class and
then
to
instantiate "Response" with the chosen subclass but to no avail.

Thank you for your time.
--
Kees


Mar 21 '07 #3
Kees,

As Kevin has pointed out, the problem is that the code accessing your
Communication instance does not (and cannot) know what type the Response
property is other than it derives from Response. Look at it the other way,
if the Response was a Letter, what would happen when it got to the line:

Communication1.Response.EmailAddress = "bla"

The EmailAddress property does not exist on the Letter class!!

If the properties you want to access is common to all instances of Response,
then you could either implement it in the Response class, or declare an
abstract (MustImplement) property in Response and implement it in the sub
classes. If the property is specific to a certain subclass, then you MUST
cast to the appropriate type to be able to access it.

HTH,

Bill

"Kees de Winter" wrote:
Oops in my pseudo-code I forgot the constructor :

Public Class Communication
Public Sub New(ByVal enumType1 as enumType)
if enumType1 = enumType.Email Then Response = New Email
[..]
End Sub
Public Response As Message
End Class

Then the declaration is:
Dim Communication1 As New Communication(enumType.Email)
Communication1.Response.EmailAddress = "bla" << not possible
Thank you Kevin however with this constructor added, I thought the Response
field could know that it should be an Email because it has been instantiated
as such. However apparently it doesn't look at that and sticks with how it
was declared. I don't want to fill my pages with CType's, is there a more
elegant way of coding this?

--
Kees
"Kevin Spencer" <un**********@nothinks.comwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
First of all, the Response property of the Communication class is not an
instance of the Email class, any more than a vehicle is a truck. Let me
put
it this way: You're talking about inheritance here. A car is a vehicle; it
inherits vehicle. A truck is a vehicle; it inherits vehicle. But a car is
not a truck. Let's substitute the words "vehicle," "car" and "truck" for
your problem. The Communication class has a public field that is a
"vehicle." You want to be able to treat it as a truck before you define
what
type of vehicle it is. Since you don't know what it is, you can only treat
it like a vehicle, that is, access the properties of it which belong to
the
vehicle class, both cars and trucks.

In fact, you haven't even instantiated it, that is, created an instance of
the class to work with. You are assigning values to properties of
something
that doesn't exist yet. When you create a field, you only create storage
space for something of a certain type. If it is a parent type, any class
which inherits that type is storable there. However, unless the specific
type of the class is known, your app cannot work with the properties of
that
specific class. You have to define an instance of the class you want it to
be, assign it to the field of the class ity resides in, and then treat it
like the specific class you have assigned.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Kees de Winter" <no****@nospam.orgwrote in message
news:46***********************@text.nova.planet.nl ...
HI,
>
How can I access the fields and methods of a subclass in a variable that
was
declared as a parent-class and instantiated as a subclass.
>
Public Class Communication
Public Response As Message
some more stuff
End Class
>
>
Public Class Message
some fields, methods etc.
End Class
>
Public Class Letter
Inherits Message
Public Stamp As String
End Class
>
Public Class Email
Inherits Message
Public Emailaddress As String
End Class
>
>
Now in the main procedure, say pageload, I'd like to easily use code
like
this:
>
Dim Communication1 As New Communication
Communication1.Response.EmailAddress = "bla"
>
But of course it's not possible, I need to use:
CType(Communication1.Response, Email).EmailAddress = "bla"
>
Is there a way to accomplish this? I have also tried by providing a
Enum-type variable in the constructor of the Communication class and
then
to
instantiate "Response" with the chosen subclass but to no avail.
>
Thank you for your time.
--
Kees
>
>


Mar 21 '07 #4
Ok, thanks for your help. I got it now, what I wanted to do is just not
possible.

Kees
"Bill" <Bi**@discussions.microsoft.comwrote in message
news:8B**********************************@microsof t.com...
Kees,

As Kevin has pointed out, the problem is that the code accessing your
Communication instance does not (and cannot) know what type the Response
property is other than it derives from Response. Look at it the other
way,
if the Response was a Letter, what would happen when it got to the line:

Communication1.Response.EmailAddress = "bla"

The EmailAddress property does not exist on the Letter class!!

If the properties you want to access is common to all instances of
Response,
then you could either implement it in the Response class, or declare an
abstract (MustImplement) property in Response and implement it in the sub
classes. If the property is specific to a certain subclass, then you MUST
cast to the appropriate type to be able to access it.

HTH,

Bill

"Kees de Winter" wrote:
Oops in my pseudo-code I forgot the constructor :

Public Class Communication
Public Sub New(ByVal enumType1 as enumType)
if enumType1 = enumType.Email Then Response = New Email
[..]
End Sub
Public Response As Message
End Class

Then the declaration is:
Dim Communication1 As New Communication(enumType.Email)
Communication1.Response.EmailAddress = "bla" << not possible
Thank you Kevin however with this constructor added, I thought the
Response
field could know that it should be an Email because it has been
instantiated
as such. However apparently it doesn't look at that and sticks with how
it
was declared. I don't want to fill my pages with CType's, is there a
more
elegant way of coding this?

--
Kees
"Kevin Spencer" <un**********@nothinks.comwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
First of all, the Response property of the Communication class is not
an
instance of the Email class, any more than a vehicle is a truck. Let
me
put
it this way: You're talking about inheritance here. A car is a
vehicle; it
inherits vehicle. A truck is a vehicle; it inherits vehicle. But a car
is
not a truck. Let's substitute the words "vehicle," "car" and "truck"
for
your problem. The Communication class has a public field that is a
"vehicle." You want to be able to treat it as a truck before you
define
what
type of vehicle it is. Since you don't know what it is, you can only
treat
it like a vehicle, that is, access the properties of it which belong
to
the
vehicle class, both cars and trucks.
>
In fact, you haven't even instantiated it, that is, created an
instance of
the class to work with. You are assigning values to properties of
something
that doesn't exist yet. When you create a field, you only create
storage
space for something of a certain type. If it is a parent type, any
class
which inherits that type is storable there. However, unless the
specific
type of the class is known, your app cannot work with the properties
of
that
specific class. You have to define an instance of the class you want
it to
be, assign it to the field of the class ity resides in, and then treat
it
like the specific class you have assigned.
>
--
HTH,
>
Kevin Spencer
Microsoft MVP
>
Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
>
"Kees de Winter" <no****@nospam.orgwrote in message
news:46***********************@text.nova.planet.nl ...
HI,

How can I access the fields and methods of a subclass in a variable
that
was
declared as a parent-class and instantiated as a subclass.

Public Class Communication
Public Response As Message
some more stuff
End Class


Public Class Message
some fields, methods etc.
End Class

Public Class Letter
Inherits Message
Public Stamp As String
End Class

Public Class Email
Inherits Message
Public Emailaddress As String
End Class


Now in the main procedure, say pageload, I'd like to easily use code
like
this:

Dim Communication1 As New Communication
Communication1.Response.EmailAddress = "bla"

But of course it's not possible, I need to use:
CType(Communication1.Response, Email).EmailAddress = "bla"

Is there a way to accomplish this? I have also tried by providing a
Enum-type variable in the constructor of the Communication class and
then
to
instantiate "Response" with the chosen subclass but to no avail.

Thank you for your time.
--
Kees


>
>

Mar 21 '07 #5
The type must be known. Since the parent class is the one declared, for the
code to recognize a derived class as what it is, it needs to be told.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Kees de Winter" <no****@nospam.orgwrote in message
news:46**********************@text.nova.planet.nl. ..
Oops in my pseudo-code I forgot the constructor :

Public Class Communication
Public Sub New(ByVal enumType1 as enumType)
if enumType1 = enumType.Email Then Response = New Email
[..]
End Sub
Public Response As Message
End Class

Then the declaration is:
Dim Communication1 As New Communication(enumType.Email)
Communication1.Response.EmailAddress = "bla" << not possible
Thank you Kevin however with this constructor added, I thought the
Response
field could know that it should be an Email because it has been
instantiated
as such. However apparently it doesn't look at that and sticks with how it
was declared. I don't want to fill my pages with CType's, is there a more
elegant way of coding this?

--
Kees
"Kevin Spencer" <un**********@nothinks.comwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
>First of all, the Response property of the Communication class is not an
instance of the Email class, any more than a vehicle is a truck. Let me
put
>it this way: You're talking about inheritance here. A car is a vehicle;
it
inherits vehicle. A truck is a vehicle; it inherits vehicle. But a car is
not a truck. Let's substitute the words "vehicle," "car" and "truck" for
your problem. The Communication class has a public field that is a
"vehicle." You want to be able to treat it as a truck before you define
what
>type of vehicle it is. Since you don't know what it is, you can only
treat
it like a vehicle, that is, access the properties of it which belong to
the
>vehicle class, both cars and trucks.

In fact, you haven't even instantiated it, that is, created an instance
of
the class to work with. You are assigning values to properties of
something
>that doesn't exist yet. When you create a field, you only create storage
space for something of a certain type. If it is a parent type, any class
which inherits that type is storable there. However, unless the specific
type of the class is known, your app cannot work with the properties of
that
>specific class. You have to define an instance of the class you want it
to
be, assign it to the field of the class ity resides in, and then treat it
like the specific class you have assigned.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Kees de Winter" <no****@nospam.orgwrote in message
news:46***********************@text.nova.planet.n l...
HI,

How can I access the fields and methods of a subclass in a variable
that
was
declared as a parent-class and instantiated as a subclass.

Public Class Communication
Public Response As Message
some more stuff
End Class
Public Class Message
some fields, methods etc.
End Class

Public Class Letter
Inherits Message
Public Stamp As String
End Class

Public Class Email
Inherits Message
Public Emailaddress As String
End Class
Now in the main procedure, say pageload, I'd like to easily use code
like
this:

Dim Communication1 As New Communication
Communication1.Response.EmailAddress = "bla"

But of course it's not possible, I need to use:
CType(Communication1.Response, Email).EmailAddress = "bla"

Is there a way to accomplish this? I have also tried by providing a
Enum-type variable in the constructor of the Communication class and
then
to
instantiate "Response" with the chosen subclass but to no avail.

Thank you for your time.
--
Kees




Mar 22 '07 #6

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

Similar topics

3
by: Mayer Goldberg | last post by:
Can someone please explain the motivation behind the following restriction in the language: I define an interface and two classes implementing it: public interface InterA {} public class B...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
3
by: John Salerno | last post by:
Along with events and delegates, polymorphism has been something I sort of struggle with every now and then. First, let me quote the book I'm reading: "Polymorphism is most useful when you have...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.