PDA

View Full Version : Solved: custom class - multiple values for an attribute



BrianMH
04-20-2011, 05:34 AM
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.

Paul_Hossler
04-20-2011, 06:21 AM
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

Bob Phillips
04-21-2011, 12:10 AM
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.

BrianMH
04-21-2011, 01:40 AM
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


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?

Bob Phillips
04-21-2011, 08:20 AM
Not quite Brian. what I was trying to do was similar to the outer loop,



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


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.