HeSaidJoe or anyone else - Collections problem
Hi,
This code works... but I don't understand why do I have to do: Set Person As Cperson twice. First I do it at the top of the procedure, then I DO IT AGAIN AFTER adding the object to collection. Why?
Also, since I'm Set(ting) it twice, shouldn't I destroy it twice as well (Set Person = Nothing). I only do it at the bottom.
Thanks.
Code:
Option Explicit
Dim People As New Collection
Private Person As CPerson
Private Sub cmdRequestPerson_Click()
Set Person = New CPerson
Dim strPersonName As String
Dim ctr As Integer
Dim intFileNumber As Integer
' Temp variables for reading file
Dim sName As String
Dim sPhone As String
Dim iAge As Integer
' Read Persons.txt in to a collection
intFileNumber = FreeFile
' Read name, phone, age for all poeple in the file
Open "Phones.txt" For Input As intFileNumber
Do While (Not (EOF(intFileNumber)))
Input #intFileNumber, sName, sPhone, iAge
' Fill object
Person.Name = sName
Person.Phone = sPhone
Person.Age = iAge
' Add current object to collection
People.Add Person
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
' WHY DO I HAVE TO SET NEW OBJECT HERE AGAIN???????????
' I ALREADY SET IT UP AT THE TOP OF SUB
Set Person = New CPerson
Loop
Close #intFileNumber
' Get Person's name from user
strPersonName = InputBox("Enter Person's name")
' Go thru the collection & find requested Person's age
For ctr = 1 To People.Count
If strPersonName = People.Item(ctr).Name Then
msgbox People.Item(ctr).Name & vbTab & _
People.Item(ctr).Age
Exit Sub
End If
Next ctr
' Destroy object
Set Person = Nothing
End Sub