Using the element zero of an array

4D - Documentation   Français   English   German   4th Dimension 2004, Command Theme List   4th Dimension 2004, Command Alphabetical List   4th Dimension 2004, Constant Theme List   Back   Previous   Next

version 6.0


An array always has an element zero. While element zero is not shown when an array supports a form object, there is no restriction in using it with the language.

One example of the use of element zero is the case of the combo box discussed in the section Arrays and Form Objects.

Here are two other examples.

1. If you want to execute an action only when you click on an element other than the previously selected element, you must keep track of each selected element. One way to do this is to use a process variable in which you maintain the element number of the selected element. Another way is to use the element zero of the array:

      ` atNames scrollable area object method
   Case of
      : (Form event=On Load)
            ` Initialize the array (as shown further above)
         ARRAY TEXT (atNames;5)
            ` ...
            ` Initialize the element zero with the number
            ` of the current selected element in its string form
            ` Here you start with no selected element
         atNames{0}:="0"

      : (Form event=On Unload)
            ` We no longer need the array
         CLEAR VARIABLE(atNames)

      : (Form event=On Clicked)
         If (atNames#0)
            If (atNames#Num(atNames{0}))
               vtInfo:="You clicked on: "+atNames{atNames}+" and it was not selected before."
               atNames{0}:=String(atNames)
            End if
         End if
      : (Form event=On Double Clicked)
         If (atNames#0)
            ALERT ("You double clicked on: "+atNames{atNames}
         End if
   End case

2. When sending or receiving a stream of characters to or from a document or a serial port, 4D provides a way to filter ASCII codes between platforms and systems that use different ASCII maps— the commands USE ASCII MAP, Mac to ISO, ISO to Mac, Mac to Win and Win to Mac.

In certain cases, you might want to fully control the way ASCII codes are translated. One way to do this is to use an Integer array of 255 elements, where the Nth element is set to the translated ASCII code for the character whose source ASCII code is N. For example, if the ASCII code #187 must be translated as #156, you would write <>aiCustomOutMap{187}:=156 and <>aiCustomInMap{156}:=187 in the method that initializes the interprocess arrays used everywhere in the database. You can then send a stream of characters with the following custom project method:

      ` X SEND PACKET ( Text { ; Time } )
   For ($vlChar;1;Length($1))
      $1[[vlChar]]:=Char(<>aiCustomOutMap{Ascii($1[[vlChar]])})
   End for
   If (Count parameters>=2)
      SEND PACKET ($2;$1)
   Else
      SEND PACKET ($1)
   End if
      ` X Receive packet ( Text { ; Time } ) -> Text
   If (Count parameters>=2)
      RECEIVE PACKET ($2;$1)
   Else
      RECEIVE PACKET ($1)
   End if
   $0:=$1
   For ($vlChar;1;Length($1))
      $0[[vlChar]]:=Char(<>aiCustomInMap{Ascii($0[[vlChar]])})
   End for

In this advanced example, if a stream of characters containing NULL characters (ASCII code zero) is sent or received, the zero element of the arrays <>aiCustomOutMap and <>aiCustomInMap will play its role as any other element of the 255 element arrays.

See Also

Arrays.


4D - Documentation   Français   English   German   4th Dimension 2004, Command Theme List   4th Dimension 2004, Command Alphabetical List   4th Dimension 2004, Constant Theme List   Back   Previous   Next