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
All my arrays are read-only
All my arrays are read-only
Retired 20-year USN veteran. In IT field since 1961.
Re: All my arrays are read-only
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
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.
Re: All my arrays are read-only
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...
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: All my arrays are read-only
I didn't know that one.One solution is to go to menu Project > Properties... > Options
...and then enable "Form controls are Public"
There is another way. There is a 'Public' property in the IDE you can change for individual controls
Going to get a cup of tea now...
Re: All my arrays are read-only
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
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
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.
Re: All my arrays are read-only
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
I think this is your issue:
https://lists.gambas-basic.org/pipermai ... 68238.html
Ced
https://lists.gambas-basic.org/pipermai ... 68238.html
Personally, I've only used embedded arrays inside of structures to pass to shared libraries.
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
Ced
.... and carry a big stick!
Re: All my arrays are read-only
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
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.