Android Question list.set not working

gruizelgruis

Member
Licensed User
Longtime User
Hi all,

I use a list to store an array of information
I want to go trough the list and change a value in that array:

B4X:
Sub AddToVerkoopList(Gevr_ProdGeg As ProductGegevens)
Dim prodteller As Int
Dim CurProd As ProductGegevens
    CurProd.Initialize

Dim Gevonden As Boolean
For i = 0 To VerkoopList.Size-1
    CurProd = VerkoopList.Get(i)
    If Gevr_ProdGeg.Naam = CurProd.Naam Then '// dus exact het zelfde
        CurProd.Aantal = CurProd.Aantal + Gevr_ProdGeg.Aantal'Gevr_ProdGeg.Aantal
        VerkoopList.Set(i,CurProd)
        Gevonden = True
        Exit
    End If
Next
If Gevonden = False Then VerkoopList.Add(Gevr_ProdGeg)

End Sub

It looks like it is adding the the aantal, but when I caal this sub again. It shows the initial value
Any thoughts ?
 

gruizelgruis

Member
Licensed User
Longtime User
B4X:
    Type ProductGegevens(Id As Long, Naam As String,Aantal As Int,  Prijs As Int, SuppLst As String, ItemData As Object)
 
Upvote 0

gruizelgruis

Member
Licensed User
Longtime User
I found someting strange hapening here. The code mentioned in top posting is fine.

It looks like the value get set back to 1 just before I call the
Sub AddToVerkoopList(Gevr_ProdGeg As ProductGegevens)

B4X:
cCur_KnopGeg.Aantal = 1 '<-- this sets the
' VerkoopList.aantal aswell. It looks like its connected.

Mod_Common.AddToVerkoopList(cCur_KnopGeg)

PLease advice
 
Upvote 0

gruizelgruis

Member
Licensed User
Longtime User
Thanks Erel. Like it states in here: https://www.b4x.com/android/forum/threads/variables-objects-in-basic4android.8385/#content
<quote>
a copy of the reference is passed.
This means that the data itself isn't duplicated.
</quote>

This is exactly what seems to be the "Feature".




On an activity I catch a click event

B4X:
Sub Cmd_Omschrijving_Click
    Dim Btn As Button
    Dim Bedrag As String
    Dim cCur_KnopGeg As ProductGegevens

    Btn = Sender
    '/ omschrijving wijst naar lbl van het aantal
    '/ labe van het aantal wijst naar label van het prod id
    Dim Lbl As Label = Btn.Tag
   
    cCur_KnopGeg = ArrProdList.get(Lbl.Tag)

        cCur_KnopGeg.Aantal =  1 '<-- this resets everything back to 1 also items added to the verkooplist
     
        cCur_KnopGeg.ItemData = Lbl '// <--- de verwijzing naar het product op het scherm om de teller te kunenn resetten
       
        Mod_Common.AddToVerkoopList(cCur_KnopGeg)
   
End Sub

I use a code module with
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    ...
    Type ProductGegevens(Id As Long, Naam As String, Aantal As Int, Prijs As Int, SuppLst As String, ItemData As Object)

    Public VerkoopList As List

End Sub

Sub AddToVerkoopList(Gevr_ProdGeg As ProductGegevens)
Dim prodteller As Int

Dim Gevonden As Boolean
For i = 0 To VerkoopList.Size-1
    Dim CurProd As ProductGegevens ' <-- moved this in the for-next loop
    CurProd = VerkoopList.Get(i)
    If Gevr_ProdGeg.Naam = CurProd.Naam Then '// dus exact het zelfde
        CurProd.Aantal = CurProd.Aantal + Gevr_ProdGeg.Aantal
        VerkoopList.Set(i,CurProd)
        Gevonden = True
        Exit
    End If
Next
If Gevonden = False Then VerkoopList.Add(Gevr_ProdGeg)

It is solver by declaring a second type with the same structure

B4X:
    Type ProductGegevens(Id As Long, Naam As String, Aantal As Int, Prijs As Int, SuppLst As String, ItemData As Object)
    Type Verkoop(Id As Long, Naam As String, Aantal As Int, Prijs As Int, SuppLst As String, ItemData As Object)

B4X:
Sub AddToVerkoopList(Gevr_ProdGeg As ProductGegevens)
Dim prodteller As Int
    'CurProd.Initialize

Dim Gevonden As Boolean
For i = 0 To VerkoopList.Size-1
   
    Dim CurProd As Verkoop '// een andere type(maar wel de zelfde opbouw)
   
    CurProd = VerkoopList.Get(i)
    If Gevr_ProdGeg.Naam = CurProd.Naam Then '// dus exact het zelfde
        CurProd.Aantal = CurProd.Aantal + Gevr_ProdGeg.Aantal'Gevr_ProdGeg.Aantal
        VerkoopList.Set(i,CurProd)
        Gevonden = True
        Exit
    End If
Next

If Gevonden = False Then 
    Dim CurProd As Verkoop
    CurProd.Aantal = Gevr_ProdGeg.Aantal
    CurProd.Id = Gevr_ProdGeg.Id
    CurProd.ItemData = Gevr_ProdGeg.ItemData
    CurProd.Naam = Gevr_ProdGeg.Naam
    CurProd.Prijs = Gevr_ProdGeg.Prijs
    CurProd.SuppLst = Gevr_ProdGeg.SuppLst
    VerkoopList.Add(CurProd)
End If

End Sub

This was a brain breaker for me.

Thank you
 
Upvote 0
Top