Listbox Lastindex is returning incorrect value after inserting a row

I wanted to warn ya’ll Mavericks developers.

Listbox.Lastindex is returning incorrect value after inserting a row. It returns the inserted row’s index instead of last index. Code that has worked for months was suddenly failing on one of my machines (running 10.9) and I was terribly confused. I hope this saves others from the same difficulties. I do not get this error on 10.8.5.

I just stumbled upon this potential bug and reported it complete with example project to reproduce. Can anyone else confirm?

For now I will be using ListCount-1 in my mavericks projects.

feedback://showreport?report_id=31470

[quote=55970:@Joseph Morgan]I wanted to warn ya’ll Mavericks developers.

Listbox.Lastindex is returning incorrect value after inserting a row. It returns the inserted row’s index instead of last index.[/quote]

That is what it is supposed to do. it is supposed to return the index of the last inserted row… Ib fat all my hierarchical list boxes depend on that working and work fine under Mavericks.

This is the correct behaviour.

The name is self-explanatory - LastIndex - the last index created!

[quote=55970:@Joseph Morgan]I wanted to warn ya’ll Mavericks developers.

Listbox.Lastindex is returning incorrect value after inserting a row. It returns the inserted row’s index instead of last index. Code that has worked for months was suddenly failing on one of my machines (running 10.9) and I was terribly confused. I hope this saves others from the same difficulties. I do not get this error on 10.8.5.

I just stumbled upon this potential bug and reported it complete with example project to reproduce. Can anyone else confirm?

For now I will be using ListCount-1 in my mavericks projects.

feedback://showreport?report_id=31470[/quote]
Not a bug
see http://documentation.xojo.com/index.php/ListBox.LastIndex
The number of the last row added with the AddRow, AddFolder, or InsertRow method.

Oh… I have been relying on last index with a total misunderstanding of what it’s used for. And, after years and years, I guess I have never inserted a row. Thank you!

So, two questions:

  1. How do I retract a feedback report
  2. Is (listbox count -1) the most efficient way to get the last row in the list?

add information requesting it be closed

Yes

Thank you Karen!

[quote=55984:@Joseph Morgan]Oh… I have been relying on last index with a total misunderstanding of what it’s used for. And, after years and years, I guess I have never inserted a row. Thank you!

So, two questions:

  1. How do I retract a feedback report
    [/quote]
    Already closed :slight_smile:

Yup
Note that you might want to check if count > 0 before doing that

Actually, while I’m not an English Major, I don’t think it’s self-explanatory at all. I actually previously thought LastIndex WAS the last index of an array or list or whatever. Last Index. The Last Index.

I would think it’s more self-explanatory if it was called LastIndexAdded.

Fortunately I’ve never used it, being used to UBound(array) or ListCount - 1. I’ve been tempted to use it, but I don’t like unnecessary shortcuts.

So Joseph, don’t feel bad.

The documentation does make that clear
see http://documentation.xojo.com/index.php/ListBox.LastIndex where the first thing it says is
The number of the last row added with the AddRow, AddFolder, or InsertRow method.

I did not think it was obvious either just based on the name “LastIndex”. Of course it would be obvious if were similar to Simon’s text “LastIndexCreated” or something of the like. But the Language Reference clearly states it. I just never read it and made assumptions based solely on the name . Oh well. I know now.

And now it’s time to search and replace tons of “LastIndex” references because all my projects are littered with it.

When in doubt read the docs :stuck_out_tongue:

That is what I do.

And most of the time I have trouble deciding which one to use: LastIndex or ListIndex. So the doc let me know which one I have to use.

Give that I now understand what LastIndex is, I’d never use it.

If you have recently added/inserted a row, you usually (if not always) already know what row was last added/inserted. Use what you already know, don’t depend on something else (out of your control) keeping track for you. That’s a recipe for bugs and poor code self-documentation.

For example, I like this better:

ListBox1.AddRow("Hawaii") row = List1.ListCount - 1 // do something with the row

[quote=56242:@Garth Hjelte]Give that I now understand what LastIndex is, I’d never use it.
If you have recently added/inserted a row, you usually (if not always) already know what row was last added/inserted.
[/quote]

It has always worked correctly for me…

My most common use is in expanding rows in hierarchal list boxes.

Knowing the last row inserted when expanding then can be tricky if you are also expanding the the child rows (and their children etc) on addition (which I sometimes do).

In that case, it’s heck of a lot simpler to rely on lastindex, which works flawlessly.

  • Karen

Karen,

I think you have answered another question that has baffled me for some time. But I couldn’t put into words to ask on this forum.

I have expanded rows and all their children’s rows and all their childrens rows etc… are all expanded…, and all the rows are cell type checkbox.

I have had trouble checking the parent checkbox and having ALL it’s children also receiving cellcheck=true especially when the rows are already expanded. I should be able to then loop through from parent’s listindex to lastindex. Do you think that would work?

What you need to realize is that if you are expanding child rows as you go along that Expand rows is called recursively.

So In Expand Rows

Dim ParentChecked as Boolean = me.CellCheck(row,0)
Dim Children() as MyTreeDataStructure = me.RowTag(row)

For each Child as MyTreeDataStructure in Children
If Child.hasChildren then
me.AddFolder Child.Text
me.CellCheck(me.LastIndex,0) = ParentChecked
me.Expanded(me.lastindex) = True
Else
me.AddRow Child.Text
me.CellCheck(me.LastIndex,0) = ParentChecked
End If
Next

Forgot to do it as code

In Listbox.ExpandRow:

[code]Dim ParentChecked as Boolean = me.CellCheck(row,0)
Dim Children() as MyTreeDataStructure = me.RowTag(row)

For each Child as MyTreeDataStructure in Children
If Child.hasChildren then
me.AddFolder Child.Text
me.CellCheck(me.LastIndex,0) = ParentChecked
me.Expanded(me.lastindex) = True
Else
me.AddRow Child.Text
me.CellCheck(me.LastIndex,0) = ParentChecked
End If
Next[/code]

Argh … missed something again:

In Listbox.ExpandRow:

[code]Dim ParentChecked as Boolean = me.CellCheck(row,0)
Dim Children() as MyTreeDataStructure = me.RowTag(row)

For each Child as MyTreeDataStructure in Children
If Child.hasChildren then ’ Or if Child.ArrayOfChildren.Unbound > -1
me.AddFolder Child.Text
me.CellCheck(me.LastIndex,0) = ParentChecked
me.RowTag(me.LastIndex) = Child.ArrayOfChildren
me.Expanded(me.lastindex) = True

Else
me.AddRow Child.Text
me.CellCheck(me.LastIndex,0) = ParentChecked
End If
Next[/code]

Karen, thanks again for your help. I will try this code out.