User Tools

Site Tools


7.4.5 Inline array

With the inline notation of an array, you can create an array in a compact way and fill it with data immediately.

Syntax for an inline array:

Array = [ Expression [ , ... ] ]

The statement creates an array and returns it. The data type of all elements is checked. If they are all of the same type, or if they can be converted to the same native data type, then an array of a specific type is returned. For example, string[] if all elements are only string values, float[] for floating point numbers and so on…. . Otherwise, a variant[] array is returned. You create an array within an expression with the[….] operator.

The next 2 lines can be read like this:

Dim aCharactersArray As String[]
aCharactersArray = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"]

The declared array' aCharactersArray' is assigned the inline array[“I”,“IV”,…,“M”]. It has been declared anonymously or as it is called in the Gambas documentation: inline (? The Inline array[“I”,“IV”,…,“M”] is a full-fledged array - but without name and can be used like a normal array.


The array[“I”,“IV”,…,“M”] is an inline array - but not an embedded array, although both terms are almost the same. The “Inline” refers to the way in which the array is written. For embedded arrays,“Embedded” refers to the position of the array in memory. Further information can be found in chapter 7.4.6 Embedded Array. Example 1

In the first project an inline array with 5 elements is specified, all of which have a different data type:

Dim aArray As Array
Dim vElement As Variant
Dim iCount As Integer

aArray = ["Adam", 44, True, Sqr(9), Year(Now)]
Print "Typ of aArray = "; Object.Type(aArray)

For Each vElement In aArray
  Print vElement

Print "Alternative output via an index:"
For iCount = 0 To aArray.Max
  Print aArray[iCount]

Output in the IDE console:

Typ of aArray = Variant[]


The alternative output via an index provides the same result. Example 2

The join(…)-method is used for the inline array[“A”,“B”,“C”]:

PRINT ["A", "B", "C"].Join("/")

Output: A/B/C Example 3

The two inline arrays are used in the following source code:

[[“I”,“IV”,“V”,“IX”,“X”,“XL”,“L”,“XC”,“C”,“CD”,“D”,“CM”,“M”] [1,4,5,9,10,40,50,90,100,400,500,900,1000]

and are assigned in the lines (1*) and (2*) to the declared arrays 'aCharactersArray' and 'aNumbersArray' in different ways:

Function IntegerNumberToNumberCharacter(iIntegerNumber As Long) As String
  Dim aCharacterArray As String[]
  Dim aNumbersArray As New Integer[]
  Dim iCount As Integer
  Dim sNumberCharacter As String
' Use of inline arrays
  aCharacterArray = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"]   *
  aNumbersArray.Insert([1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000])              **
  For iCount = 12 To 0 Step -1
    While (iIntegerNumber >= aNumberArray[iCount])
      iIntegerNumber = iIntegerNumber - aNumbersArray[iCount]
      sNumberCharacter = sNumberCharacter & aCharacterArray[iCount]
  Next ' iCount
  Return sNumberCharacter
End ' NumberToNumbersign → Conversion according to an approach by Carlos Alberto Longen Example 4

Dim a2DArrayNames As New String[10, 5]
For i = 0 To a2DArrayNames.Bounds[0] - 1
  For j = 0 To a2DArrayNames.Bounds[1] - 1
    a2DArrayNames[i, j] = ["Adam", "Bruno", "Carina", "Detlef", "Erwin"][Rnd(0, 5)]      ★★★
  Next ' j
Next ' i

The string array is the names:

[“Adam”,“ Bruno”,“Carina”,“Detlef”,“Erwin”]

From this inline array, an element is selected with a random index in the range of 0 to 4 (!) since the conversion to an integer value always truncates all decimals. The line (3*) thus assigns a random name to an element of the native multidimensional array 'a2DArrayNames' addressed by the i,j-coordinates.

Here follows the complete source code of the procedure from which the above-mentioned adapted source code section was taken:

Public Sub btnShow2D_Click()
  Dim i, j, k As Integer
  Dim a2DArrayNames As New String[][]
  Dim myArray As New String[]
  For i = 0 To 9
    a2DArrayNames[i] = New String[5]
    For j = 0 To 4
      a2DArrayNames[i][j] = ["Adam", "Bruno", "Carina", "Detlef", "Erwin"][Rnd(0, 5)]
' Structured output in the console:
  For Each myArray In a2DArrayNames
    For j = 0 To myArray.Max
        If (j + 1) Mod (myArray.Max + 1) = 0 Then
           Print myArray[j]
          Print myArray[j],
' Alternative
  For Each myArray In a2DArrayNames
    For j = 0 To myArray.Max
      Print myArray[j],
      If (j + 1) Mod (myArray.Max + 1) = 0 Then Print
    Next ' j
  Next ' myArray

Structured output in the IDE console:

Carina  Adam    Adam    Detlef  Detlef
Adam    Erwin   Adam    Bruno   Bruno
Erwin   Erwin   Detlef  Carina  Adam
Carina  Detlef  Adam    Detlef  Carina
Adam    Erwin   Detlef  Bruno   Erwin
Detlef  Detlef  Detlef  Bruno   Erwin
Erwin   Erwin   Bruno   Erwin   Erwin
Erwin   Carina  Erwin   Carina  Detlef
Bruno   Adam    Detlef  Detlef  Bruno
Adam    Detlef  Detlef  Carina  Adam Example 5

The simplest way to read data from a process (–> chapter 21.3.1 Using Quick syntax) is to use the short form of the EXEC instruction - the so-called Quick syntax:

EXEC  aCommand TO (String-)Variable ' aCommand is a (String-)Array
Public Sub btnPingOverExec_Click()
 Dim sEdition As String
  FMain.Mouse = Mouse.Wait
  EXEC [sProgramName, TextBox1.Text, "-c", "4"] To sEdition ' String array as inline array
  TextArea.Insert(gb.NewLine & sEdition)
  FMain.Mouse = Mouse.Default
End ' btnPingOverExec_Click Example 6

The following example shows how the use of inline arrays can considerably increase clarity in the source code. The individual variants require the German month names or the days of the week for a specific date.

1st variant

  Dim dDate as Date
  Dim sCurrentMonth as String

  IF Month(dDate) = 12 THEN sCurrentMonth = "December".
  IF Month(dDate) = 11 THEN sCurrentMonth = "November".
  IF Month(dDate) = 10 THEN sCurrentMonth = "October".
  IF Month(dDate) = 9 THEN sCurrentMonth = "September".
  IF Month(dDate) = 8 THEN sCurrentMonth = "August".
  IF Month(dDate) = 7 THEN sCurrentMonth = "July".
  IF Month(dDate) = 6 THEN sCurrentMonth = "June".
  IF Month(dDate) = 5 THEN sCurrentMonth = "May".
  IF Month(dDate) = 4 THEN sCurrentMonth = "April".
  IF Month(dDate) = 3 THEN sCurrentMonth = "March".
  IF Month(dDate) = 2 THEN sCurrentMonth = "February".
  IF Month(dDate) = 1 THEN sCurrentMonth = "January"

2nd variant

  Public Function GetMonth(dDate) As String
    Select Case Month(dDate)
      Case 1
        Return "January"
      Case 2
        Return "February"
      Case 12
        Return "December"
    End Select

  sCurrentMonth = GetMonth(Now())

3. variant - short and concise:

  Dim aMonth As String[]

  aMonths = ["January", "February", "March", "April", ..., "October", "November", "December"].
  sCurrentMonth = aMonths[Month(dDate)-1]

The following is an analogous example of displaying the weekday in a TextBox for a date selected via the DateChooser component:

  aWeekDays = ["Sunday", "Monday", "Tuesday", ..., "Thursday", "Friday", "Saturday"]
  txbDateDayOfWeek.Text = aWeekDays[WeekDay(DateChooser1.Value)] ' Sunday = 0! Example 7

Here, an inline array is already used for array declaration:

Dim aNames As String[] = ["Eagle", "Bear", "Badger", "Fox", "Tit", "Eagle Owl", "Moose"] Example 8

To insert data into a matrix, you can also use inline arrays, three of which are used in the following section of the source code:

  Dim hMatrix As Matrix

  hMatrix = New Matrix(3, 5, False) ' Matrix of 3 rows with 5 columns, real numbers

' Fill matrix with values

  hMatrix.SetRow(0, [11, 12, 13, 14, 15]) ' Elements here only of type Float
  hMatrix.SetRow(1, [21, 22, 23, 24, 25])
  hMatrix.SetRow(2, [31, 32, 33, 34, 35]) Example 9

Class.New (gb) instantiates a class:

Function New ( [ Arguments As Array ] ) As Object

This routine works exactly like the NEW operator except that the constructor arguments must be specified by an inline array.

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k7/k7.4/k7.4.5/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools