PDA

View Full Version : Basic Class Modules



Jfp87
08-20-2015, 10:27 AM
Guys,

I decided to create a small project to help me learn & understand class modules. The project is based around a group of dog agility clubs (girlfriend is a member of one), here is the rough structure so far:

'Club' object class:
Properties - 'Club Name', 'Club Rank', 'Owners' (each club should have it's own owners collection)
Methods - 'Add Owner'

'Clubs' collection class:
Properties - 'Count' & 'Item'
Methods - 'Add'

'Owner' object class:
Properties - 'Owner Name' & 'Owner Age'

'Owners' collection class:
Properties - 'Count' & 'Item' (for referring to individual 'Owner' objects)
Methods - 'Add'

Basically, I have looked at this for so long that I don't know what I am doing anymore.

Each club object should have an 'Owners' property which points to a collection based on the 'Owners' collection class. It's quite easy to see the relationships I am trying to create, but I am finding it more difficult to code. It's the relationships between the classes which is confusing me a bit.

If anyone can shed light on this I would appreciate it.

Thanks,
J

SamT
08-20-2015, 11:57 AM
Have you perfected the Owner Class yet?To test it, you should be able to Set a Variable to the class and manipulate it in VBA using only VBA Objects


Sub Test_clsOwner()
Dim Owner as Object
Dim MyCollection As Collection
Set Owner = New clsOwner

With Owner
.Name = "John Smith"
.Birthday = "1-jan-50" 'Should accept any valid Date format, return value should be in Local System Short Date Format.
End With

X = Owner.Age. 'should return 65

MyCollection.Add Owner

X = MyCollection(1).Name 'Should Return "John Smith"

End sub


Simple Class Module Example. Code goes in Class Module "clsOwner"

Option Explicit

' "m" prefix = Module level variable
' "mp" Prefix = Module level Procedure: Function or Sub
'Prefixes allows the use of the same meaningful names to be used for different purposes

Private mName As String
Private mBirthday As Date
Private mDogs 'As clsDog 'Require clsDog exist before use

Private Function mpAge() As Integer
mAge = DateDiff("yyyy", Now, mBirthday)
End Function

'Name is Read/Write
Property Get Name() As String
Name = mName
End Property

Property Let Name(mName As String)
Dim Temp As String
Temp = Name
'code to verify Name String looks like a real name
mName = Temp
End Property

'Age Is Read Only
Property Get Age() As Integer
Age = mpAge 'calls private function
End Property

Property Get Dogs() As Object

End Property

Property Set Dogs(mDogs As Object)
'Note Property SET, not Property LET
End Property

SamT
08-20-2015, 12:31 PM
Have you completed your Object Model yet?

Example Object Model:


Objects

Properties
Methods
Events
Notes


Clubs

Count
Add






Item






Remove











Club
Name






Address






Phone






Owners






Dogs


all dogs of all owners



Events


shows, parties, elections









Events
Count
Add





Dates
Item

Dates = Calculated Array




Remove




Event
Name






Location






Date






Type












Owners
Count
Add






Item






Remove











Owner
Name






Address






Phone






Dogs












Dogs
Count
Add





TotalWins
Item





totalshows
Remove





TotalPlaces












Dog
Name






Birthday






Age


If Birthday = "" then Calculate Birthday



Breed






Events


As dog Event



Wins






Places






Shows












DogEvent
Name






Date






Type






Standing

gmaxey
08-20-2015, 02:53 PM
You might find this helpful if not amusing:

http://gregmaxey.mvps.org/word_tip_pages/class_study_hangman_for_word.html

Jfp87
08-21-2015, 07:34 AM
Thank guys,

Both of your posts have helped me along in my understanding.

Cheers