Isn’t a listbox 0 based?

Hi All.

I am really confused. I am trying to fill in a listbox, with the files in a folder.
Simple enough, right?

Well, when I try the standard

app.theNativePathChosenForVideoFolder = FolderItem.ShowSelectFolderDialog

// how many items are in the folder.  For testing only
var howManyItemsAreInThisFolder as Integer
var filesInTheFolderCount as Integer
var theFileNames as FolderItem

howManyItemsAreInThisFolder = (app.theNativePathChosenForVideoFolder.Count)

// for testing only
MessageBox "There are : " + howManyItemsAreInThisFolder.ToString

// fill in the listbox.  Why?  I have NO IDEA!

For filesInTheFolderCount  = 0 To howManyItemsAreInThisFolder -1
  
  System.DebugLog "Files in the folder count = " + filesInTheFolderCount.ToString
  windowNewSequentialMoviePlayerVLC.listboxMovies.AddRow (app.theNativePathChosenForVideoFolder.item(filesInTheFolderCount).name)
  
Next filesInTheFolderCount

// for testing only
If app.theNativePathChosenForVideoFolder <> Nil Then
  MessageBox ((app.theNativePathChosenForVideoFolder.ShellPath))
End If

I get an out of bounds error. A system.debug shows it only runs one time for Item 0 in this case.

If I do this:

app.theNativePathChosenForVideoFolder = FolderItem.ShowSelectFolderDialog

// how many items are in the folder.  For testing only
var howManyItemsAreInThisFolder as Integer
var filesInTheFolderCount as Integer
var theFileNames as FolderItem

howManyItemsAreInThisFolder = (app.theNativePathChosenForVideoFolder.Count)

// for testing only
MessageBox "There are : " + howManyItemsAreInThisFolder.ToString

// fill in the listbox.  Why?  I have NO IDEA!

For filesInTheFolderCount  = 1 To howManyItemsAreInThisFolder
  
  System.DebugLog "Files in the folder count = " + filesInTheFolderCount.ToString
  windowNewSequentialMoviePlayerVLC.listboxMovies.AddRow (app.theNativePathChosenForVideoFolder.item(filesInTheFolderCount).name)
  
Next filesInTheFolderCount

// for testing only
If app.theNativePathChosenForVideoFolder <> Nil Then
  MessageBox ((app.theNativePathChosenForVideoFolder.ShellPath))
End If

It runs perfectly.

I’m confused…

Regards

You are only using AddRow here so what does this have to do with a listbox being 0-based or not?

1 Like

You’re using FolderItem.Item, which is the old API1 call. It’s one based. Item(0) doesn’t exist.

3 Likes

try

For Each file As FolderItem In   app.theNativePathChosenForVideoFolder.Children
    windowNewSequentialMoviePlayerVLC.listboxMovies.AddRow (file.Name)
Next
2 Likes

Item(0) was the parent folder itself no ?

1 Like

Was, probably, still is: check the doc.

I skipped Item(0) since the REALbasic times…

And you already know what the parent folder is…

It’s always been nil in my experience.

I definitely recall the explanation being that Item(0) was the parent. I can’t speak to it being nil however.

The docs said it was the parent, but I always found it to be nil, at least on Windows. I can’t speak to the other platforms.

1 Like

Given the times where I tried both (because I can’t remember which one is correct (0 or 1 based)), I never got item(0) being not nil on the Mac either.

I develop under my Mac for Mac and Windows. Then I don’t remember if item(0) was the parent folder under Windows. If I discovered that, I would have filled a bug report as it should be the same under Mac and Windows.

I’d be astonished if an .item() - one of the children of a folderitem - would be the parent.

However, a CMD directory listing (at least under Windows) looks like this:

So i can imagine that .item(0) would return the equivalent of . or ..
Since they are not what you expect as ‘children’ or ‘files contained by’ a folderitem, it makes sense not to use it.

Thus, .item(1) is where you start.

1 Like

Even in my oldest running Realstudio 2012 2.1 on Windows 11 there is no mention in the documentation of item(0) as the parent item. But there is a parent-property, as is today.

That may be a good mnemonic for this function!