read only array help

Post your Gambas programming questions here.
User avatar
sadams54
Posts: 139
Joined: Monday 9th July 2018 3:43am
Contact:

read only array help

Post by sadams54 »

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
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: read only array help

Post by stevedee »

sadams54 wrote: Sunday 22nd December 2019 6:52am 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:-
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.
User avatar
sadams54
Posts: 139
Joined: Monday 9th July 2018 3:43am
Contact:

Re: read only array help

Post by sadams54 »

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.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: read only array help

Post by stevedee »

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:-
' 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
User avatar
sadams54
Posts: 139
Joined: Monday 9th July 2018 3:43am
Contact:

Re: read only array help

Post by sadams54 »

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.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: read only array help

Post by stevedee »

sadams54 wrote: Monday 23rd December 2019 6:19am ... 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
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: read only array help

Post by cogier »

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.
User avatar
sadams54
Posts: 139
Joined: Monday 9th July 2018 3:43am
Contact:

Re: read only array help

Post by sadams54 »

' 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?
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: read only array help

Post by stevedee »

sadams54 wrote: Monday 23rd December 2019 7:24pm ...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:-
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
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: read only array help

Post by cogier »

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.
Post Reply