Page 1 of 2

All my arrays are read-only

Posted: Monday 2nd December 2019 11:23pm
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

Re: All my arrays are read-only

Posted: Tuesday 3rd December 2019 12:41am
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

Re: All my arrays are read-only

Posted: Tuesday 3rd December 2019 11:42am
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...

Re: All my arrays are read-only

Posted: Tuesday 3rd December 2019 12:23pm
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-)

Re: All my arrays are read-only

Posted: Tuesday 3rd December 2019 12:28pm
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.

Re: All my arrays are read-only

Posted: Tuesday 3rd December 2019 12:43pm
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.

Re: All my arrays are read-only

Posted: Tuesday 3rd December 2019 4:46pm
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

Re: All my arrays are read-only

Posted: Wednesday 4th December 2019 4:05pm
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.

Re: All my arrays are read-only

Posted: Wednesday 4th December 2019 5:39pm
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

Re: All my arrays are read-only

Posted: Wednesday 4th December 2019 6:05pm
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