Declaring Arrays

Post your Gambas programming questions here.
Post Reply
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Declaring Arrays

Post 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’ ?
Old african saying:
You eat an elephant one small bite at a time.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Declaring Arrays

Post 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.
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: Declaring Arrays

Post 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
Old african saying:
You eat an elephant one small bite at a time.
Post Reply