I'm finally learning to use class objects and I wish I had done this sooner! It's making things much simpler to deploy and maintain but one bit I'm hung up on is creating an array or collection of a class objects. For example, I can load a record from a database into my class object, save it back to the database, etc, but if I want to load a bunch of records based on some criteria, I can't figure out how to group them. I've tried adding into an array and a collection but in both instances each record in the array, collection refers to the last object that was set because I'm using a 'placeholder' object to load into from a loop, see below (i've commented out the previous attempt at using an array), i.e. the debug line prints out the same PlateID from the last record that was selected in the table, even though it correctly loads each record before setting to the array or collection. Seems like these only point to the object but don't actually store it so my question is, is there any way to do what I'm trying to do or is there a better way I should be doing this?
Thanks
- Private Sub Load_Plate_Button_Click()
- Dim selplateid As String, choice As Integer, rng As Range, row As Integer, i As Integer, selcount As Integer
- Dim fwddpm As String, revdpm As String
- Dim load_clsPlate As New clsPlates
- Dim list_clsPlates As New Collection
- 'Dim arr_clsPlate() as clsPlates
- ' selcount = 0
- For i = 1 To PlateForm.PlateList.ListCount - 1
- If PlateForm.PlateList.Selected(i) = True Then
- selcount = selcount + 1
- load_clsPlate.Load_Plates (PlateForm.PlateList.List(i, 16))
- 'MsgBox (load_clsPlate.PlateID_Plates)
- ' ReDim Preserve arr_clsPlate(1 To selcount)
- ' Set arr_clsPlate(selcount) = load_clsPlate
- list_clsPlates.add load_clsPlate
- End If
- Next i
- For Each load_clsPlate In list_clsPlates
- Debug.Print load_clsPlate.PlateID_Plates
- Next load_clsPlate
- end sub