Question Related to Inheritance( multiple)

  • Thread starter Thread starter Kishor
  • Start date Start date
K

Kishor

Hi,

I am looking for the solution to my problem. this is related to inheritance
(Inheriting properties of base class in multiple quantity) . I am writing
code in VB.net language, I am having two classes clsVehicle and clsWheel.
clsWheel has two properties diameter and pressure. clsVehicle has two
properties Name and Make. I want to implement these classes this way



create a object of Vehicle class.

dim Ovehicle as New clsVehicle



Now I want to have a statements like this

implementation code.

Ovehicle.name = "VehicleName"

Ovehicle.make= "VehicleMake"

Ovehicle.wheels.count = 4

Ovehicle.wheel(1).diameter = "100"

Ovehicle.wheel(2).diameter = "100"

Ovehicle.wheel(3).diameter = "100"

Ovehicle.wheel(4).diameter = "100"



What are the properties and other code has to be implemented in these
classes so that I can have all these above statements valid. Can any one
help me writing this code. I am here pasting the code of the class. I have
tried using parameterized properties but I failed. I tried using this
statement inherits clsWheel in cls Vehicle. I also tried creating instance
of clswheel in clsVehicle dim owheel() as clswheel.



Please guide me to achieve this





Public Class clsVehicle

Private mname As String

Private mmake As String





Public Property name() As String

Get

Return mname

End Get

Set(ByVal Value As String)

mname = Value

End Set

End Property

Public Property Make() As String

Get

Return mmake

End Get

Set(ByVal Value As String)

mmake = Value

End Set

End Property

End Class

Public Class clsWheels

Private mDiam As String

Private mpressure As String

Public Property Diameter() As String

Get

Return mDiam

End Get

Set(ByVal Value As String)

mDiam = Value

End Set

End Property

Public Property pressure() As String

Get

Return mpressure

End Get

Set(ByVal Value As String)

mpressure = Value

End Set

End Property

End Class





Tia,

Kishor
 
I don't think that this is inheritance, more of a oo question. anyway heres
wat i do (i do put seperate classes in seperate files so i dont know if this
would give compilation order problems)

in your clsvehicle declare

private wheel1 as new clsWheel

and make a property for it

Public Property wheel() As clsWheel
Get
Return wheel1
End Get
Set(ByVal Value As clsWheel)
wheel1 = Value
End Set
End Property

then you get a Ovehicle.wheel.diameter ...

havent done this w arrays but i think it could be as simple as declaring the
weel1 as an array

hope it helps

eric
 
Kishor said:
Hi,

I am looking for the solution to my problem. this is related to
inheritance (Inheriting properties of base class in multiple
quantity) . I am writing code in VB.net language, I am having two
classes clsVehicle and clsWheel. clsWheel has two properties diameter
and pressure. clsVehicle has two properties Name and Make. I want to
implement these classes this way

Untested, but it should work: (note: wheel index is zero-based)


Dim Ovehicle As New clsVehicle

Ovehicle.name = "VehicleName"
Ovehicle.Make = "VehicleMake"
Ovehicle.Wheels.Count = 4
Ovehicle.Wheels(0).Diameter = "100"
Ovehicle.Wheels(1).Diameter = "100"
Ovehicle.Wheels(2).Diameter = "100"
Ovehicle.Wheels(3).Diameter = "100"



Public Class clsVehicle
Private mname As String
Private mmake As String
Public ReadOnly Wheels As New clsWheels


Public Property name() As String
Get
Return mname
End Get
Set(ByVal Value As String)
mname = Value
End Set
End Property
Public Property Make() As String
Get
Return mmake
End Get
Set(ByVal Value As String)
mmake = Value
End Set
End Property
End Class

Public Class clsWheels
Private mItems As New ArrayList
Default Public ReadOnly Property Item( _
ByVal Index As Integer) As clsWheel

Get
Return DirectCast(mItems(Index), clsWheel)
End Get
End Property
Public Property Count() As Integer
Get
Return mItems.Count
End Get
Set(ByVal Value As Integer)
If Value = mItems.Count Then Return
If Value < mItems.Count Then
mItems.RemoveRange(Value, mItems.Count - Value)
Else
Dim i As Integer
For i = mItems.Count + 1 To Value
mItems.Add(New clsWheel)
Next
End If
End Set
End Property

End Class

Public Class clsWheel
Private mDiam As String
Private mpressure As String
Public Property Diameter() As String
Get
Return mDiam
End Get
Set(ByVal Value As String)
mDiam = Value
End Set
End Property
Public Property pressure() As String
Get
Return mpressure
End Get
Set(ByVal Value As String)
mpressure = Value
End Set
End Property
End Class


