read only array help

Post

Posted
Rating:
#1 (In Topic #353)
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
stevedee is in the usergroup ‘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:-

Code (gambas)

  1. Public myArray As New String[300]

…or separately like this:-

Code (gambas)

  1. Public myArray As String[]
  2. ...
  3.  
  4. Public Sub Whatever()
  5.  
  6.     myArray = New String[300]
  7.  
  8.  

I hope this helps.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
stevedee is in the usergroup ‘Regular’
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:-

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Public myarray1 As String[]
  4. Public myarray2 As New String[5]
  5.  
  6. Public Function LoadArray() As String
  7.  
  8.   myarray1 = New String[10]
  9.   myarray1[1] = "Hello"
  10.   Return myarray1[1]
  11.  

…and then do this in the main form:-

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public Sub Button1_Click()
  4.  
  5.   Me.Text = Module1.LoadArray()
  6.   Module1.myarray2[1] = Me.Text
  7.   Button1.Text = Module1.myarray2[1]
  8.  
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
stevedee is in the usergroup ‘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
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Online now: Yes Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
 ' 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?
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
stevedee is in the usergroup ‘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:-

Code (gambas)

  1.  
  2. Public Sub Button1_Click()
  3.  
  4.   Label1.Text = X[9]
  5.   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-help-and-arrays-and-variables-td27589.html
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Guru
cogier is in the usergroup ‘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.
Online now: Yes Back to the top

Post

Posted
Rating:
#11
Avatar
Enthusiast
sadams54 is in the usergroup ‘Enthusiast’
the problem is the update to the run time modules, it has changed how declarations work and also screwed up my printing ability. I now have months of work to fix this and some of it is critical
Online now: No Back to the top
1 guest and 0 members have just viewed this.