PDA

View Full Version : Terminology



Brostis
08-14-2016, 12:47 PM
Hello team,

Very new to the game here but picking up as much as I can as I go.

I'm not sure of the terminology for what I want to do here, I think its classes? Objects/attributes? array?

Essentialy, I want to store variables with their own sub variables i.e. I want to have something like this

Person,
2,omnivore,hot

Cow,
4,herbivore,hot

Lion,
4,carnivore,hot

Snake,
0,carnivore,cold

And then be able to recall the variables by name into a table reading

Animal:
Legs:
Diet:
Blood:

and have the variable (person,cow,lion,snake) along with its subvariables populate the table, and since I want a fair few animals in my list, including mythical, I'd like to be able to streamline this as far as possible.

Can anyone assist in the terminology or referencing to a basic how to?

Thanks

Brostis

Paul_Hossler
08-14-2016, 02:03 PM
Several ways to do it, but I'd suggest a class for Animal and a collection for the classes

This is what clsAnimal could look like



Option Explicit

Public Enum eDiet
Omnivore = 1
Herbivore = 2
Carnivore = 3
End Enum

Public Enum eBlood
Hot = 1
Cold = 2
End Enum

Private m_Animal As String
Private m_Legs As Long
Private m_Diet As eDiet
Private m_Blood As eBlood

Property Let Animal(s As String)
m_Animal = s
End Property
Property Get Animal() As String
Animal = m_Animal
End Property


Property Let NumberOfLegs(n As Long)
m_Legs = n
End Property
Property Get NumberOfLegs() As Long
NumberOfLegs = m_Legs
End Property


Property Let Diet(d As eDiet)
m_Diet = d
End Property
Property Get Diet() As eDiet
Diet = m_Diet
End Property
Property Get DietStr() As String
DietStr = Array("omnivore", "herbivore", "carnivore")(m_Diet - 1)
End Property


Property Let Blood(b As eBlood)
m_Blood = b
End Property
Property Get Blood() As eBlood
Blood = m_Blood
End Property
Property Get BloodStr() As String
BloodStr = Array("hot blooded", "cold blooded")(m_Blood - 1)
End Property

Sub Dump()
Call MsgBox("The " & Animal & " is a " & BloodStr & " " & DietStr & " with " & NumberOfLegs & " legs", vbInformation + vbOKOnly, "Animals")
End Sub





And the main module that has the collection



Option Explicit

Public CollectionOfAnimals As Collection

Sub test()
Dim TheAnimal As clsAnimal
Dim v As Variant

Set CollectionOfAnimals = New Collection
Set TheAnimal = New clsAnimal
With TheAnimal
.Animal = "Person"
.NumberOfLegs = 2
.Diet = Omnivore
.Blood = Hot
End With
CollectionOfAnimals.Add TheAnimal, TheAnimal.Animal
Set TheAnimal = New clsAnimal
With TheAnimal
.Animal = "Cow"
.NumberOfLegs = 4
.Diet = Herbivore
.Blood = Hot
End With
CollectionOfAnimals.Add TheAnimal, TheAnimal.Animal
Set TheAnimal = New clsAnimal
With TheAnimal
.Animal = "Lion"
.NumberOfLegs = 4
.Diet = Carnivore
.Blood = Hot
End With
CollectionOfAnimals.Add TheAnimal, TheAnimal.Animal
Set TheAnimal = New clsAnimal
With TheAnimal
.Animal = "Snake"
.NumberOfLegs = 0
.Diet = Carnivore
.Blood = Hot
End With
CollectionOfAnimals.Add TheAnimal, TheAnimal.Animal

For Each v In CollectionOfAnimals
v.Dump
Next
MsgBox "A person has " & CollectionOfAnimals("Person").NumberOfLegs & " legs"

End Sub




There's no error checking for adding one that's already there for example

I would consider an .Add sub in the class to plop in all parameters at once

Some things to think on

SamT
08-14-2016, 03:14 PM
Public Type Animal
Type as Sting
Legs as Long
Diet As String
Blood As String
End Type


Sub AddAnimals()
Dim Animals As Collection

Dim X as Animal

With X
.Type = Human
.Legs = 3
.Diet = Rocks
.Blood = Green
End With

Animals.Add X

End Sub

'Another sub example
Dim Animals As Collection
Dim X As Animal

For each Cell in Range
With X
.Type = Cell.Value
.Blood = Cell.Offset(,1)
.Diet = Cell.Offset(,2)
.Legs = Cell.Offset(,3)
End With
Animals.Add X
Next


Dim Ani as Animal
Rw = 1
For each Ani in Animals
Cells(rw, 1) = Ani.Type
Rw=rw+ 1
Next ani