473,657 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A class structure question

Hello,

I have a problem trying to figure out the following design issue. I have a
base class and a number of classes derived from that base. A method in the
base class covers 90% of the functionality required for all classes, however
I need to have that 10% functionality that is left in the derived classes.
What I would like to do is have the method in the base class be able to call
the method in the derived classes, it should do this according to the actual
class that was instantiated.

For example: The base class is "bClass", and there are two derived classes
"Class1" and "Class2". The method "Test" in bClass needs to call
"OtherMetho d" in either Class1 or Class2. If I instantiate an object of type
Class1 and call "Test" I need it to call "OtherMetho d" in Class1. Can this
be done?

Thanks,
Sid.
Aug 12 '06 #1
9 1013
How about using the "MyClass" keyword?
"Sid Price" <si*@nowhere.co mwrote in message
news:uY******** ******@TK2MSFTN GP05.phx.gbl...
Hello,

I have a problem trying to figure out the following design issue. I have a
base class and a number of classes derived from that base. A method in the
base class covers 90% of the functionality required for all classes,
however I need to have that 10% functionality that is left in the derived
classes. What I would like to do is have the method in the base class be
able to call the method in the derived classes, it should do this
according to the actual class that was instantiated.

For example: The base class is "bClass", and there are two derived classes
"Class1" and "Class2". The method "Test" in bClass needs to call
"OtherMetho d" in either Class1 or Class2. If I instantiate an object of
type Class1 and call "Test" I need it to call "OtherMetho d" in Class1. Can
this be done?

Thanks,
Sid.


Aug 13 '06 #2

Sid Price wrote:
Hello,

I have a problem trying to figure out the following design issue. I have a
base class and a number of classes derived from that base. A method in the
base class covers 90% of the functionality required for all classes, however
I need to have that 10% functionality that is left in the derived classes.
What I would like to do is have the method in the base class be able to call
the method in the derived classes, it should do this according to the actual
class that was instantiated.

For example: The base class is "bClass", and there are two derived classes
"Class1" and "Class2". The method "Test" in bClass needs to call
"OtherMetho d" in either Class1 or Class2. If I instantiate an object of type
Class1 and call "Test" I need it to call "OtherMetho d" in Class1. Can this
be done?

Thanks,
Sid.
It almost sounds like you need to create an abstract class (MustInherit
in VB.NET). It would look something like:

Option Explicit On
Option Strict On

Imports System

Module Module1

Sub Main()
Dim bc1 As TheBaseClass = New ChildClass1
Dim bc2 As TheBaseClass = New ChildClass2

bc1.TheSubThatC allsTheMethod()
bc2.TheSubThatC allsTheMethod()

End Sub

Private MustInherit Class TheBaseClass
Protected MustOverride Sub TheAbstractMeth od()
Public Sub TheSubThatCalls TheMethod()
' do stuff
Me.TheAbstractM ethod()
'do more stuff
End Sub
End Class

Private Class ChildClass1
Inherits TheBaseClass

Protected Overrides Sub TheAbstractMeth od()
Console.WriteLi ne("Inside ChildClass1")
End Sub
End Class

Private Class ChildClass2
Inherits TheBaseClass

Protected Overrides Sub TheAbstractMeth od()
Console.WriteLi ne("Inside ChildClass2")
End Sub
End Class

End Module

HTH

--
Tom Shelton [MVP]

Aug 13 '06 #3
Price,

Reading your message I had the same idea of Tom about the mustInherit.
However at the end I had the idea that this would not be always needed, you
are only asking about the possibilitie to override (as Tom shows nicely) or
to shadow members. However without that mustInherit is Tom's sample as well
very good.

See for Shadows this, you use it the same as overriding but the base class
is than not any more used at all for that member (be aware by this at what
level you use it, how you use interfaces and how you cast).
http://msdn.microsoft.com/library/de...keyShadows.asp

I hope this helps,

Cor

"Sid Price" <si*@nowhere.co mschreef in bericht
news:uY******** ******@TK2MSFTN GP05.phx.gbl...
Hello,

I have a problem trying to figure out the following design issue. I have a
base class and a number of classes derived from that base. A method in the
base class covers 90% of the functionality required for all classes,
however I need to have that 10% functionality that is left in the derived
classes. What I would like to do is have the method in the base class be
able to call the method in the derived classes, it should do this
according to the actual class that was instantiated.

For example: The base class is "bClass", and there are two derived classes
"Class1" and "Class2". The method "Test" in bClass needs to call
"OtherMetho d" in either Class1 or Class2. If I instantiate an object of
type Class1 and call "Test" I need it to call "OtherMetho d" in Class1. Can
this be done?

Thanks,
Sid.


Aug 13 '06 #4
"Sid Price" <si*@nowhere.co mha scritto nel messaggio
news:uY******** ******@TK2MSFTN GP05.phx.gbl...
For example: The base class is "bClass", and there are two derived classes
"Class1" and "Class2". The method "Test" in bClass needs to call
"OtherMetho d" in either Class1 or Class2. If I instantiate an object of
type Class1 and call "Test" I need it to call "OtherMetho d" in Class1. Can
this be done?
Define OtherMethod as abstract in bClass (that should be named ClassBase and
not bClass).
In Class1 and Class2 if you need special behavior do an override of
OtherMethod.
--

Free .Net Reporting Tool - http://www.neodatatype.net
Aug 13 '06 #5

