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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arrays in classes

Status
Not open for further replies.

johnnyv

Programmer
Jul 13, 2001
216
CA
A general question about classes
can you declare variable arrays inside a class?
and if so
how do you write the let and get properties inside the class
for each variable in the array
an example
I am accessing a table in access containing 10 fields named songname1,songname2, songname3....songname10
I want to be able to move the data from the fields in the table to data members inside a class
I then want to step through the data members of the class to see if they contain values ie a song name


any comments sugestions thanks
 
Simple ans: Yes

ClassMod
Private aryMyArray(9) As String 'or (1 To 10)

Public Property Let MySongs(SongName As String, Index As Integer)

aryMyArray(Index) = SongName

End Property

Public Property Get MySongs(Index As Integer) As String

MySongs = aryMyArray(Index)

End Property

You could also use a dynamic array:


ClassMod
Private aryMyArray() As String
Public Property Let NumElements(intNum As Integer)
ReDim Preserve aryMyArray(intNum)
End Property
Public Property Get NumElements()As Integer
NumElements = UBound(aryMyArray)
End Property
+ as above let/get

another way, would be to get rid of the array entirely, and use a class to hold data (1 for each title) and put them in a collection.



Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
Hi,

Or you can transfer an entire array by declaring it as variant. I haven't tried it as a property only in a public sub (or function) and it works fine, so I guess that that also the case as a property.

You can even make arrays of arrays:
----------------------------------------------------------
Dim MyData as variant

public sub MySub(MyArr as variant)
Redim preserve MyData(Ubound(MyData)+1)
MyData(ubound(MyData))= MyArr
end sub
-----------------------------------------------------------

Sunaj
 
Also, have you considered using a collection rather than an array? One of the big problems os using arrays in classes is that it is difficult to get the boundaries of the array outside of the class. You end up having hacks such as ArrayCount properties which are a PITA.

Chaz
 
thanks for the reply guys Tim I get a compile error in the let call. 'property procedures for the same property are inconsistent. Or procedure has an optional parameter?? and ideas
 
sorry...
try this:

Public Property Let MySongs(Index As Integer, SongName As String)

aryMyArray(Index) = SongName

End Property

Public Property Get MySongs(Index As Integer) As String ' , Optional SongName As String

MySongs = aryMyArray(Index)

End Property

Here's how to call:
MC is name of class obj
Private Sub Command1_Click()
With MC
.MySongs(3) = "q"
'sets element index of 3 to q
End With
End Sub

Private Sub Command2_Click()
With MC
Label1.Caption = .MySongs(3)
'returns q
End With
End Sub
Tim

Remember the KISS principle:
Keep It Simple, Stupid!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top