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

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
"OtherMethod" in either Class1 or Class2. If I instantiate an object of type
Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can this
be done?

Thanks,
Sid.
Aug 12 '06 #1
9 996
How about using the "MyClass" keyword?
"Sid Price" <si*@nowhere.comwrote in message
news:uY**************@TK2MSFTNGP05.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
"OtherMethod" in either Class1 or Class2. If I instantiate an object of
type Class1 and call "Test" I need it to call "OtherMethod" 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
"OtherMethod" in either Class1 or Class2. If I instantiate an object of type
Class1 and call "Test" I need it to call "OtherMethod" 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.TheSubThatCallsTheMethod()
bc2.TheSubThatCallsTheMethod()

End Sub

Private MustInherit Class TheBaseClass
Protected MustOverride Sub TheAbstractMethod()
Public Sub TheSubThatCallsTheMethod()
' do stuff
Me.TheAbstractMethod()
'do more stuff
End Sub
End Class

Private Class ChildClass1
Inherits TheBaseClass

Protected Overrides Sub TheAbstractMethod()
Console.WriteLine("Inside ChildClass1")
End Sub
End Class

Private Class ChildClass2
Inherits TheBaseClass

Protected Overrides Sub TheAbstractMethod()
Console.WriteLine("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.comschreef in bericht
news:uY**************@TK2MSFTNGP05.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
"OtherMethod" in either Class1 or Class2. If I instantiate an object of
type Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can
this be done?

Thanks,
Sid.


Aug 13 '06 #4
"Sid Price" <si*@nowhere.comha scritto nel messaggio
news:uY**************@TK2MSFTNGP05.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
"OtherMethod" in either Class1 or Class2. If I instantiate an object of
type Class1 and call "Test" I need it to call "OtherMethod" 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.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.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
"OtherMethod" in either Class1 or Class2. If I instantiate an object of type
Class1 and call "Test" I need it to call "OtherMethod" in Class1. Can this
be done?
Two ways:
(1) If you really want two methods ("Test" and "OtherMethod"):

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 "OtherMethod"):

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 "OtherMethod"):

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
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...
1
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...
21
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...
3
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
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....
3
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...
2
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...
4
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
5
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
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...
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
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
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,...
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
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.