473,654 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Polymorphism

Hi!

I'm struggling a bit with Polymorphism in vb.net. There is something I know
I can do in C++, but I cannot seem to do it in vb.net (or c# for that
matter).

Basically, I have an abstract base class that deals with adding, deleting,
editing another class.

I have written an implementation of that class, and also a derived version
of the 'managed' class - so effectively my derived version manages a
specialised version of whatever the base one does. However, the compiler
complains that I havent implemented the MustInherit methods...

To explain a bit better...
Public Class CMessage
' This is the "managed" class

Public MustInherit Class CMessageManager
' This is the abstract base class
Public MustOverride Function MessageReceived (ByRef objMessage As
CMessage) As Boolean

Public Class CEmail Inherits CMessage
' Derived version

Public Class CEmailMessageMa nager Inherits CMessageManager
' Implemented version - want it to deal with emails

Public MustOverride Function MessageReceived (ByRef objMessage As CEmail)
As Boolean
' Pass in an email and deal with it
This concept is supported by C++ - it must be possible to do it in .NET, but
I really dont know how.

Any help would be greatly appreciated!


Nov 20 '05 #1
30 2009
In article <O$************ **@TK2MSFTNGP09 .phx.gbl>, Richard Tappenden wrote:
Hi!

I'm struggling a bit with Polymorphism in vb.net. There is something I know
I can do in C++, but I cannot seem to do it in vb.net (or c# for that
matter).

Basically, I have an abstract base class that deals with adding, deleting,
editing another class.

I have written an implementation of that class, and also a derived version
of the 'managed' class - so effectively my derived version manages a
specialised version of whatever the base one does. However, the compiler
complains that I havent implemented the MustInherit methods...

To explain a bit better...
Public Class CMessage
' This is the "managed" class

Public MustInherit Class CMessageManager
' This is the abstract base class
Public MustOverride Function MessageReceived (ByRef objMessage As
CMessage) As Boolean

Public Class CEmail Inherits CMessage
' Derived version

Public Class CEmailMessageMa nager Inherits CMessageManager
' Implemented version - want it to deal with emails

Public MustOverride Function MessageReceived (ByRef objMessage As CEmail)
As Boolean
' Pass in an email and deal with it
This concept is supported by C++ - it must be possible to do it in .NET, but
I really dont know how.

Any help would be greatly appreciated!


I'm not sure what the problem is - the compiler is telling you exactly
what the problem is... If you want to be able to use
CEmailMessageMa nager then you must override the MessageReceived
function..

Public Class CEmailMessageMa nager Inherits CMessageManager

Public Overrides Function MessageReceived (ByRef objMessage As
CEmail) As Boolean
' Do stuff
End Function
End Class

Even in C++ this is the rule. You can't use a function that has no
implementation. The rules for using Abstract base classes are pretty
much the same in VB.NET as the are in C++.
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #2
Hi Tom,

Thanks for the reply - but I think you've misunderstood the question - I'm
well aware that I can create an instance of an abstract class etc...

The problem is that I cant seem to override the base class methods unless
they all use the base message class to do the operations - i.e:

The base class method =
Public MustOverride Function MessageReceived (ByRef objMessage As CMessage)
As Boolean

My derived version
Public Overrides Function MessageReceived (ByRef objMessage As CEmailMessage)
As Boolean
' Do something
End Function.

The overridden version complains - stating that I still need to override
MessageReceived (ByRef objMessage CMessage) As Boolean.

In C++, you can do this - i.e. it resolves the virtual table correctly.

For example - check these c++ files...(apolog ies if they don't look nice, I
knocked them up quickly to prove the point)

"Tom Shelton" <to*@mtogden.co m> wrote in message
news:uQ******** ******@TK2MSFTN GP09.phx.gbl...
In article <O$************ **@TK2MSFTNGP09 .phx.gbl>, Richard Tappenden

wrote:
Hi!

