iOS Question apparent & sporatic List.Add() bug in debugger

Rattigan_Roger

Member
Licensed User
Longtime User
For y=1 To 5
listItem=oneList.Get(y)
anotherList.Add(list_item)
Next

All variables are properly Dim-ed and initialized.

The above snippet of code sometimes but NOT ALWAYS ,while in the debugger in the context of a much larger program seems to fail in a rather fascinating way.
Checking the value of listItem from oneList as it progresses through the loop is as follows:
item#1
item#2
item#3
item#4
item#5

When checking the contents of anotherList when the loop finishes is as follows:
5(0x5)
item#1
item#1
item#1
item#1
item#1

As far as I can tell the finished app is fine and does not exhibit the behavior.
This being a non-repeatable bug, as sometimes the code seems to do just what I think I'm telling it to do, is a bit maddening.
A Tools>Clean Project does not appear to have any effect one way or another.
 

stevel05

Expert
Licensed User
Longtime User
It is not clear from your code exactly what you are trying to do. There could be a few errors in it as it stands:

1) The first element in oneList will be element 0, so I would expect to see:

B4X:
For y = 0 to 4

Should listItem and list_Item be the same object?

You should Dim the listItem to an appropriate type each time you assign it, otherwise it will just change the pointer for all added elements.

If so your code should be more like this:

B4X:
For y=0 To 4
    Dim listItem As String = oneList.Get(y)
    anotherList.Add(listItem)
Next
 
Upvote 0

Rattigan_Roger

Member
Licensed User
Longtime User
It is not clear from your code exactly what you are trying to do. There could be a few errors in it as it stands:

1) The first element in oneList will be element 0, so I would expect to see:

B4X:
For y = 0 to 4

Should listItem and list_Item be the same object?

You should Dim the listItem to an appropriate type each time you assign it, otherwise it will just change the pointer for all added elements.

If so your code should be more like this:

B4X:
For y=0 To 4
    Dim listItem As String = oneList.Get(y)
    anotherList.Add(listItem)
Next


I certainly appreciate the reply and Thank You.
While I did try the loop with the Dim statement inside as you suggested (the list_item, listItem was a forum typo) it didn't fix it.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I suspect that the code in post#1 is not your real code ?
So without knowing what exatly you have done it is difficult to give a concrete advice.
What type of object is listItem
As already mentioned by stevel05 the index for Lists begins with 0 not with 1.
Do you call the loop several times ?
If yes, do you want to add the values at the end ?
Otherwise you would need to clear the list before adding new values.
 
Upvote 0

Rattigan_Roger

Member
Licensed User
Longtime User
genList.clear
For y=1 To ans.Size-1
list_item=codePicker2_list.Get(y)
s=ans.Get(y-1)
If ans.Get(y-1)="" Then Exit
list_item=list_item.Replace("<miles>",s)
genList.Add(list_item)
Next

above is the real code
s is a local string dimmed in the subroutine
List_Item is a process Global string
genList,ans and codePicker2_list. are process global lists

sometimes (about 40% of the time) the code behaves itself. I can tell because the list items from genList are displayed.

Thanking all in advance for Your time and trouble.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Well, something I don't understand.
You say that ans is a list from a csv file beginning with 1 because of the header.
So, why do you use s=ans.Get(y-1) reading the header !?

When you post code you must use CODE tags.
upload_2015-12-28_15-51-52.png
 
Upvote 0

Rattigan_Roger

Member
Licensed User
Longtime User
Thank You,
and I do have an explanation for that.
The list codePicker2_list comes from a CSV file with a header.
The list ans comes from another csv file without a header.

I was not aware of the code function.
I will be sure to use it.

The problem is that sometimes the list codePicker2_list loads properly with different elements in each position as it should.

The other times it loads with the same element in all the positions.
Even though the very same code is executed.
Looking at the value of of list_item they appear to be right until the list is loaded.
 
Upvote 0
Top