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

Class inheritence & subclassing

Hi, I recently upgraded to VB.net from VB6.. and woah... I feel lost :¬O

One of my reasons for upgrading is I was told that VB.net can do class
inheritance and subclassing easier.

Would someone be so kind as to provide a small demo about classes for some
made-up info?

Im wanting something along the lines of:

classBox contains classBag
classBag contains classBulb

On being asked: classBulb.color

classBulb returns "Red"
On being asked: classBag.color

classBag would return "Black" unless classBulb is in it.

If classBulb is in classBag then classBag returns "Purple"

On being asked: classBox.color

classBox would return "Black" if nothing was in it

classBox would return "Pink" is classBulb was in it

classBox would return "Brown" if classBag was returning "Purple" when asked.

Thats a total of 3 "objects" in their own classes and showing class
interaction / modification.

If someone could make a short demo piece for VB.net around that then I would
be very grateful... or even if someone could just point me to a small
tutorial on the internet about it?

Thank you for taking the time to read my post :¬)

--
Trammel is a member of DWC (http://dwc.no-ip.org)
(Please reply to group only)
Nov 21 '05 #1
3 1920
Hi,

I don't think inheritance will let you achieve your desired result

the inherited objects would look something like the following

Public Class ClassBox
Inherits ClassBag

Private m_Color As Color = Color.Purple

Public Overrides Property Color() As Color
Get
Return m_Color
End Get
Set(ByVal Value As Color)
m_Color = Value
End Set
End Property

End Class

Public Class ClassBag
Inherits ClassBulb

Private m_Color As Color = Color.Black

Public Overrides Property Color() As Color
Get
Return m_Color
End Get
Set(ByVal Value As Color)
m_Color = Value
End Set
End Property

End Class

Public Class ClassBulb

Private m_Color As Color = Color.Red

Public Overridable Property Color() As Color
Get
Return m_Color
End Get
Set(ByVal Value As Color)
m_Color = Value
End Set
End Property

End Class

If you declare an instance of ClassBox it is also a ClassBag and a
ClassBulb is does not contain a copy of these. ClassBox and ClassBag
simply override some of the functionality of their parent objects.

The best way i can describe this is with the Animal, Dog,
GermanShepherd scenario.

Public Class Animal

Public Property IsAlive As Boolean
Get
Return True
End Get
End Property

Public Overridable ReadOnly Property Color
Get

End Get
End Property

End Class

Public Class Dog
Inherits Animal

Public Property HasFourLegs As Boolean
Get
Return True
End Get
End Property

End Class

Public Class GermanShepherd
Inherits Dog

Public Overrides ReadOnly Property Color
Get
Return Color.Black
End Get
End Property

End Class

If you declare an object of type GermanShepherd it also contains all
of the methods and properties of a Dog and an Animal because a
GermanShepherd is a Dog and a Dog is an animal.

An object of type GermanShepherd would contain the following
properties

IsAlive
Color
HasfourLegs

I Hope this has been of some help

Regards

James Thresher

On Wed, 15 Jun 2005 18:07:32 GMT, "Trammel" <Me@Server.com> wrote:
Hi, I recently upgraded to VB.net from VB6.. and woah... I feel lost :¬O

One of my reasons for upgrading is I was told that VB.net can do class
inheritance and subclassing easier.

Would someone be so kind as to provide a small demo about classes for some
made-up info?

Im wanting something along the lines of:

classBox contains classBag
classBag contains classBulb

On being asked: classBulb.color

classBulb returns "Red"
On being asked: classBag.color

classBag would return "Black" unless classBulb is in it.

If classBulb is in classBag then classBag returns "Purple"

On being asked: classBox.color

classBox would return "Black" if nothing was in it

classBox would return "Pink" is classBulb was in it

classBox would return "Brown" if classBag was returning "Purple" when asked.

Thats a total of 3 "objects" in their own classes and showing class
interaction / modification.

If someone could make a short demo piece for VB.net around that then I would
be very grateful... or even if someone could just point me to a small
tutorial on the internet about it?

Thank you for taking the time to read my post :¬)


Nov 21 '05 #2
Inheritance / interfaces / design patterns can be applied to this
problem but would need to be approached differently than above.