I'm struggling a bit with Polymorphism in vb.net. There is something I know I can do in C++, but I cannot seem to do it in vb.net (or c# for that
matter).

Basically, I have an abstract base class that deals with adding, deleting, editing another class.

I have written an implementation of that class, and also a derived version of the 'managed' class - so effectively my derived version manages a
specialised version of whatever the base one does. However, the compiler
complains that I havent implemented the MustInherit methods...

To explain a bit better...
Public Class CMessage
' This is the "managed" class

Public MustInherit Class CMessageManager
' This is the abstract base class
Public MustOverride Function MessageReceived (ByRef objMessage As
CMessage) As Boolean

Public Class CEmail Inherits CMessage
' Derived version

Public Class CEmailMessageMa nager Inherits CMessageManager
' Implemented version - want it to deal with emails

Public MustOverride Function MessageReceived (ByRef objMessage As CEmail) As Boolean
' Pass in an email and deal with it
This concept is supported by C++ - it must be possible to do it in .NET, but I really dont know how.

Any help would be greatly appreciated!


I'm not sure what the problem is - the compiler is telling you exactly
what the problem is... If you want to be able to use
CEmailMessageMa nager then you must override the MessageReceived
function..

Public Class CEmailMessageMa nager Inherits CMessageManager

Public Overrides Function MessageReceived (ByRef objMessage As
CEmail) As Boolean
' Do stuff
End Function
End Class

Even in C++ this is the rule. You can't use a function that has no
implementation. The rules for using Abstract base classes are pretty
much the same in VB.NET as the are in C++.
--
Tom Shelton
MVP [Visual Basic]







Nov 20 '05 #3
"Richard Tappenden" <ri******@mailk ey.com> schrieb
The problem is that I cant seem to override the base class methods
unless they all use the base message class to do the operations -
i.e:

The base class method =
Public MustOverride Function MessageReceived (ByRef objMessage As
CMessage) As Boolean

My derived version
Public Overrides Function MessageReceived (ByRef objMessage As
CEmailMessage) As Boolean
' Do something
End Function.

The overridden version complains - stating that I still need to
override MessageReceived (ByRef objMessage CMessage) As Boolean.
The derived class must contain a procedure with the same signature. In your
case, the signature is different because the type of the argument is
CEmailMessage, not CMessage as in the base class.
In C++, you can do this - i.e. it resolves the virtual table
correctly.

For example - check these c++ files...(apolog ies if they don't look
nice, I knocked them up quickly to prove the point)

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
In article <u1************ **@TK2MSFTNGP09 .phx.gbl>, Richard Tappenden wrote:
Hi Tom,

Thanks for the reply - but I think you've misunderstood the question - I'm
well aware that I can create an instance of an abstract class etc...

The problem is that I cant seem to override the base class methods unless
they all use the base message class to do the operations - i.e:

The base class method =
Public MustOverride Function MessageReceived (ByRef objMessage As CMessage)
As Boolean

My derived version
Public Overrides Function MessageReceived (ByRef objMessage As CEmailMessage)
As Boolean
' Do something
End Function.

The overridden version complains - stating that I still need to override
MessageReceived (ByRef objMessage CMessage) As Boolean.


Aah, now I understand and Armin hit the nail on the head. You have not
overridden the base function because the sig does not match. You are
passing a CEmailMessage - not CMessage, which is what the base class
defines.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #5
Hi Armin.

Thanks for the reply - I understand what you are saying, however the
CEmailMessage class is derived from CMessage - so it should work (at least
it does work that way in C++)

"Armin Zingler" <az*******@free net.de> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
"Richard Tappenden" <ri******@mailk ey.com> schrieb
The problem is that I cant seem to override the base class methods
unless they all use the base message class to do the operations -
i.e:

The base class method =
Public MustOverride Function MessageReceived (ByRef objMessage As
CMessage) As Boolean

My derived version
Public Overrides Function MessageReceived (ByRef objMessage As
CEmailMessage) As Boolean
' Do something
End Function.

