B4J Question Create A Queue Order List

Addo

Well-Known Member
Licensed User
i have this Type And This List

B4X:
Dim alist As List

For i = 0 To 200 -1
Dim username As String
Dim usr As User
   
usr.username = "Item"&i
usr.aQueue= 503
alist.Add(usr)
       
Next
   
alist.SortType("aQueue", True)

later on i tried to change the order of sort by Assigning a new Value to the aQueue integer

the values i try to assign is between 5 and 498 based on a lower integer in the item list

i did the following


B4X:
Public Sub SetQueue(Auser As user)
     
Dim Cqueue As Int

Cqueue= 5

For i = alist.Size -1 To 0 Step -1

Dim usr As user

usr = alist.Get(i)

If (usr.aQueue > 2) And (usr.aQueue< 499)Then

If Cqueue = usr.aQueue  Then
Cqueue= Cqueue + 1
End If

Cqueue = usr.aQueue




End If

Next

Cqueue = Cqueue + 1

Auser.aQueue= Cqueue


   
End Sub


what i expecting from the sub is returning an integer start with 6 From First Request then start to get new integer based on lower integer in the Item list plus 1

as example

6
7
8
9
etc... until it reach 498


but the output of this sub gives repetitive numbers

6
7
7
7
7

it keeps giving same integers after second request even i checked if the value is equal then added + 1 and still getting same integers


what iam doing wrong ?
 

DonManfred

Expert
Licensed User
Longtime User
what iam doing wrong ?
Not using custom types correctly.
I don´t see you initializing the new opject.
B4X:
Dim alist As List
alist.initialize ' Missing!

For i = 0 To 200 -1
Dim username As String
Dim usr As User
usr.initialize ' Missing!
  
usr.username = "Item"&i
usr.aQueue= 503
alist.Add(usr)
      
Next
  
alist.SortType("aQueue", True)
 
Upvote 0

Addo

Well-Known Member
Licensed User
Not using custom types correctly.
I don´t see you initializing the new opject.
B4X:
Dim alist As List
alist.initialize ' Missing!

For i = 0 To 200 -1
Dim username As String
Dim usr As User
usr.initialize ' Missing!

usr.username = "Item"&i
usr.aQueue= 503
alist.Add(usr)
    
Next

alist.SortType("aQueue", True)


sorry mybad

but the SetQueue sub still giving a repetitive integers

each time i call this sub i excpected to get
6
7
8
9 etc until it reach 498

but what i get is
6
7
7
7
and go on. the loop doesn't increase the value as expected
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Why do you get 6, 7 ,7, 7?

Easy. The first run through gives you 6 since cQueue starts off as 5, 503 (the initial values of) is outside the if > 2 and < 499

B4X:
If Cqueue = usr.aQueue  Then
     Cqueue= Cqueue + 1
End If

Cqueue = usr.aQueue

This code makes no sense to me. Let's say Cqueue is 5 (as it is initially when you start the method) and usr.aQueue is 5, then cQueue becomes 6 (because of Cqueue + 1), but then you reset Cqueue to 5 (via Cqueue = usr.aQueue). Before assigning the value to the user, you once more increment Cqueue by 1 (making it 6 again) and assigning it to the usr.aQueue. The next time you go from top to bottom of the list, you arrive at the bottom, in which case Cqueue = usr.aQueue sets Cqueue to 6. Then outside the For/Next loop, you increment the value by one, giving you 7. Next call, the same happens and therefore you get 6, 7, 7, 7, 7, 7 and on.
 
Upvote 0

Addo

Well-Known Member
Licensed User
Why do you get 6, 7 ,7, 7?

Easy. The first run through gives you 6 since cQueue starts off as 5, 503 (the initial values of) is outside the if > 2 and < 499

B4X:
If Cqueue = usr.aQueue  Then
     Cqueue= Cqueue + 1
End If

Cqueue = usr.aQueue

This code makes no sense to me. Let's say Cqueue is 5 (as it is initially when you start the method) and usr.aQueue is 5, then cQueue becomes 6 (because of Cqueue + 1), but then you reset Cqueue to 5 (via Cqueue = usr.aQueue). Before assigning the value to the user, you once more increment Cqueue by 1 (making it 6 again) and assigning it to the usr.aQueue. The next time you go from top to bottom of the list, you arrive at the bottom, in which case Cqueue = usr.aQueue sets Cqueue to 6. Then outside the For/Next loop, you increment the value by one, giving you 7. Next call, the same happens and therefore you get 6, 7, 7, 7, 7, 7 and on.


i think you get me wrong , i am trying to make a Queue between number from 6 to 498

each request of setQueue should search for the lowest integer value in the loop and add + 1 to it. so the next item takes next turn in sequence.

the lowest value in the list should be bigger than 2 and smaller than 498

thats why i added if condition to avoid items smaller than 2 and bigger than 498

here is the solution it is simple and was missing from my brain

B4X:
If Cqueue <= usr.aQueue Then
Cqueue =  usr.aQueue
End If

i compared the value that starts with 5 sense it will not be repetitive then get the bigger value based to it , then increase the bigger value.. and so on
 
Last edited:
Upvote 0
Top