Consulting

Results 1 to 3 of 3

Thread: Terminology

  1. #1
    VBAX Newbie
    Joined
    Aug 2016
    Posts
    1
    Location

    Terminology

    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

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •