0

I am studing VBA object module and trying to use it as an easy way of retriave objects properties.

I write the code below and would really appreciate if you could tell me if there is a better way of doing it, or if I am in the right path for write some good code.

Consider that I intend to use this on a bigger project, so I need to know if it is well written.

Class Module Animals

Option Explicit

Private pCat As New Animal
Private pDog As New Animal
Private pFish As New Animal

Private Sub Class_Initialize()
  pCat.Mammal = True
  pCat.NumberOfPaws = 4
  
  pDog.Mammal = True
  pDog.NumberOfPaws = 4
  
  pFish.Mammal = False
  pFish.NumberOfPaws = 0
End Sub

Property Get Cat() As Animal
  Set Cat = pCat
End Property

Property Get Dog() As Animal
  Set Dog = pDog
End Property

Property Get Fish() As Animal
  Set Fish = pFish
End Property

Class Module Animal

Option Explicit

Private nPaws As Integer
Private pMammal As String

Property Get NumberOfPaws() As Integer
  NumberOfPaws = nPaws
End Property

Property Let NumberOfPaws(numPaws As Integer)
  nPaws = numPaws
End Property

Property Get Mammal() As Boolean
  Mammal = pMammal
End Property

Property Let Mammal(IsMammal As Boolean)
  pMammal = IsMammal
End Property

Subroutine for testing the object modeules above

Option Explicit

Sub Test()

  Dim objAnimal As New Animals
  
  Debug.Print objAnimal.Cat.Mammal
  Debug.Print objAnimal.Cat.NumberOfPaws
  
  Debug.Print objAnimal.Dog.Mammal
  Debug.Print objAnimal.Dog.NumberOfPaws
  
  Debug.Print objAnimal.Fish.Mammal
  Debug.Print objAnimal.Fish.NumberOfPaws

End Sub
naguall
  • 43
  • 1
  • 8

1 Answers1

0

If you plan to use a large number of Animal objects, it's wise not to create a new container for them as a new object, but to use a built-in one, like Collection, which has list and dictionary properties. Also you can use a Dictionary object.

It also makes sense to add the Name property to the Animal object to distinguish one object from another in the list. Additionally: you can make it convenient to create objects (like https://stackoverflow.com/a/15224115/15035314), as well as to display information about the object (see the procedure PrintMe()).

Animal class module

Option Explicit

Private mName As String
Private nPaws As Integer
Private pMammal As Boolean

Public Sub InitiateProperties(Name As String, Paws As Integer, Mammal As Boolean)
    mName = Name
    nPaws = Paws
    pMammal = Mammal
End Sub

Public Sub PrintMe()
    Debug.Print "The animal is " & mName & ", it has " & nPaws & " paws(s) and is" & IIf(pMammal, " ", " not ") & "a mammal"
End Sub

Property Get Name() As Integer
  Name = mName
End Property

Property Let Name(theName As Integer)
  mName = theName
End Property

Property Get NumberOfPaws() As Integer
  NumberOfPaws = nPaws
End Property

Property Let NumberOfPaws(numPaws As Integer)
  nPaws = numPaws
End Property

Property Get Mammal() As Boolean
  Mammal = pMammal
End Property

Property Let Mammal(IsMammal As Boolean)
  pMammal = IsMammal
End Property

Module1 common module

Option Explicit

' based on https://stackoverflow.com/a/15224115/15035314
Public Function CreateAnimal(Name As String, Paws As Integer, Mammal As Boolean) As Animal
    Set CreateAnimal = New Animal
    CreateAnimal.InitiateProperties Name, Paws, Mammal
End Function

Sub test()
    Dim Animals As New Collection, A As Animal
    
    Animals.Add CreateAnimal("Cat", 4, True), "Cat"
    Animals.Add CreateAnimal("Dog", 4, True), "Dog"
    Animals.Add CreateAnimal("Fish", 0, False), "Fish"
    ' and so on
    
    For Each A In Animals
        A.PrintMe
    Next
    
    Debug.Print "The cat has " & Animals("Cat").NumberOfPaws & " paws" 'get the animal from the collection by name
End Sub

Running test() prints:

The animal is Cat, it has 4 paws(s) and is a mammal
The animal is Dog, it has 4 paws(s) and is a mammal
The animal is Fish, it has 0 paws(s) and is not a mammal
The cat has 4 paws
Алексей Р
  • 7,507
  • 2
  • 7
  • 18
  • Thanks for the repply Алексей Р. But I would like to keep the hability to call an animal property using the intellisense list. So I can use Like objAnimals.Cat.NumberOfPaws every time I need this information. – naguall Aug 15 '21 at 20:09