[Gambas-user] Object Array problem

Sebi sebikul at ...626...
Fri Nov 9 15:43:51 CET 2012


IMO, the problem is that you are just updating the values of dataset, and then adding it to the array. All items point to the same object, and end up having the value of the last update. You should create a new instance of the structure in each loop. 
-----Original Message-----
From: wally <wally at ...2037...>
Date: Fri, 09 Nov 2012 15:36:40 
To: <Gambas-user at lists.sourceforge.net>
Reply-To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
Subject: [Gambas-user] Object Array problem

What is bad in this code ?

' Gambas class file

Public Struct Tupel
  x As Integer
  y As Integer
End Struct
 
Public dataArr As New Tupel[]

Public Sub Button1_Click()

    Dim i As Integer
    Dim dataset As New Tupel
    
    For i = 0 To 9
        dataset.x = (i + 1)
        dataset.y = (i + 1) * 10
        dataArr.Add(dataset, 0)
        Print i, dataset.x, dataset.y
    Next
    
    Print "\n\n"
    
    For i = 0 To dataArr.Max
         
         Print i, dataArr[i].x, dataArr[i].y
         
     Next

End

Output:
0       1       10
1       2       20
2       3       30
3       4       40
4       5       50
5       6       60
6       7       70
7       8       80
8       9       90
9       10      100



0       10      100
1       10      100
2       10      100
3       10      100
4       10      100
5       10      100
6       10      100
7       10      100
8       10      100
9       10      100


project attached
thx wally






More information about the User mailing list