Consulting

Results 1 to 5 of 5

Thread: Solved: custom class - multiple values for an attribute

  1. #1
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location

    Solved: custom class - multiple values for an attribute

    Hi,

    I'm still looking at making some custom classes and have a question before I decide this is definately what I want to do. I'm wondering can you assign multiple values to a single property and how I would go about this.

    For instance say a class is employee and an attribute is phone number. Now I could say add a property of businessphone, homephone, mobilephone etc. This limits me to 3 phone numbers. What if for instance the employee had 5. Would it be possible to assign all 5 to the phone property and be able to add or subtract them as necessary? If so what would the best way to go about this be?

    Thanks in advance for your help.
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    FWIW, I'd put a Collection in my Class, keyed by the type of data (e.g. Home Phone) and the value = the data itself (e.g. 800-987-6543)

    The Collection would be exposed as a property, and/or you could have special methods and properties for the frequently used options

    Paul

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Brian,

    This is a tad tricky, as you have two collections. One collection of people, and one collection of numbers per person.

    I knocked up an example for my own interest. It works, but I am not sure I have it quite right because I cannot iterate the numbers in a for ... next loop, so I have to iterate each item in a For i = 1 To Count ... Next.

    If I can fix that issue I will post it again.
    Attached Files Attached Files
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    [vba]
    Sub MyUsers()
    Dim AllPeople As People
    Dim ThisPerson As Person
    Dim mpPerson As Person
    Dim mpNumber As PhoneNumber
    Dim ThisNumber As PhoneNumber
    Dim mpDetails As String
    Dim LastRow As Long
    Dim i As Long, j As Long
    Set AllPeople = New People
    With Worksheets("UserData")

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = 2 To LastRow

    Set ThisPerson = New Person
    j = i
    Do

    Set ThisNumber = New PhoneNumber
    ThisNumber.PhoneType = .Cells(j, "C").Value2
    ThisNumber.Number = .Cells(j, "D").Value2
    ThisPerson.Add ThisNumber
    j = j + 1
    Loop Until .Cells(j, "A").Value2 <> .Cells(i, "A").Value2 Or _
    .Cells(j, "B").Value2 <> .Cells(i, "B").Value2
    i = j - 1

    ThisPerson.FirstName = .Cells(i, "A").Value2
    ThisPerson.LastName = .Cells(i, "B").Value2
    AllPeople.Add ThisPerson
    Next i
    End With
    mpDetails = "List of everyone:"
    For Each mpPerson In AllPeople

    mpDetails = mpDetails & vbNewLine & vbTab & mpPerson.FirstName & " " & mpPerson.LastName
    For Each mpNumber In mpPerson.Items
    mpDetails = mpDetails & vbNewLine & vbTab & vbTab & mpNumber.PhoneType & " " & mpNumber.Number
    Next



    Next mpPerson

    MsgBox mpDetails

    Set ThisNumber = Nothing
    Set ThisPerson = Nothing
    Set ThisNumber = Nothing
    Set mpPerson = Nothing
    Set mpNumber = Nothing
    Set AllPeople = Nothing
    End Sub
    [/vba]

    using this slight change I was able to use a for next loop. Is this how you meant or should it potentially work in a different way?
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not quite Brian. what I was trying to do was similar to the outer loop,

    [VBA]

    mpDetails = "List of everyone:-"
    For Each mpPerson In AllPeople

    mpDetails = mpDetails & vbNewLine & vbTab & mpPerson.FirstName & " " & mpPerson.LastName
    For Each mpNumber In mpPerson

    mpDetails = mpDetails & vbNewLine & vbTab & vbTab & mpNumber.PhoneType & ":" & vbTab & mpNumber.Number
    Next mpNumber
    Next mpPerson
    [/VBA]

    Interestingly, although this failed to work originally (I think :-(), I did it with .Items as well, which worked as it did with you, but now it works fine without the .Items - very odd.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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