read only array help
Posted
#1
(In Topic #353)
Enthusiast

This array is a global array inside a module. I need it to be read/write. I tried putting new in there but that just produces an error about mixing embedded arrays and new. I feel stupid not being able to figure this issue out. Please help.
public A[300] as string
Posted
Regular

sadams54 said
I keep ending up with a read only array. below is the line…
public A[300] as string
You must Declare & Instantiate an array, either in one go like this:-
…or separately like this:-
I hope this helps.
Posted
Enthusiast

Public myArray As New String[300]
Nice try but the above is the same thing I tried. You can't put new in a global declaration. And the array bounds has to be at the array not the type declaration so this produces either a mix error or a syntax error.
…or separately like this:-
Public myArray As String[]
…
Public Sub Whatever()
myArray = New String[300]
This also produces a syntax error. so no help.
Posted
Regular

Can you post your code so I can see the problem in context?
I can do this in a module:-
…and then do this in the main form:-
Posted
Enthusiast

Posted
Regular

sadams54 said
… your syntax produces errors as I mentioned above.
OK, perhaps someone else can run my code example to help narrow down your problem.
Info extract from my Gambas menu Help > System Information:-
[System]
Gambas=3.14.2
OperatingSystem=Linux
Kernel=4.15.0-72-generic
Architecture=x86_64
Distribution=Peppermint 9 Nine
Desktop=LXDE
Theme=Gtk
Language=en_GB.UTF-8
Memory=7810M
Posted
Guru

Posted
Enthusiast

Public A[300] As String
Sub ClearA()
Do Stuff
That is the section of code. It becomes a read only array and I need read write. If I put the "[300]" after string I get syntax error. If I put try "public A as new string[300]" I got No issues on one fedora 30 system and a can't mix new and embedded array on a fedora 31 system. OK seems like it may be a version issue. Now what?
Posted
Regular

sadams54 said
…No issues on one fedora 30 system and a can't mix new and embedded array on a fedora 31 system. OK seems like it may be a version issue. Now what?
What versions of Gambas are you using? I ask this because I notice from the Gambas release notes for V3.14.1 it says:-
"Read-only arrays are now really read-only."
…which may indicate why you see a difference.
It looks to me as if this declaration method is now useless, because it creates a read-only array, with no obvious way to set values. I tried something like this:-
Which proves that an array is created, as I get an out-of-bounds error on the last statement only, and the object inspector shows that X has 0-299 blank strings.
I think the way forward is to use array declarations similar to the ones in my earlier post, and then try to workout why these are not working for you.
You may find some of the old discussions on Nable of interest (…although they send me to sleep!) but be careful as some comments seem to relate to Gambas2: http://gambas.8142.n7.nabble.com/About-help-and-arrays-and-variables-td27589.html
Posted
Guru

Public A[300] As String
This declaration if for embedded arrays only and they are not recommended see quote below from the Gambas Help files.
Embedded arrays were created to ease the interface between Gambas and external functions located in shared libraries.
Consequently, I strongly suggest to use them only if you cannot use normal arrays.
I thought I would load Fedora 31 and I have done that. Gambas installs but won't run, that is to say I can't get it to run. So I can't test your points about this Distro.
Can you download and run my program 15 Puzzle Game which is available here or on the Gambas Farm? This has plenty of arrays in it.
Posted
Enthusiast

1 guest and 0 members have just viewed this.


