4D v13.4

Using the element zero of an array

Home

 
4D v13.4
Using the element zero of an array

Using the element zero of an array  


 

 

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. In ASCII compatibility mode, 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 CHARACTER SET, 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{Character code($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{Character code($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.

(*) However, there is one exception: in an array type List Box, the zero element is used internally to store the previous value of an element being edited, so it is not possible to use it in this particular context.

 
PROPERTIES 

Product: 4D
Theme: Arrays

 
SEE ALSO 

Arrays