VBAndCs / sVB-Small-Visual-Basic

Small Visual Basic (sVB) is an educational programming language, created by Eng. Mohammad Hamdy as an evolved version of Microsoft Small Basic (SB). It is meant to be easier and more powerful at the same time, to introduce programming basics to kids and beginners of any age, provided that they can use the English keyboard on the Windows OS.
Other
232 stars 16 forks source link

Inner array elements of a multi-dimensional array seems immutable. I can't change it directly. #70

Closed dynamicboy closed 3 months ago

dynamicboy commented 3 months ago

version: sVB 3.0.7.4 details: Inner array elements of a multi-dimensional array seems immutable. I can't change it directly. code:

A = {{1, 2, 3}, {100, 200, 300}}

ForEach ItemArr In A
   ItemArr[2] = 0
Next
GBLib.PrintArrays(A) ' not changed

' ======================================
ForEach ItemArr In A
   NewArr = {ItemArr[1], 0, ItemArr[3]}
   ItemArr = NewArr
Next
GBLib.PrintArrays(A) ' not changed

' ======================================
For I = 1 To A.Count
   CrtItemArr = A[I]
   CrtItemArr[2] = 0
Next
GBLib.PrintArrays(A) ' not changed

' ======================================
For I = 1 To A.Count
   CrtItemArr = A[I]
   NewArr = {CrtItemArr[1], 0, CrtItemArr[3]}
   A[I] = NewArr
Next
GBLib.PrintArrays(A) ' changed

screenshot: 2024-06-24_212114

VBAndCs commented 3 months ago

Hi @dynamicboy I've already answered this in your previous post. Array of SB and sVB behaves as value types not reference types, to make it easy for kids. So, each single operation on any array will create a new array, even when you change an item at any index! So, when you copy the inner array, every time tou change an item of it, you will get a totally different array, hence the original array is not changed. This is designed by the Small Basic compiler, so when you try this in MS Small Basic you will get the same result.

A[1][1] = 1
A[1][2] = 2
A[1][3] = 3
A[2][1] = 100
A[2][2] = 200
A[2][3] = 300

For i= 1 To 2
  ItemArr= a[i]
   ItemArr[2] = 0
endfor

To solve this, you should index the original array, or save the modified inner array back to it as yopu did in the last sample:

A = {{1, 2, 3}, {100, 200, 300}}

For I = 1 To 2
   A[I][2] = 0
Next

TW.WriteLine(A.ToStr())

Thanks.

dynamicboy commented 3 months ago

Understood, thank you! Additionally, the sVB reference book appears to be missing the File.WriteArray and File.ReadArray functions.

VBAndCs commented 3 months ago

Thanks for the hint. I will add these two methods to the next update of the book.


From: dynamicboy @.> Sent: Tuesday, June 25, 2024 9:25 AM To: VBAndCs/sVB-Small-Visual-Basic @.> Cc: Mohammad Hamdy Ghanem @.>; Comment @.> Subject: Re: [VBAndCs/sVB-Small-Visual-Basic] Inner array elements of a multi-dimensional array seems immutable. I can't change it directly. (Issue #70)

Understood, thank you! Additionally, the sVB reference book appears to be missing the File.WriteArray and File.ReadArray functions.

— Reply to this email directly, view it on GitHubhttps://github.com/VBAndCs/sVB-Small-Visual-Basic/issues/70#issuecomment-2188413894, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALQ5MVWVESL6DZJU3XYN5ULZJEZRLAVCNFSM6AAAAABJZ2PQY2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBYGQYTGOBZGQ. You are receiving this because you commented.Message ID: @.***>