Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Definening an array of a certain class-type

Status
Not open for further replies.

Overdoos

IS-IT--Management
May 31, 2000
79
BE
Hi,

I'm new to VB6 and I'm trying (just to get the hang of things) to create a phonebook using flatfiles. (I'll need the flatfile-parsing for future stuff)

I created a class that looks like this:

--
'local variable(s) to hold property value(s)
Private mvarFirstName As Variant 'local copy
Private mvarLastName As Variant 'local copy
Private mvarTelNumber As Variant 'local copy
Private mvarEmailAddress As Variant 'local copy
Public Function StringOut(Optional lstrSeparator As Variant = " :: ") As String
strOutString = mvarFirstName & lstrSeparator & mvarLastName & lstrSeparator & mvarTelNumber & lstrSeparator & mvarEmailAddress
End Function

Public Property Let EmailAddress(ByVal vData As Variant)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.EmailAddress = 5
mvarEmailAddress = vData
End Property

Public Property Set EmailAddress(ByVal vData As Variant)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.EmailAddress = Form1
Set mvarEmailAddress = vData
End Property

Public Property Get EmailAddress() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.EmailAddress
If IsObject(mvarEmailAddress) Then
Set EmailAddress = mvarEmailAddress
Else
EmailAddress = mvarEmailAddress
End If
End Property

Public Property Let TelNumber(ByVal vData As Variant)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.TelNumber = 5
mvarTelNumber = vData
End Property

Public Property Set TelNumber(ByVal vData As Variant)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.TelNumber = Form1
Set mvarTelNumber = vData
End Property

Public Property Get TelNumber() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.TelNumber
If IsObject(mvarTelNumber) Then
Set TelNumber = mvarTelNumber
Else
TelNumber = mvarTelNumber
End If
End Property

Public Property Let LastName(ByVal vData As Variant)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.LastName = 5
mvarLastName = vData
End Property

Public Property Set LastName(ByVal vData As Variant)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.LastName = Form1
Set mvarLastName = vData
End Property

Public Property Get LastName() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.LastName
If IsObject(mvarLastName) Then
Set LastName = mvarLastName
Else
LastName = mvarLastName
End If
End Property

Public Property Let FirstName(ByVal vData As Variant)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.FirstName = 5
mvarFirstName = vData
End Property

Public Property Set FirstName(ByVal vData As Variant)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.FirstName = Form1
Set mvarFirstName = vData
End Property

Public Property Get FirstName() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.FirstName
If IsObject(mvarFirstName) Then
Set FirstName = mvarFirstName
Else
FirstName = mvarFirstName
End If
End Property
--

and now I would like to create an array in my application that can hold data-types of that class, to store the records I read from my flat files...

Unfortunatly, I cannot find any way to define an array using this class as type (it gives me run-time errors).

--o0o--

Just to be as complete as possible, this is my last effort for now...
--
Open lblTelBoek.Caption For Input As 1
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, strTelBkLine ' Read data into two variables.
intPosOfSemiColon = InStr(strTelBkLine, ";") 'a trailing $ forces the variable to 'string'.
strFullName = Left(strTelBkLine, intPosOfSemiColon - 1)
intPosOfNameSeparator = InStr(strFullName, " ")
Set persoon1.intRecordTeller = New Contact
With persoon1.intRecordTeller
FirstName = Left(strFullName, intPosOfNameSeparator - 1)
LastName = Right(strFullName, Len(strFullName) - inPosOfNameSeparator)
TelNumber = Right(strTelBkLine, Len(strTelBkLine) - intPosOfSemiColon)
End With
intRecordTeller = intRecordTeller + 1
Loop
Close
--
(Beware: this code probably suxx bigtime, but I got tired after #-times without finding any decent tutorial online :/ If you can point me to a great online tutorial that might help this PL-SQL/perl/php-developer become a half-decent VB-coder, I'ld be very grateful)

 
You should probably be creating a collection class rather than an array. Select "Project / Add Class Module / VB Class Builder ". Click on the top of the tree where your project name appears and then click "Add New Collection".

On the screen that appears, provide a name for the collection (probably "Contacts" since this one is "Contact") and in the right pane click on the "contact" class under "Collection of Existing Class". Click "OK" and exit the class builder. Select "Yes" when it asks you if you want to update the project.

You will now have a new collection class to which you can add contacts, retrieve them, enumerate them, delete them, etc.
 
Assuming you have an array declared somewhere like this:

dim persoon1(100) as Contact

Then
Set persoon1.intRecordTeller = New Contact

becomes
Set persoon1(intRecordTeller) = New Contact

and this follows:

With persoon1(intRecordTeller)
.FirstName = Left(strFullName, intPosOfNameSeparator - 1)
.LastName = Right(strFullName, Len(strFullName) - inPosOfNameSeparator)
.TelNumber = Right(strTelBkLine, Len(strTelBkLine) - intPosOfSemiColon)
End With
intRecordTeller = intRecordTeller + 1


..probaly leading to an index out of bounds error, since the array will eventually not be the right size to hold all your records.
This is why a collection is a bit nicer - you just 'add' to the collection and VB handles the memory allocation for you.

You can simulate this with an array using
Redim Preserve just after the
intRecordTeller = intRecordTeller + 1,
or take the really kludgy way out and dim the array extra large before you begin..

dim persoon1(3000) as Contact
remembering to check the value of intRecordTeller to ensure it never exceeds you rinitial value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top