Solved

Help with Classes

Posted on 2000-02-24
3
165 Views
Last Modified: 2013-11-23
I need some help in create classes. Specifically, I need to know how to create classes with subclass. Two issues:

1. A class is a sub type of another class. such as  A text box is a type of Control. or A Car is a type of Automobile.
For example, is I create a object called myCar  I need to be able to do
For Each Car...Next and For Each Automobile.. Next.,  I would see myCar in both loops


2. A class has an array of another class. For exaple, ABuilding have 25 rooms and a room can have  several seats.

Thanks for help


0
Comment
Question by:GeorgeVT
3 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2555631
Inheritance is not allowed as defined in C/C++/Java and similar object oriented languages (maybe in VB7 but that's not a solution). What you can do is define a property in the class with a type of a class such as

Private m_Automobile As New clsAutomobile

  Public Property Let Automobile(New_Automobile as clsAutomobile) As clsAutomobile

    Set m_Automobile = New_Automobile

  End Property

  Public Property Get Automobile() As clsAutomobile

    Set Automobile = m_Automobile

  End Property

This will give you access to properties that are similar to all objects that inherit the properties a class of type Automobile has with code like

  carname.Automobile.Cylinders = 7


Arrays can be stored in properties with property definitions like

  Private m_Seats() As New clsSeat

  Public Property Let SeatCount(New_SeatCount As Long)

    Redim m_Seats(New_SeatCount-1)

  End Property

  Public Property Let Seat(Index As Long, New_Seat As clsSeat)

    Set m_Seats(Index-1) = New_Seat

  End Property

  Public Property Get Seat(Index As Long) As clsSeat

    Set Seat = m_Seats(Index-1)

  End Property


Hope this helps...
0
 
LVL 9

Accepted Solution

by:
samopal earned 400 total points
ID: 2555687
Look at this example.

(Forest) is a collection of (Tree)
(Tree) has [name] and a collection of (Leaf)
(Leaf) has [color] and [size]

Class Leaf
==========
Private mvarColor As String
Private mvarSize As Long
Public Property Let Size(ByVal vData As Long)
    mvarSize = vData
End Property
Public Property Get Size() As Long
    Size = mvarSize
End Property
Public Property Let Color(ByVal vData As String)
    mvarColor = vData
End Property
Public Property Get Color() As String
    Color = mvarColor
End Property

Class Leafs
===========
Private mCol As Collection

Public Function Add(Color As String, Size As Long) As Leaf
    Dim objNewMember As New Leaf
    objNewMember.Color = Color
    objNewMember.Size = Size
    mCol.Add objNewMember
'return the object created
    Set Add = objNewMember
    Set objNewMember = Nothing
End Function
Public Property Get Item(vntIndexKey As Variant) As Leaf
  Set Item = mCol(vntIndexKey)
End Property
Public Property Get Count() As Long
    Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
    mCol.Remove vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
    'this property allows you to enumerate
    'this collection with the For...Each syntax
    Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
    'creates the collection when this class is created
    Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
    'destroys collection when this class is terminated
    Set mCol = Nothing
End Sub

Class Tree
==========
Private mvarsTree As String
Private mvarLeafs As Leafs
Public Property Get Leafs() As Leafs
    If mvarLeafs Is Nothing Then
        Set mvarLeafs = New Leafs
    End If
    Set Leafs = mvarLeafs
End Property
Public Property Set Leafs(vData As Leafs)
    Set mvarLeafs = vData
End Property
Private Sub Class_Terminate()
    Set mvarLeafs = Nothing
End Sub
Public Property Let sTree(ByVal vData As String)
    mvarsTree = vData
End Property
Public Property Get sTree() As String
    sTree = mvarsTree
End Property

Class Forest
============
Private mCol As Collection
Public Function Add(sTree As String) As Tree
    Dim objNewMember As New Tree
'set the properties passed into the method
    objNewMember.sTree = sTree
    Set objNewMember.Leafs = New Leafs
    mCol.Add objNewMember
'return the object created
    Set Add = objNewMember
    Set objNewMember = Nothing
End Function
Public Property Get Item(vntIndexKey As Variant) As Tree
  Set Item = mCol(vntIndexKey)
End Property
Public Property Get Count() As Long
    Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
    mCol.Remove vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
    'this property allows you to enumerate
    'this collection with the For...Each syntax
    Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
    'creates the collection when this class is created
    Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
    Set mCol = Nothing
End Sub

In the Form1 add CommandButton

Option Explicit
Dim mLeaf As Leaf
Dim mTree As Tree
Dim Park As New Forest

Private Sub Form_Load()
    Set mTree = Park.Add("Fir-tree")
    mTree.Leafs.Add "yellow", 1
    mTree.Leafs.Add "red", 8
    mTree.Leafs.Add "yellow", 7
   
    Set mTree = Park.Add("Pine-tree")
    mTree.Leafs.Add "green", 1
    mTree.Leafs.Add "green", 1
    mTree.Leafs.Add "green", 1
    mTree.Leafs.Add "green", 1
End Sub

Private Sub Command1_Click()
'count all leafs in the forest
    For Each mTree In Park
        For Each mLeaf In mTree.Leafs
Debug.Print mTree.sTree & "  " & mLeaf.Color & "  " & mLeaf.Size
        Next
    Next
End Sub



HTH
0
 
LVL 18

Expert Comment

by:mdougan
ID: 2555696
Let's say that you have a class called Automobile, and you have two other classes called Sportscar and Luxurycar.

In the general declarations for both Sportscar and Luxurycar you'd have the statement:

Implements Automobile

Then, each of these two classes will have access to the properties defined in Automobile.  You may wish to create Get and Let procedures to access the private member variables of the Automobile class.  Check the MSDN help on Implements for a sample of this.

In your class, you can have a member variable that is an array of another class.  Say that my class name is called DeckOfCards in it's general declarations section it might have:

Private mCard(1 to 52) as Card

where Card is another VB Class.  When referencing this inside of the code for DeckOfCards you'd access the properties of Card like:

for i = 1 to 52
  mCard(i).Value = 1
  mCard(i).Suit = "Spades"
next i

If you made mCard a public member variable of DeckOfCards, then code would reference like so:

Dim x as new DeckOfCards

for i = 1 to 52
  x.mCard(i).Value = 1
  x.mCard(i).Suit = "Spades"
next i
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now