From the description you seem to have a set of items; some of which can

can contain other items

Box => Container
Bag => Container
Bulb => Non-container
All of these items have a color, and the apparent color of a container
depends upon the contents (if any).

This is a pretty good fit for the Composite Design Pattern (see notes
below) and can be implemented in VB.Net using both interfaces and
inheritance.
1) IColoredComponent (see code below)

Provides a common interface for accessing containers and objects.
2) ColoredContainer

Implementation of a container object. All containers are black when
empty and this implementation assumes that the apparent color
combinations do
not change from container to container (e.g. if a bag contains a red
object then it appears purple, and if a box contains a red object then
it also appears purple)

NOTE: This makes it simpler for the example (we only need one class for
box and bag) but not required. We could derive box and bag classes
from this class and override the MaskColor() function in each.
3) ColoredObject

Implementation of an on-container object. We will derive all objects
from this.
NOTE: The Contents() property is not relevant for this but needs to be
implemented as it is part of the Interface - this is a known trade off
with the composite pattern
4) Bulb

Inherits from ColoredObject and is red.

COMPOSITE DESIGN PATTERN

A Design Pattern is a shape that can be applied to many recurring
problems. The seminal book about Patterns is the Gamma one (see
references) but there are lots of books out there about the topic.

The Composite Pattern is used where we have items that can be
stand-alone or can be made up of other similar items. 'Made up of' can
be extended to mean 'contain' - so it can be applied to this problem.

PROBLEMS:

This is a lot of work for just the colors.

Presumably you will have lots of other functionality for the items and
you may want to use the containers and non-containers interchangeabley
in some context (e.g. list the contents of a room).

The code as is useful as an example for this small problem but would
need a lot of changes if this is only part of a problem domain.
Those changes are totally dependent upon the problem and out of the
range of a small answer.

LEARNING:

If you are new to VB.Net and OO and want to make use of all of VB.Net's
abilities (and not just write VB6 using the VB.Net compiler) then I'm
afraid that you have a lot of reading ahead of you.

The Design Patterns book is a must-have for OO developers - problem is
it not a good book to start on.
The Design Patterns in VB.Net is more accessible that the Gamma book
and is pretty good, but is aimed at intermediate to advanced levels.
All my early OO learning was in C++ so I don't know any good starter
VB.Net books that cover OO design well.

Try a search through this NG for book recommendations but focus on ones
trying to teach OO concepts. There are myriad books covering how to
draw controls on screens and read/write from databases using ADO.NET
and ... all the mechanical tasks, but fewer that talk about good ways
to design applications.
Alan.

REFERENCES

"Design Patterns: Elements of Reusable Object-Oriented Software",
Gamma et al. ISBN 0-201-63361-2
"Professional Design Patterns in VB.Net: Building Adaptable
Applications", Fischer et al. ISBN 1-59059-274-3

SOURCE CODE

'-------------------------------------------------
' Application class

Class App
Shared Sub Main()

Dim aBulb As New Bulb
Dim aBag As New ColoredContainer
Dim aBox As New ColoredContainer
System.Console.WriteLine("The bulb is " + aBulb.Color.ToString)
System.Console.WriteLine("The Empty bag is " +
aBag.Color.ToString)
System.Console.WriteLine("The Empty box is " +
aBox.Color.ToString)

aBox.Contents = aBag
System.Console.WriteLine("The Box containing an empty bag is "
+ aBox.Color.ToString)

