All my arrays are read-only

Post your Gambas programming questions here.
User avatar
issboss
Posts: 32
Joined: Wednesday 16th October 2019 6:20pm
Location: Ohio, USA
Contact:

All my arrays are read-only

Post by issboss »

I've been working on a database project and have come across something I can't solve.

I have declared (in a separate module) the following: Public arrName[4000] As String

In my Main form coding, I can reference this array just fine. The program compiles and runs with no error except when I try to put anything INTO the array. I always get an error telling me the array is Read-Only.

Is there something I have to do to declare them read/write? The documentation on arrays and their declarations is very confusing. I've tried all sorts of things like making them STATIC, and putting them in a Class instead of a module, but they all give me some sort of error when I try to use them.

The ultimate goal is to be able to access the main array system from any module or form. In VB6 we could use the Global keyword. I thought that in Gambas, the keyword "Public" made it available to everything. It does, but read-only?

Bill
Retired 20-year USN veteran. In IT field since 1961.
User avatar
issboss
Posts: 32
Joined: Wednesday 16th October 2019 6:20pm
Location: Ohio, USA
Contact:

Re: All my arrays are read-only

Post by issboss »

Ok. I've moved everything into a single Main module. The arrays are STILL read only at runtime.


EDIT: (two hours later). I seem to have found the answer. I added a "new" following the "As" and now I can refer to them as both read and write.

Now, if I can just figure out how to refer to a textbox on the main form from another form's code, I'll be happy. "frmMain.txtWhatever.text" causes a "don't know what this is" error when executed from another form's code.

Bill
Retired 20-year USN veteran. In IT field since 1961.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: All my arrays are read-only

Post by stevedee »

issboss wrote: Tuesday 3rd December 2019 12:41am...Now, if I can just figure out how to refer to a textbox on the main form from another form's code, I'll be happy....
Yes, that is because form controls are Private by default.

One solution is to go to menu Project > Properties... > Options
...and then enable "Form controls are Public"

You should then be able to use...
FMain.TextBox1.Text = "Hello"
...from another form.

I think there is another way to do this, but will have to make myself a cuppa tea while I try to remember...
User avatar
cogier
Site Admin
Posts: 1117
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: All my arrays are read-only

Post by cogier »

One solution is to go to menu Project > Properties... > Options
...and then enable "Form controls are Public"
I didn't know that one. :shock:

There is another way. :arrow: There is a 'Public' property in the IDE you can change for individual controls

Image

Going to get a cup of tea now... 8-)
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: All my arrays are read-only

Post by stevedee »

stevedee wrote: Tuesday 3rd December 2019 11:42am ...I think there is another way to do this...
If you don't want to make your controls Public, just create a Public routine in the 1st form:-
Public Sub UpdateName(strName As String)
  
  txtName.text = strName  
  
End
...then call it from your 2nd form:-
Public Sub Button1_Click()

  FMain.UpdateName("Steve")

End
...which will update the textbox called txtName on the 1st form.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: All my arrays are read-only

Post by stevedee »

cogier wrote: Tuesday 3rd December 2019 12:23pm ...There is a 'Public' property in the IDE you can change for individual controls...
Nice one Charlie, I hadn't spotted that.


I guess if we were bothered about OOP and the concept of encapsulation, then keeping all controls Private and just having Public routines to Get & Set values would be the way to go. The great thing about programming for personal use is that you can please yourself.
User avatar
issboss
Posts: 32
Joined: Wednesday 16th October 2019 6:20pm
Location: Ohio, USA
Contact:

Re: All my arrays are read-only

Post by issboss »

stevedee wrote: Tuesday 3rd December 2019 12:43pm . . . . The great thing about programming for personal use is that you can please yourself.
How right you are, Stevedee.

I went over all the switches in the project properties and never once tumbled to the "make controls public". I bet I read it 10 times. What I'm doing is converting a rather massive DB program I wrote in VB6 into Gambas. Slow going, though. The similarities are what turns out to be the toughest to translate. Go figure.

When/if I get it finished, I'll see about putting it up in the Farm as a Multimedia Database for DVD's, vinyl/reel-to-reel tapes and records, and other items that fit the generic interface.

Thanks to you both.

Bill
Retired 20-year USN veteran. In IT field since 1961.
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: All my arrays are read-only

Post by jornmo »

I believe I’ve read somewhere that Steve’s method is the recommended way, and that setting everything public should be avoided as far as possible. Using properties is also something to consider.
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: All my arrays are read-only

Post by Cedron »

I think this is your issue:

https://lists.gambas-basic.org/pipermai ... 68238.html

You can work around this bug right now by turning your embedded array
declaration

Public sortname_LOGO[9, 2] As String

into a real array

Public sortname_LOGO As New String[9, 2]

And why are you using embedded arrays in the first place? Normal arrays
have many advantages over embedded arrays, such as:

- they can grow and shrink dynamically,
- their lifetime is not bound to the containing object,
- they can be local variables.

The only virtue of embedded arrays is that they are placed compactly
inside the containing object, making them suitable to interact with
native libraries. If you are not doing that, embedded arrays should
be avoided. There is no reason to use them. Although this advice has
nothing to do with this particular bug here.

Regards,
Tobi
Personally, I've only used embedded arrays inside of structures to pass to shared libraries.

Ced
.... and carry a big stick!
User avatar
issboss
Posts: 32
Joined: Wednesday 16th October 2019 6:20pm
Location: Ohio, USA
Contact:

Re: All my arrays are read-only

Post by issboss »

I read the Wiki several more times before I tumbled onto Real arrays and changed my definitions to ones like this:

Public arrName as New String[intMaxSize] (Where intMaxSize was set at the beginning as an Integer Constant = 4000)

They are now performing very well for me--reachable from every module. My immediate goal is to transcribe the VB6 program and make it workable in Gambas, THEN go back and make it more efficient. Along the way I've been learning a great deal about Gambas and how it works. In many ways better then VB.

Bill
Retired 20-year USN veteran. In IT field since 1961.
Post Reply