+ Reply to Thread
Results 1 to 3 of 3

data storage

Hybrid View

  1. #1
    Registered User
    Join Date
    09-19-2011
    Location
    oc,ca
    MS-Off Ver
    Excel 2003
    Posts
    44

    data storage

    I have a 10 element array named "people" and it contains the name of 10 different people. Each element of the array "people" has an unknown # of attributes.

    My question is what are some ways I can store the attributes of each name so that they are linked to the corresponding name in the array?

    Currently, I joined the attributes in a single string and then split them into an array when I need to use them. For example


    People(0)="John"
    Attribute1(0)="male , 44 yrs old, white, single"
    AttributeArray=Split("Attribute1(0)", ",", , vbTextCompare)


    Is there a better way?

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: data storage

    why not just a two-dimensional array compared to using a jagged array
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: data storage

    This would be a perfect application of custom classes.
    You would want two custom objects:
    clsPerson has the properties of Name, Age, Gender, MaritalStatus and Race
    clsPeople has a collection of clsPerson's, called Persons; a property Count, and a function Add

    in a class module (named clsPerson) put

    'in class module clsPerson
    
    Public Index As Long
    Public Collection As Collection
    Dim pName As String
    Dim pGender As Boolean
    Dim pAge As Double
    Dim pRace As String
    Dim pMarital As String
    
    Property Get Name() As String
        Name = pName
    End Property
    Property Let Name(strName As String)
        pName = StrConv(strName, vbProperCase)
        If Index = 0 Then
            pName = strName
        Else
            Collection.Remove Index
            If Collection.Count < Index Then
                Collection.Add Item:=Me, Key:=pName
            Else
                Collection.Add Item:=Me, Key:=pName, before:=Index
            End If
        End If
    End Property
    
    Property Get Gender() As String
        Gender = "Female"
        If pGender Then
            Gender = "Male"
        End If
    End Property
    Property Let Gender(strGender As String)
        pGender = (LCase(strGender) = "male")
    End Property
    
    Property Get Age() As Double
        Age = pAge
    End Property
    Property Let Age(dblAge As Double)
        If (0 <= dblAge) Then pAge = dblAge
    End Property
    
    Property Get Race() As String
        Race = pRace
    End Property
    Property Let Race(strRace As String)
        pRace = StrConv(strRace, vbProperCase)
    End Property
    
    Property Get MaritalStatus() As String
        MaritalStatus = pMarital
    End Property
    Property Let MaritalStatus(strMarital As String)
        pMarital = StrConv(strMarital, vbProperCase)
    End Property
    
    Private Sub Class_Initialize()
        pAge = -1
    End Sub
    
    Private Sub Class_Terminate()
        Set Collection = Nothing
    End Sub
    in the class module clsPeople:

    ' in class module clsPeople
    
    Public Persons As Collection
    
    Function Add(Name As String, _
                            Optional Gender As String, _
                            Optional Age As Double = -1, _
                            Optional Race As String = "unknown", _
                            Optional MaritalStatus As String = "unknown") As clsPerson
        Dim aPerson As New clsPerson
        With aPerson
            .Name = Name
            If 0 <= Age Then .Age = Age
            .Gender = Gender
            .Race = Race
            .MaritalStatus = MaritalStatus
            .Index = Persons.Count + 1
            Set .Collection = Persons
        End With
        Persons.Add Item:=aPerson, Key:=aPerson.Name
        Set Add = aPerson
        Set aPerson = Nothing
    End Function
    
    Property Get Count() As Long
        Count = Persons.Count
    End Property
    Private Sub Class_Initialize()
        Set Persons = New Collection
    End Sub
    
    Private Sub Class_Terminate()
        Dim onePerson As clsPerson
        For Each onePerson In Persons
            Set onePerson = Nothing
        Next onePerson
        Set Persons = Nothing
    End Sub
    And it could be used with code like this (in a normal module)
    Sub Test()
        Dim Folks As New clsPeople
        Dim aPerson As clsPerson
        Dim i As Long
        
        Folks.Add "bob", "male", 44, "Single", "white"
        Folks.Add "sally", "female", 56, "married", "latina"
        Folks.Add "George", "male", MaritalStatus:="bigamist"
        Folks.Add "Sandy", Gender:="female", Age:=25
        
        MsgBox "Bob's info is" & vbCr & vbCr & PersonSummary(Folks.Persons("Bob"))
        
        For i = 1 To Folks.Count
            MsgBox PersonSummary(Folks.Persons(i))
        Next i
    End Sub
    
    Function PersonSummary(aPerson As clsPerson) As String
        With aPerson
            PersonSummary = .Name
            PersonSummary = PersonSummary & vbCr & .Age & " years old"
            PersonSummary = PersonSummary & vbCr & .Gender
            PersonSummary = PersonSummary & vbCr & .MaritalStatus
            PersonSummary = PersonSummary & vbCr & .Race
        End With
    End Function
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1