Dicas - Como criar Coleções de Objetos

Veja nesta dica, como criar coleções de objetos no .NET Framework.

Para criar CollectionBase usamos o seguinte código:

Namespace System.Collections

A declaração pode ser feita da seguinte forma:

CollectionBase = class (System.Object, IList, ICollection, IEnumerable); abstract;

Assim, provemos a classe básica abstrata para uma coleção fortemente tipada.

Exemplo

O exemplo a seguir implementa o CollectionBase classificando e usando implementação para criar uma coleção de objetos de Int16:

Program System_Collections_CollectionBase; uses System.Collections; type Int16Collection = class(CollectionBase) public function get_Item(index: Integer): Int16; procedure set_Item(index: Integer; const Value: Int16); function Add(value: Int16): Integer; function IndexOf(value: Int16): Integer; procedure Insert(index: Integer; value: Int16); procedure Remove(value: Int16); function Contains(value: Int16): Boolean; property Item[index: Integer]: Int16 read get_Item write set_Item; default; strict protected procedure OnInsert(index: Integer; value: System.Object); override; procedure OnRemove(index: Integer; value: System.Object); override; procedure OnSet(index: Integer; oldValue: System.Object; newValue: System.Object); override; procedure OnValidate(value: System.Object); override; end; SamplesCollectionBase = class public class procedure Main; private class procedure PrintIndexAndValues ( myCol: Int16Collection); end; { SamplesCollectionBase } class procedure SamplesCollectionBase.Main; var i: Integer; myI16: Int16Collection; begin // Creates and initializes a new CollectionBase myI16 := Int16Collection.Create; myI16.Add(Int16(1)); myI16.Add(Int16(2)); myI16.Add(Int16(3)); myI16.Add(Int16(5)); myI16.Add(Int16(7)); // Displays the contents of the //collection using the enumerator Console.WriteLine('Initial contents of the collection:'); PrintIndexAndValues(myI16); // Searches the collection with Contains and IndexOf Console.WriteLine('Contains 3: ', myI16.Contains(3)); Console.WriteLine('2 is at index .', myI16.IndexOf(2)); Console.WriteLine; // Inserts an element into the collection at index 3 myI16.Insert(3, (Int16(13))); Console.WriteLine('Contents of the collection after inserting at index 3:'); PrintIndexAndValues(myI16); // Gets and sets an element using the index. myI16[4] := 123; Console.WriteLine('Contents of the collection after setting the element at index 4 to 123:'); PrintIndexAndValues(myI16); // Removes an element from the collection myI16.Remove((Int16(2))); // Displays the contents of //the collection using the index. Console.WriteLine('Contents of the collection after removing the element 2:'); i := 0; while (i < myI16.Count) do begin Console.WriteLine('[]: ', i, myI16[i]); i := i + 1; end; end; class procedure SamplesCollectionBase.PrintIndexAndValues ( myCol: Int16Collection); var myEnumerator: System.Collections.IEnumerator; i: Integer; begin i := 0; myEnumerator := myCol.GetEnumerator; while myEnumerator.MoveNext do begin Console.WriteLine('[]: ', i, myEnumerator.Current); i:=i+1; end; Console.WriteLine; end; { Int16Collection } function Int16Collection.Add(value: Int16): Integer; begin Result := List.Add(value); end; function Int16Collection.Contains(value: Int16): Boolean; begin // If value is not of type Int16, this will return false. Result := List.Contains(value); end; function Int16Collection.get_Item(index: Integer): Int16; begin Result := (Int16(List[index])); end; function Int16Collection.IndexOf(value: Int16): Integer; begin Result := List.IndexOf(value); end; procedure Int16Collection.Insert ( index: Integer; value: Int16); begin List.Insert(index, value); end; procedure Int16Collection.OnInsert ( index: Integer; value: System.Object); begin if (value.GetType <> System.Type.GetType('System.Int16')) then raise ArgumentException.Create ( 'value must be of type Int16.', 'value'); end; procedure Int16Collection.OnRemove ( index: Integer; value: System.Object); begin if (value.GetType <> System.Type.GetType('System.Int16')) then raise ArgumentException.Create ( 'value must be of type Int16.', 'value'); end; procedure Int16Collection.OnSet ( index: Integer; oldValue, newValue: System.Object); begin if (newValue.GetType <> System.Type.GetType('System.Int16')) then raise ArgumentException.Create ( 'newValue must be of type Int16.', 'newValue'); end; procedure Int16Collection.OnValidate (value: System.Object); begin if (value.GetType <> System.Type.GetType('System.Int16')) then raise ArgumentException.Create ( 'value must be of type Int16.'); end; procedure Int16Collection.Remove(value: Int16); begin List.Remove(value); end; procedure Int16Collection.set_Item ( index: Integer; const Value: Int16); begin List[index] := value; end; //test program begin SamplesCollectionBase.Main; end.

Como saída teremos:

Conteúdos iniciais da coleção: [0]: 1 [1]: 2 [2]: 3 [3]: 5 [4]: 7 Contains 3: True 2 is at index 1. Contents of the collection after inserting at index 3: [0]: 1 [1]: 2 [2]: 3 [3]: 13 [4]: 5 [5]: 7 Contents of the collection after setting the element at index 4 to 123: [0]: 1 [1]: 2 [2]: 3 [3]: 13 [4]: 123 [5]: 7 Contents of the collection after removing the element 2: [0]: 1 [1]: 3 [2]: 13 [3]: 123 [4]: 7
Ebook exclusivo
Dê um upgrade no início da sua jornada. Crie sua conta grátis e baixe o e-book

Artigos relacionados