Page 1 of 1

Declaring Arrays

Posted: Tuesday 2nd April 2019 12:38pm
by Doctor Watson
Some beginners enigma :?
I can’t find how and where to declare Arrays.
I think this should be done in the Project’s first form : Fmain
When I try to run for example in Fmain.Class :

Public Sub Form_Open()
Dim A[50] AS String
End

I get : “Embedded arrays are forbidden here in Fmain.Class”
And I get the same notification wherever I try.
In the Beginners Guide I read “Arrays are always initialized as void at startup”
So, where is ‘Startup’ ?

Re: Declaring Arrays

Posted: Tuesday 2nd April 2019 1:43pm
by cogier
Hi Doctor!
Here is a start for you. This small program will run, it will find all the 'hidden' files in your Home folder: -
' Gambas class file

iGlobalArray As New Integer[]                   'Global array

Public Sub Form_Open()
Dim sZ As New String[50, 2]                     'Creates a multi dimensional array
Dim sB As New String[50]                        'Creates an empty array of 50
Dim sD As New String[]                          'Creates an array that can be added to with .Add (see below)

Dim sA As String[] = ["A", "B", "C"]            'Creates a simple Array  
Dim sC As String[] = Dir(User.Home)             'Creates a new array with all the file names in you Home folder

Dim sTemp As String                             'Temp string

For Each sTemp In sC                            'For each file name in sC
  If sTemp Begins "." Then sD.Add(sTemp)        'If the file begins with "." then add it to sD
Next

Print sD.Join(" - ")                            'Print the array joined with " - "

End
The iGlobalArray can be used anywhere in this class. If you want to use it in another class add 'Public' to the beginning of the line.

The arrays in Public Sub Form_Open() can only be used in this procedure.
In the Beginners Guide I read “Arrays are always initialized as void at startup”
So, where is ‘Startup’ ?
This says that when you startup your program all arrays will be empty.

Re: Declaring Arrays

Posted: Wednesday 3rd April 2019 7:53am
by Doctor Watson
I think I'm getting there ... ;)
It will take some time as I did some programming - many years ago - in RealBasic. Gambas feels like it, but it's obviously not quite the same.
Old African proverb says: "One eats an elephant one little bit at a time"
Thanks