aBag.Contents = aBulb
System.Console.WriteLine("The Bag containing a bulb is " +
aBag.Color.ToString)
System.Console.WriteLine("The Box containing a bag containing a
bulb is " + aBox.Color.ToString)
System.Console.ReadLine() ' pauses the display, press enter
to continue
End Sub
End Class
'-------------------------------------------------
' IColoredComponent
Public Interface IColoredComponent

ReadOnly Property Color() As System.Drawing.Color
Property Contents() As IColoredComponent

End Interface

'-------------------------------------------------
' ColoredContainer

Public Class ColoredContainer
Implements IColoredComponent

Private Const DEFAULT_COLOR As System.Drawing.KnownColor =
System.Drawing.KnownColor.Black

Public Sub New()
_color = Color.FromKnownColor(DEFAULT_COLOR)
_colorCombinations = GetColorCombinations()
End Sub

#region "IColoredComponent Implementation"

Public ReadOnly Property Color() As System.Drawing.Color Implements
IColoredComponent.Color
Get
Dim ret As System.Drawing.Color = _color
If (Not Contents Is Nothing) Then
ret = MaskColor(Contents)
End If
Return ret

End Get
End Property

Public Property Contents() As IColoredComponent Implements
IColoredComponent.Contents
Get
Return _contents
End Get
Set(ByVal Value As IColoredComponent)
_contents = Value
End Set
End Property

#end region

Protected Overridable Function MaskColor( _
ByVal contents As IColoredComponent _
) As System.Drawing.Color

Dim ret As System.Drawing.Color = contents.Color
If (_colorCombinations.Contains(contents.Color)) Then
ret = CType(_colorCombinations(contents.Color),
System.Drawing.Color)
End If
Return ret

End Function
Private Function GetColorCombinations() As
System.Collections.Hashtable

Dim ret As New System.Collections.Hashtable

ret.Add(Color.Red, Color.Purple)
ret.Add(Color.Black, Color.Pink)
ret.Add(Color.Purple, Color.Brown)

Return ret

End Function
Private _color As System.Drawing.Color
Private _contents As IColoredComponent
Private _colorCombinations As System.Collections.Hashtable

End Class

'-------------------------------------------------
' ColoredObject

Public Class ColoredObject
Implements IColoredComponent
Private Const DEFAULT_COLOR As System.Drawing.KnownColor =
System.Drawing.KnownColor.Black

Public Overridable ReadOnly Property Color() As
System.Drawing.Color Implements IColoredComponent.Color
Get
Return System.Drawing.Color.FromKnownColor(DEFAULT_COLOR)
End Get
End Property

Public Property Contents() As IColoredComponent Implements
IColoredComponent.Contents
Get
Debug.Assert(False, "This does nothing in the ColoredObject
class")
End Get
Set(ByVal Value As IColoredComponent)
Debug.Assert(False, "This does nothing in the ColoredObject
class")
End Set
End Property
End Class

'-------------------------------------------------
' Bulb

Public Class Bulb
Inherits ColoredObject

Private Const DEFAULT_COLOR As System.Drawing.KnownColor =
System.Drawing.KnownColor.Red
Public Overrides ReadOnly Property Color() As System.Drawing.Color
Get
Return System.Drawing.Color.FromKnownColor(DEFAULT_COLOR)
End Get
End Property

End Class

Nov 21 '05 #3
Both of your examples look good and have helped me get a grounding in class
inheretence, etc.

I'd like to thank you both for your valuable time and feedback to my problem
:¬)
*goes back to lurking as a slightly less perplexed happy-bunny*

--
Trammel is a member of DWC (http://dwc.no-ip.org)
(Please reply to group only)
Nov 21 '05 #4

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

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
17
by: Dave | last post by:
Hi I'm making a 3D Engine which consists of the class C3DEngine. Part of this engine is a file loader, a class called CMeshLoader. I have made an instance of CMeshLoader in C3DEngine, ie...
1
by: ik | last post by:
Hello All, Please point me to the right news group if this is not the right one. I have to store c++ class hierarchy, in some structure. From that structure, I need to query for the baseclasses....
4
by: Slavyan | last post by:
(I just started to learn C#.NET) What's the syntax in c# for a class to inherit more than one class. I know following syntax: public class MyClass : MyOtherClass { } but I need to inherit...
3
by: DC Gringo | last post by:
I have an image control (that pulls an image off an ESRI map server): <ASP:IMAGE ID="imgZonedCountry" RUNAT="server"></ASP:IMAGE> In the code behind I am setting the ImageURL to a String value...
7
by: Steven D'Aprano | last post by:
I'm having problems with sub-classes of built-in types. Here is a contrived example of my subclass. It isn't supposed to be practical, useful code, but it illustrates my problem. class...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
1
by: =?ISO-8859-1?Q?Ricardo_Ar=E1oz?= | last post by:
That is self.__attributes Been reading about the reasons to introduce them and am a little concerned. As far as I understand it if you have a class that inherits from two other classes which...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.