"Tom Shelton" <to*@mtogden.co mwrote in message
news:11******** **************@ 74g2000cwt.goog legroups.com...
It almost sounds like you need to create an abstract class (MustInherit
in VB.NET).
--
Tom Shelton [MVP]
That looks like it does exactley what I need, thank you Tom.
Sid.
Aug 14 '06 #6
Sid Price wrote:
I have a problem trying to figure out the following design issue. I have a
base class and a number of classes derived from that base. A method in the
base class covers 90% of the functionality required for all classes, however
I need to have that 10% functionality that is left in the derived classes.
What I would like to do is have the method in the base class be able to call
the method in the derived classes, it should do this according to the actual
class that was instantiated.

For example: The base class is "bClass", and there are two derived classes
"Class1" and "Class2". The method "Test" in bClass needs to call
"OtherMetho d" in either Class1 or Class2. If I instantiate an object of type
Class1 and call "Test" I need it to call "OtherMetho d" in Class1. Can this
be done?
Two ways:
(1) If you really want two methods ("Test" and "OtherMetho d"):

Class bClass
Public Sub Test()
' 90% of test Code
Me.OtherMethod( )
End Sub
Public Overridable Sub OtherMethod()
' Do Nothing
End Sub
End Class

Class Class1
Public Overrides Sub OtherMethod()
' Do the other 10%
End Sub
End Class

(2) Or, if you're not too bothered about having two methods, you can use
what I call "extending" (overriding, but then re-using the base class'
implementation) , as in

Class bClass
Public Overridable Sub Test()
' 90% of the job
End Sub
End Class

Class Class1
Public Overrides Sub Test()
MyBase.Test() ' 90%

' Now, do the other 10%
End Sub

End Class

HTH,
Phill W.
Aug 16 '06 #7
Two ways:
(1) If you really want two methods ("Test" and "OtherMetho d"):

Class bClass
Public Sub Test()
' 90% of test Code
Me.OtherMethod( )
End Sub
Public Overridable Sub OtherMethod()
' Do Nothing
End Sub
End Class
Having a method that does nothing is a bad design idea. It's like putting a
button on a remote control that does nothing.
(2) Or, if you're not too bothered about having two methods, you can use
what I call "extending" (overriding, but then re-using the base class'
implementation) , as in
Extending is what everyone else calls it as well.
Aug 16 '06 #8
Scott M. wrote:
>Two ways:
(1) If you really want two methods ("Test" and "OtherMetho d"):

Class bClass
Public Sub Test()
' 90% of test Code
Me.OtherMethod( )
End Sub
Public Overridable Sub OtherMethod()
' Do Nothing
End Sub
End Class

Having a method that does nothing is a bad design idea. It's like putting a
button on a remote control that does nothing.
In itself, it does nothing, but it provides a "hook" for a derived class
to do something else .. er .. instead.
>(2) Or, if you're not too bothered about having two methods, you can use
what I call "extending" (overriding, but then re-using the base class'
implementation ), as in

Extending is what everyone else calls it as well.
That's a relief. There's enough terminology around already without me
inventing any more of it ;-)

Regards,
Phill W.

Aug 17 '06 #9
>Having a method that does nothing is a bad design idea. It's like
>putting a button on a remote control that does nothing.

In itself, it does nothing, but it provides a "hook" for a derived class
to do something else .. er .. instead.
This is still a bad design interface. Since we don't know *if* a derived
class will even want to do *something* with this method, we are adding a
method for the sake of adding a method.
Aug 17 '06 #10

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

Similar topics

1
2081
by: Victor Hannak | last post by:
I have two classes derived from a base class. The two derived classes each utilize a structure that is slightly different from one another. i.e. DerivedClass1: struct NodeStruct { float NodeValue; ListStruct *NextNode; }
1
3261
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data structures (ints), and more complex data structures (strings and vectors) in it, and would like to write the entire class to a data file that could then be read back and loaded. However I'm having difficulty with this -- I found out (due to an...
21
4063
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class A { std::vector<B*> Bs; public:
3
3500
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust ------------------------------------------------------------------------
13
1552
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes. In the first I new an integer and assign it to i, in the second one I don't bother. In both cases, an integer is created and I can use it. If I try to use a Collection object without New, I get a NULL reference exception.
3
1806
by: Ren | last post by:
Hi all, I'm still rather new to .NET so I hope you'll bear with me as I try and explain my question. I am writing an ASP.NET application using VB.NET. I am accessing a web method from a webservice that returns a structure. On the client side I have added the webservice as a reference and created a class that contains the structure as a member as well as some other variables. The structure looks something like:
2
1189
by: Warex | last post by:
Hello, I have 2 questions maybe someone can answer. 1. On a function class when you return an item is it possible to return more than one and how is that done? Currently I am using for one Avariable = MyFunction() 2. In vb express is it possible to create an application that will allow a
4
2118
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
5
2725
by: cbmeeks | last post by:
Hello all. I have a project that I am working on and I need some suggestions. First, I have a class that contains a value and a reference to a parent class. For example: public class Data
2
1501
by: K Viltersten | last post by:
Suppose there is the following class structure. class S {...} class A : S {...} class B : S {...} class A1 : A {void m(){...}} class B1 : B {void m(){...}} class B2 : B {...} Just to clarify the issue. A1 and B1 share a method (void m() ) AND B2 doesn't have that
0
8402
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
8829
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...
0
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8508
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
7341
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...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
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();...
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.