Collections is a set of objects of one or several types. Using collections enables the user to equally process each object included into collections. In the Fore language collections are implemented in the Collections.
if required, the proper collection can be created where properties or methods of basic collection can be redefined and all necessary features can be implemented.
Collections are processed by various cycles available in Fore.
Sub UserProc;
Var
Arr: IArrayList;
Begin
Arr := New ArrayList.Create;
// Add elements to collection
Arr.Add("A");
Arr.Add("D");
Arr.Add("G");
Arr.Add("C");
Arr.Add("B");
// View collection before some operations
Debug.WriteLine("---Before some operations---");
ShowCollection(Arr);
// Sorting
Arr.Sort(Comparer.StringComparer);
Debug.WriteLine("---After sorting---");
ShowCollection(Arr);
// Reverse order in collection
Arr.Reverse;
Debug.WriteLine("---Rotation in reverse order---");
ShowCollection(Arr);
// Search element in collection
Debug.WriteLine("Index of the G element:" + Arr.IndexOf("G").ToString);
End Sub UserProc;
Sub ShowCollection(Collection: IEnumerable);
Var
v: Variant;
Begin
For Each v In Collection Do
Debug.Write(v + " ");
End For;
Debug.WriteLine("");
End Sub ShowCollection;
Example of creating a proper collection that extends the ArrayList system collection. When elements are added to the collection, a unique GUID is generated for each element:
Sub UserProc;
Var
m: MyCollections;
v: Array;
i: Integer;
Begin
m := New MyCollections.Create;
// Add elements to collection
m.Add("First string");
m.Add("Second string");
m.Add(100500);
For i := 0 To m.Count - 1 Do
v := m.Item(i);
Debug.WriteLine("Value: " + v[0] + ". GUID: " + v[1]);
End For;
End Sub UserProc;
// Proper collection based on the ArrayList collection
Class MyCollections: ArrayList
_Array: IArrayList;
_Guid: IArrayList;
i: Integer;
Public Constructor Create;
Begin
_Array := New ArrayList.Create;
_Guid := New ArrayList.Create;
End Constructor Create;
// Redetermine necessary properties and methods
Public Function Add(Obj: Variant): Integer;
Begin
_Array.Add(Obj);
Return (_Guid.Add(GuidGenerator.Generate));
End Function Add;
Public Property Count: Integer
Get
Begin
Return _Array.Count;
End Get
End Property Count;
Public Property Item(Index: Integer): Array
Get
Var
_Arr: Array;
Begin
If Index < _Array.Count Then
_Arr := New Variant[2];
_Arr[0] := _Array.Item(Index);
_Arr[1] := _Guid.Item(Index);
Return _Arr;
Else
Return Null
End If;
End Get
End Property Item;
End Class MyCollections;
Various development environment assemblies implement the system collections that are used to work with certain groups of objects. Collection names contain "s" at the end. Specific collection elements are described by the interfaces, which names match with the collection name but do not contain "s" at the end. For example: IMetabaseObjectDescriptors - collection of descriptions of objects and IMetabaseObjectDescriptor - description of one object. ITreeListNodes - collection of tree elements and ITreeListNode - one element of tree.
Working with all system assemblies is similar that is why the code created for one collection processing can be quickly adapted to processing of other collection. Collections have the following general properties and methods:
Count. It returns the number of objects in the collection.
Item. It allows access to collection element by index.
Find or its modifications. It allows for searching objects in the collection by any attribute.
If collections are extendable, they have the following general methods:
Add or its modifications. It allows for adding new objects to the collection.
Remove or its modifications. It allows for removing objects from the collection by certain attribute.
Clear. It allows for clearing the entire collection.
Executing the following example requires a form with a button named Button1 and the TreeList component named TreeList1. The code is a handler of button click event.
Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
Var
MB: IMetabase;
MDescs: IMetabaseObjectDescriptors;
MDesc: IMetabaseObjectDescriptor;
Nodes: ITreeListNodes;
Node: ITreeListNode;
i, k: Integer;
Begin
MB := MetabaseClass.Active;
// Collection of descriptions of objects located in repository root folder
MDescs := MB.Root.Children;
// Collection of elements of the TreeList component
Nodes := TreeList1.Nodes;
k := MDescs.Count;
For i := 0 To k - 1 Do
// Get object description by index
MDesc := MDescs.Item(i);
// Create a tree element corresponding to repository object
Node := Nodes.Add(Null, MDesc.Id);
Node.Data := MDesc;
End For;
End Sub Button1OnClick;
Most of system collections support work with the For Each cycle:
Var
//...
MDescs: IMetabaseObjectDescriptors;
MDesc: IMetabaseObjectDescriptor;
//...
Begin
//...
For Each MDesc In MDescs Do
//...
// Work with description of single object
//...
End For;
See also: