Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

配列とは異なり、コレクションでは索引によるキーは必要ありません。この例では、社員のコレクションは番号ではなく#Surnameフィールドがキーになっている点を除くと例1と同じです。

社員の名前を入力し、保存ボタンをクリックすると、社員名がコレクションに保存されます。

Image RemovedImage Added

表示ボタンをクリックすると、コレクションに入力されたすべての社員がメッセージ・ボックスに表示されます。

Image RemovedImage Added

コレクションのキーは、#SurNameで一意であるため、重複する名前はコレクション内の既存のアイテムを更新します。

コレクションの定義

社員名を保存するコレクションは、以下のように定義されます。

     Define_

...

Com Class(#Prim_

...

kCol<#GiveName #SurName>) Name(#Employee)
  • コレクションの名前は、#Employee

  • 社員の#GiveName(名前)を収集

  • キーは、一意の社員の#SurName(名字)

例2:索引によるキーが必要ないコレクション

コレクションへのアイテムの追加

Image RemovedImage Added

社員の名前全体をコレクションに保存するコードは、保存ボタンのClickイベントに置かれます。

...

     Set Com(#Employee<#SurName>)

...

 Value(#GiveName)

上の例では、コレクションは#Surnameフィールドでキーが付けられているので、例1のように次の使用可能なコレクション・アイテムのキーを計算する必要がありません。

例2:索引によるキーが必要ないコレクション

コレクション・アイテムの操作

Image RemovedImage Added

コレクション内の各アイテムには、For/EndForループを使用してアクセスします。

表示ボタンのClickイベントは、各社員の#GiveNameと#Surnameの値をメッセージ・ボックスに取得するFor/EndForループを使用してコレクション内の各エントリーの参照を繰り返します。

...

     For Each(#Employee_GiveName)

...

 In(#Employee)

...

 Key(#Employee_SurName)

...

     Use Builtin(MESSAGE_BOX_ADD)

...

 With_Args('Employee'

...

 #EMPLOYEE_GIVENAME.

...

VALUE #EMPLOYEE_SURNAME.

...

VALUE 'was found in the collection.')
Endfor

上の例では、以下のことに注意してください。

  • For/EndForループは、変数の名前(#Employee_GiveName)が参照されるたびにキーの名前(#Employee_SurName)を指定して現在のコンポーネントのキーにアクセスします。この方法で、#Surnameフィールドの値がメッセージに取得できます。

例2:索引によるキーが必要ないコレクション

コレクションの例2のソース・コード

コレクションの作成方法を見るには、このソースをコピーしてフォームに貼り付け、フォームをコンパイルして実行してください。

...

     Function Options(*DIRECT)
Begin_Com Role(*EXTENDS #PRIM_FORM) Clientheight(124) Clientwidth(363) Height(151) Left(269) Top(185) Visualstyle(#VS_NORM) Width(371)
* Using a Keyed Collection (PRIM_KCOL) as a keyed array.

...

     * Define the keyed collection to be used to store employee names.
* The collection is named #Employee.
* It collects an employee's #GiveName (given or first name).
* It is uniquely keyed an employee's #SurName (last name or surname).
Define_Com Class(#Prim_kCol<#GiveName #SurName>) Name(#Employee)

...

     * Define the form.
* Field #GIVENAME allows the employee's given/first name to be input.
* Field #SURNAME  allows the employee's last or surname to be input.
* Button #PHBN_SAVE saves employee names into the collection.
* Button #PHBN_SHOW shows the details of all the employees in the collection.
Define_Com Class(#GiveName.Visual) Name(#GiveName) Displayposition(2) Height(19) Left(16) Parent(#COM_OWNER) Tabposition(1) Top(16) Width(329)
Define_Com Class(#SurName.Visual) Name(#SurName) Displayposition(1) Height(19) Left(16) Parent(#COM_OWNER) Tabposition(2) Top(40) Width(329)
Define_Com Class(#PRIM_PHBN) Name(#PHBN_SAVE) Buttondefault(True) Caption('Save ') Displayposition(3) Left(152) Parent(#COM_OWNER) Tabposition(3) Top(80) Width(97)
Define_Com Class(#PRIM_PHBN) Name(#PHBN_SHOW) Caption('Show ') Displayposition(4) Left(264) Parent(#COM_OWNER) Tabposition(4) Top(80)
* -------------------------------------------------------------------------

...

     * #PHBN_SAVE.

...

Click : Save the detail on an employee in the keyed collection
* -------------------------------------------------------------------------
Evtroutine Handling(#PHBN_SAVE.Click)
* Save the employee's name into the keyed collection.

...

     * The collection is uniquely keyed by #SurName so a duplicate
* name will update the existing item in the collection. Try putting
* in 2 employees with the same surname (eg: JONES).
Set Com(#Employee<#SurName>) Value(#GiveName)

...

     * Clear the name fields ready for input of the next name
* and reset the focus to the first name field
Change Field(#GIVENAME #SURNAME) To(*NULL)
Invoke Method(#GiveName.SetFocus)
Endroutine
*

...

 -------------------------------------------------------------

...

     * #PHBN_SHOW.

...

Click : Build a message of all the employees saved
* -------------------------------------------------------------
Evtroutine Handling(#PHBN_SHOW.Click)

...

     * Iterate through the collection referencing each entry in the
* collection as an object by using a for / end for loop.
For Each(#Employee_GiveName)

...

 In(#Employee)

...

 Key(#Employee_SurName)

...

     Use Builtin(MESSAGE_BOX_ADD)

...

 With_Args('Employee'

...

 #EMPLOYEE_GIVENAME.

...

VALUE #EMPLOYEE_SURNAME.

...

VALUE 'was found in the collection.')
Endfor
* Show the employee count and final results

...

  
Use Builtin(MESSAGE_BOX_ADD)

...

 With_Args('

...

There are' #EMPLOYEE.ITEMCOUNT 'employees currently in the collection.')

...

     Use Builtin(MESSAGE_BOX_SHOW)

...

 With_Args(

...

OK OK INFO *COMPONENT)
Endroutine

...


End_Com

...

     Example 2: Collections do not Need to Be Keyed by Index