Fill an array variable from a loop!

Hello everyone,
I don’t understand where the error is!
In a loop to list all the files in a directory and put each filename in an array variable, well I have an error! I put you the code below
Thanks for your help

Var dlg As New SelectFolderDialog
dlg.ActionButtonCaption = “Select”
dlg.Title = “Title Property”
dlg.PromptText = “Prompt Text”
dlg.InitialFolder = SpecialFolder.Documents

f = dlg.ShowModal
If f <> Nil Then
’ Use the folderitem here
Label1.Text=f.shellpath
Else
’ User cancelled
End If

Var pic() As String
Var nb As Integer
For Each file As FolderItem In f.Children
nb = nb+1
pic(nb)=file.Name
Next

What kind of error? :smiley:

f is not declared. Your posted code is full of curly quotes, too, but perhaps your actual code is not.

1 Like

pic.add(file.Name)

2 Likes
  • f can be NIL
  • file can be something else than a picture file.
  • pic(nb) should be replaced with pic.Add …

Well, you have many errors in there. But we do not know which one you need our help with to fix it. :wink:

Thanks, but in fact I misspoke!
The pic variable does contain all the file names, regardless of their extension (I’ll do a filter for jpg files later).
But the final goal is to display the images in this directory in a canvas, how can I do that?
That’s why I wanted to retrieve all the file names in a variable and use it later.
Sorry, I’m a beginner… :pleading_face:

No need to feel sorry. We have all been there. :slight_smile:

Var f As FolderItem
Var pic() As String
Var dlg As New SelectFolderDialog

dlg.ActionButtonCaption = "Select"
dlg.Title = "Title Property"
dlg.PromptText = "Prompt Text"
dlg.InitialFolder = SpecialFolder.Documents

f = dlg.ShowModal
If f = Nil Or Not f.Exists Then Return

Label1.Text=f.shellpath

For Each file As FolderItem In f.Children
  
  If Not file.IsFolder Then
    
    If file.Name.Right(4) = ".png" Then
      
      // Because your previous nb=nb+1 did not increase the pic array magically
      // and because the size of the array is unknow upo to this point
      // you can increase it 'on thefly' using the following:
      pic.Add(file.Name)
      // this will add a new element on each cycle
      // If you later need to know the 'size' of the array,
      // you can use pic.Count f.e.
      
    End If
    
  End If
  
Next

The line If file.Name.Right(4) = ".png" Then adds only files ending with .png to the array. You can add more file types with lines like this If file.Name.Right(4) = ".ini" Or file.Name.Right(5) = ".jpeg" Then.
There are smarter solutions for this, but i think for a beginner, this is the way to start.

You can use an array of pictures instead of the name. In the loop, you can try to load the picoture.

Try
  pic.add(Picture.Open(f))
Catch e As IOException
  ' unable to open picture
End Try
1 Like

Thank you @Marius_Dieter_Noetzel
I did misunderstud the issue :slight_smile:

Would it make sense to add a check for an oom exception then?

Catch e As OutOfMemoryException
  ' out of memory, too many pictures?
End Try

Thanks, it works well :pray:

1 Like

My mistake!

Maybe because pic is defined as String.
Try it again with:

Var pic() As Picture
Var f As FolderItem
Var pic() As Picture   '<<<<<<<<<<<<<<<<<<<<<<<<
Var dlg As New SelectFolderDialog

dlg.ActionButtonCaption = "Select"
dlg.Title = "Title Property"
dlg.PromptText = "Prompt Text"
dlg.InitialFolder = SpecialFolder.Documents

f = dlg.ShowModal
If f = Nil Or Not f.Exists Then Return

Label1.Text=f.shellpath

For Each file As FolderItem In f.Children
  
  If Not file.IsFolder Then
    
    If file.Name.Right(4) = ".png" Then
      
      Try '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        pic.add(Picture.Open(file)) '<<<<<<<< Mind the 'file' instead of just 'f' here!
      Catch e As IOException '<<<<<<<<<<<<<<<
        ' unable to open picture '<<<<<<<<<<<
      End Try '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      
    End If
    
  End If
  
Next

I’ve marked the changes with '<<<<'s :smiley:
Hope it helps. I have to leave for now. :slight_smile:

Thanks, but nothing’s happening!

So far you’ve filled an array (or should have done). Have you traced this code through the debugger to see that it’s behaving as desired?

By placing breakpoints?

Place one at the start, and when it reaches that, you can step through. You can add/remoce reakpoints during that process. Or add a ‘break’ statement right at the end (no breakpoints), and then it will run and drop into the debugger at the break statement. Then you can examine variables to verify they have the expected values.

pic Picture(-1) ?