BTW, the question is not about inheritance at all. :-)
 
hi,
Thanks for your immediate reply. I have tried this also. as per your code I
wll get only one object at a time. If I am declaring it as a arry
Your Statement
private wheel1 as new clsWheel
become like this
private wheel1() as new clsWheel
but this is not allowed in vb.net. to do this we have to change code this
way....
private wheel1 As New Hashtable() and the moment you write this it adds
overhead of creating and destroying the objects manually. I want to avoid
this. is there any way which will allow me to achieve this what I said in
first mail.

Tia.
Kishor
 
Kishor,
A variation of Armin's sample would be to derive clsWheels (which I will
call WheelCollection) from CollectionBase. Also I will use Wheel instead of
clsWheel.

Public Class WheelCollection
Inherits CollectionBase

Public Sub Add(ByVal wheel As Wheel)
InnerList.Add(wheel)
End Sub

Default Public ReadOnly Property Item(ByVal index As Integer) As
Wheel
Get
Return DirectCast(InnerList(index), Wheel)
End Get
End Property

End Class

Public Class Vehicle

Public ReadOnly Wheels As New WheelCollection

Public Property Name() As String
Get

End Get
Set(ByVal Value As String)

End Set
End Property

Public Property Make() As String
Get

End Get
Set(ByVal Value As String)

End Set
End Property

End Class

Public Class Wheel

Private m_diameter As Integer

Public Sub New(ByVal diameter As Integer)
m_diameter = diameter
End Sub

Public Property Diameter() As Integer
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

End Class

Public Module MainModule

Public Sub Main()
Dim aVehicle As New Vehicle
aVehicle.name = "VehicleName"
aVehicle.make = "VehicleMake"
aVehicle.Wheels.Add(New Wheel(100))
aVehicle.Wheels.Add(New Wheel(100))
aVehicle.Wheels.Add(New Wheel(100))
aVehicle.Wheels.Add(New Wheel(100))
End Sub

End Module

I changed to names to be more in line with the Framework and the .NET
Developers Guidelines.

http://msdn.microsoft.com/library/d...ef/html/cpconNETFrameworkDesignGuidelines.asp

The CollectionBase wraps an ArrayList for you, so you only need to add your
type safe methods.

Hope this helps
Jay
 
hi,
Thanks It is working ......

Kishor
Jay B. Harlow said:
Kishor,
A variation of Armin's sample would be to derive clsWheels (which I will
call WheelCollection) from CollectionBase. Also I will use Wheel instead of
clsWheel.

Public Class WheelCollection
Inherits CollectionBase

Public Sub Add(ByVal wheel As Wheel)
InnerList.Add(wheel)
End Sub

Default Public ReadOnly Property Item(ByVal index As Integer) As
Wheel
Get
Return DirectCast(InnerList(index), Wheel)
End Get
End Property

End Class

Public Class Vehicle

Public ReadOnly Wheels As New WheelCollection

Public Property Name() As String
Get

End Get
Set(ByVal Value As String)

End Set
End Property

Public Property Make() As String
Get

End Get
Set(ByVal Value As String)

End Set
End Property

End Class

Public Class Wheel

Private m_diameter As Integer

Public Sub New(ByVal diameter As Integer)
m_diameter = diameter
End Sub

Public Property Diameter() As Integer
Get
Return m_diameter
End Get
Set(ByVal value As Integer)
m_diameter = value
End Set
End Property

End Class

Public Module MainModule

Public Sub Main()
Dim aVehicle As New Vehicle
aVehicle.name = "VehicleName"
aVehicle.make = "VehicleMake"
aVehicle.Wheels.Add(New Wheel(100))
aVehicle.Wheels.Add(New Wheel(100))
aVehicle.Wheels.Add(New Wheel(100))
aVehicle.Wheels.Add(New Wheel(100))
End Sub

End Module

I changed to names to be more in line with the Framework and the .NET
Developers Guidelines.

http://msdn.microsoft.com/library/d...ef/html/cpconNETFrameworkDesignGuidelines.asp

The CollectionBase wraps an ArrayList for you, so you only need to add your
type safe methods.

Hope this helps
Jay
 
Is there a way to design the class WheelCollection as a general class and
instantiate it for a particular type?

I have multiple objects type to store this way and I dont like to rewrite a
colection class for each type.

Thanks,
Crirus
 
Back
Top