gruizelgruis Member Licensed User Longtime User Apr 22, 2015 #1 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: Code: Copy 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 ?
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: Code: Copy 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 Apr 22, 2015 #2 Code: Copy Type ProductGegevens(Id As Long, Naam As String,Aantal As Int, Prijs As Int, SuppLst As String, ItemData As Object) Upvote 0
Code: Copy Type ProductGegevens(Id As Long, Naam As String,Aantal As Int, Prijs As Int, SuppLst As String, ItemData As Object)
gruizelgruis Member Licensed User Longtime User Apr 23, 2015 #3 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) Code: Copy cCur_KnopGeg.Aantal = 1 '<-- this sets the ' VerkoopList.aantal aswell. It looks like its connected. Mod_Common.AddToVerkoopList(cCur_KnopGeg) PLease advice Upvote 0
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) Code: Copy cCur_KnopGeg.Aantal = 1 '<-- this sets the ' VerkoopList.aantal aswell. It looks like its connected. Mod_Common.AddToVerkoopList(cCur_KnopGeg) PLease advice
Erel B4X founder Staff member Licensed User Longtime User Apr 23, 2015 #4 Can you post a full example? Might be related to the common mistake explained here: https://www.b4x.com/android/forum/threads/variables-objects-in-basic4android.8385/#content Upvote 0
Can you post a full example? Might be related to the common mistake explained here: https://www.b4x.com/android/forum/threads/variables-objects-in-basic4android.8385/#content
gruizelgruis Member Licensed User Longtime User Apr 23, 2015 #5 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 Code: Copy 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 Code: Copy 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 Code: Copy 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) Code: Copy 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
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 Code: Copy 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 Code: Copy 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 Code: Copy 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) Code: Copy 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
Erel B4X founder Staff member Licensed User Longtime User Apr 24, 2015 #6 You don't need to declare the same type structure twice. Just make sure that you "dim" the variable when you want to create a new instance. Upvote 0
You don't need to declare the same type structure twice. Just make sure that you "dim" the variable when you want to create a new instance.