The overridden version complains - stating that I still need to
override MessageReceived (ByRef objMessage CMessage) As Boolean.
The derived class must contain a procedure with the same signature. In

your case, the signature is different because the type of the argument is
CEmailMessage, not CMessage as in the base class.
In C++, you can do this - i.e. it resolves the virtual table
correctly.

For example - check these c++ files...(apolog ies if they don't look
nice, I knocked them up quickly to prove the point)

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
Richard,
..NET is not C++! Remember C++ also supports Multiple Inheritance where as
..NET does not.

C++ allows for covariant parameters, however .NET (the CLR itself) does not
allow for covariant parameters.

I understand that you can use Eiffel for .NET http://www.eiffel.com/ and
have covariant parameters, however you will not be able to use that code
with C# or VB.NET, as it is not CLS complient...

Hope this helps
Jay

"Richard Tappenden" <ri******@mailk ey.com> wrote in message
news:ui******** ******@TK2MSFTN GP11.phx.gbl...
Hi Armin.

Thanks for the reply - I understand what you are saying, however the
CEmailMessage class is derived from CMessage - so it should work (at least
it does work that way in C++)

<<snip>>
Nov 20 '05 #7
"Richard Tappenden" <ri******@mailk ey.com> schrieb

Thanks for the reply - I understand what you are saying, however
the CEmailMessage class is derived from CMessage - so it should work
(at least it does work that way in C++)


In .NET, the signatures must match exactly. I haven't thought about why they
have to (yet).
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #8
"Armin Zingler" <az*******@free net.de> schrieb
"Richard Tappenden" <ri******@mailk ey.com> schrieb

Thanks for the reply - I understand what you are saying, however
the CEmailMessage class is derived from CMessage - so it should
work (at least it does work that way in C++)


In .NET, the signatures must match exactly. I haven't thought about
why they have to (yet).


Now I did. ;-)

If I am the author of the base class, I want to force the author(s) of the
derived class(es) to accept *any* CMessage object. If you change the
signature to CEMailMessage, you break this enforcement because not any
CMessage object can be passed anymore.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9
That really defeats the point of OO programming - remember the CEmailMessage
is "a kind of" CMessage object - therefore I should be able to pass it into
the base class method - and it will do anything with it that it could with a
CMessage.

If I then implement a more complete, specific kind of message object and a
manager class that deals with them as well - it should be able to take
advantage of the new improved class, and yet still be "a kind of" base
object.

I must say, I'm pretty disappointed that this hasn't been put into .NET, and
I really think it will make a few things difficult when moving code over
from C++ to C#.

Incidently, in C++ I think you can enforce types by using the Explicit
keyword, which would restrict the types as per your idea below.
If I am the author of the base class, I want to force the author(s) of the
derived class(es) to accept *any* CMessage object. If you change the
signature to CEMailMessage, you break this enforcement because not any
CMessage object can be passed anymore.

Nov 20 '05 #10

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

Similar topics

37
2823
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 use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
18
12578
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 about whether we should allow polymorphism. Our system is not embedded and does not need to be as real-time as, say, a pacemaker. But it does involve updating displays based on radar input. So a need for something close to real-time is desired...
3
7437
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming http://en.wikipedia.org/wiki/Polymorphism http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?query=polymorphism&action=Search
11
5090
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
7389
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition of polymorphism, but he insisted on runtime. Did I miss a new buzzword while I was sick with the flu last week or something like that?
13
14608
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 Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
18
3849
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 different objects to respond to the same request message in their own unique way" I thought that it was: "the ability of same object to respond to different messages in
2
3383
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt whether it should not be used in certain cases. Consider the case when all the params to a template function/class are similar. My questions is that whatever can be acheived by a template in such a case, can be acheived by runtime polymorphism....
11
2963
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++, that anybody who doesn't use it might as well just program in plain C. I totally disagree with this, because I think C++ has a lot of great features apart from polymorphism, such as the ability to organize code into classes, code reuse through...
17
3867
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8494
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.