<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
On 6/8/19 08:05, <a class="moz-txt-link-abbreviated" href="mailto:vuott@tiscali.it">vuott@tiscali.it</a> wrote:<br>
<blockquote type="cite"
cite="mid:92a29dd9d3b64c34e30616dbb7a58b1b@tiscali.it">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div>Hello,</div>
<div>I don’t understand why at the second reading of a Structure
array I always get the value of the last element.</div>
<div>
<pre class="moz-quote-pre" wrap=""><code class="hljs php"><span class="hljs-keyword">Public</span> Struct AAAA
b <span class="hljs-keyword">As</span> Byte
c <span class="hljs-keyword">As</span> Short
i <span class="hljs-keyword">As</span> Integer
End Struct
Public Sub Main()
Dim a <span class="hljs-keyword">As</span> <span class="hljs-keyword">New</span> AAAA
Dim aa <span class="hljs-keyword">As</span> <span class="hljs-keyword">New</span> AAAA[]
Dim i <span class="hljs-keyword">As</span> Integer
<span class="hljs-keyword">For</span> i = <span class="hljs-number">0</span> To <span class="hljs-number">7</span>
aa.Push(a)
Valorizza(i, aa[i])
<span class="hljs-keyword">Print</span> aa[i].b, <span class="hljs-string"> ' first</span>
<span class="hljs-string"> Next</span>
<span class="hljs-string">Print</span>
<span class="hljs-string">Print</span>
<span class="hljs-string"> For i = 0 To 7</span>
<span class="hljs-string"> Print aa[i].b, '</span> second
Next
End
Private Procedure Valorizza(c <span class="hljs-keyword">As</span> Integer, s <span class="hljs-keyword">As</span> AAAA)
s.b = <span class="hljs-number">4</span> * c
End</code>
</pre>
</div>
</blockquote>
Looks like all the structs in the array refer (i.e: they're
pointers) to the same 'a' struct in the first DIM.<br>
Therefore, when you change the value in ANY element, you end up
changing it in all elements just because they're all pointing to the
same original 'a'.<br>
<br>
Change the first DIM to this:<br>
<pre><code class="hljs php"> Dim a <span class="hljs-keyword">As</span> AAAA
</code></pre>
and alter the first FOR as follows:<br>
<pre><code class="hljs php"> <span class="hljs-keyword">For</span> i = <span class="hljs-number">0</span> To <span class="hljs-number">7</span>
a = New AAAA 'New line. This makes 'a' point to a new, different structure in memory in each pass
aa.Push(a)
'continue your original code here
</code></pre>
That fixes it.<br>
<br>
zxMarce.<br>
</body>
</html>