Appending to a custom class

I have images of pages from a music book. The page title includes the unit from which it is taken and the page number. I want to add those to my custom class (UnitClass) which I call gUnitPage. As the file includes pages from 12 units, I only want the information from the particular unit I am currently working with. There are other criteria that apply, but that is not relevent to this discussion.

In the program I read the info from the file and put it in a temporary UnitClass (u) with in a for…next loop. Each time through the loop, ‘u’ correctly adds the page title and the page image. I then append ‘u’ to ‘gUnitPage.’ It ups the ubound count of gUnitPage correctly, but changes all the previous items in gUnitPage with the content in the newest ‘u’.

Supposing that I have four pages, I will end with gUnitPage(0) . . . gUnitPage(3), all with the same content for gUnitPage(n).title and gUnitPage(n).pic. Here’s the relevent code:

If f <> Nil And f.Exists Then numPics=f.Count For i=1 To numPics picname=f.item(i).Name ct=InStr(picname,s) 'This looks for the Lesson from the select Unit. [s] if ct>0 then 'if Lesson is found then pic=picture.Open(f.Item(i)) if pic<>nil then nPic=ResizePic(pic) u.pageName=picName u.pagepic=npic gUnitPage.Append u end end Next Else MsgBox "sorry unable to find picture folder." End

Sorry about the comments. Those are for me and I didn’t trim them off.
Thanks for any help you can give.

Thank you for code blocking it. I saw the post before.
The issue is you need to a new copy of the class in your for loop.

 If f <> Nil And f.Exists Then
    numPics=f.Count
    For i=1 To numPics
      picname=f.item(i).Name
      ct=InStr(picname,s) 'This looks for the Lesson from the select Unit. [s]
      if ct>0 then 'if Lesson is found then 
        pic=picture.Open(f.Item(i))
        if pic<>nil then
          u = new UnitClass

          nPic=ResizePic(pic)
          u.pageName=picName
          u.pagepic=npic
          gUnitPage.Append u
        end
      end
    Next
  Else
    MsgBox "sorry unable to find picture folder."
  End

In the future you don’t have to delete your thread, you can modify your post :slight_smile:

I just solved my own problem. If the lesson was found, I added this in each loop:

u = new unitClass

That fixed it! I’m now loading four different title and images.

Thanks Tim, that is just exactly what I did and thank-you for the editing tip!