I keep ending up with a read only array. below is the line
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
read only array help
Re: read only array help
You must Declare & Instantiate an array, either in one go like this:-
Public myArray As New String[300]
...or separately like this:-
Public myArray As String[]
...
Public Sub Whatever()
myArray = New String[300]
I hope this helps.Re: read only array help
You must Declare & Instantiate an array, either in one go like this:-
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.
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.
Re: read only array help
I guess I'm missing your point, as I can declare a Public array in a code module.
Can you post your code so I can see the problem in context?
I can do this in a module:-
Can you post your code so I can see the problem in context?
I can do this in a module:-
' Gambas module file
Public myarray1 As String[]
Public myarray2 As New String[5]
Public Function LoadArray() As String
myarray1 = New String[10]
myarray1[1] = "Hello"
Return myarray1[1]
End
...and then do this in the main form:-' Gambas class file
Public Sub Button1_Click()
Me.Text = Module1.LoadArray()
Module1.myarray2[1] = Me.Text
Button1.Text = Module1.myarray2[1]
End
Re: read only array help
the context you provided is correct. I declare it outside the subs and functions at the module level. However your syntax produces errors as I mentioned above.
Re: read only array help
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
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: read only array help
I have tried the stevedee code and I had no problems, it worked as expected. I suggest you upload an example so we can have a look.
Re: read only array help
' Gambas module file
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?
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?
Re: read only array help
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:-
Public A[300] As String
Public Sub Button1_Click()
Label1.Text = X[9]
Label2.Text = X[399]
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- ... 27589.html
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: read only array help
This declaration if for embedded arrays only and they are not recommended see quote below from the Gambas Help files.Public A[300] As String
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.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.
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.