Delphi for .NET FCL Examples Reference
| DictionaryBase | namespace |
| System.Collections |
Declaration
DictionaryBase = class(System.Object, IDictionary, ICollection, IEnumerable);
Description
Provides the abstract base class for a strongly typed collection of key-and-value pairs.
Example
The following code example implements the DictionaryBase class and uses that implementation to create a dictionary of String keys and values that have a Length of 5 characters or less.
program System_Collections_DictionaryBase; {$APPTYPE CONSOLE} {$AUTOBOX ON} uses System.Collections; type ShortStringDictionary = class(DictionaryBase) public function get_Keys: ICollection; function get_Values: ICollection; function get_Items(key: string): string; procedure set_Items(key: string; const Value: string); procedure Add(key: string; value: string); function Contains(key: string): Boolean; procedure Remove(key: string); property Items[key: string]: string read get_Items write set_Items; default; property Keys: ICollection read get_Keys; property Values: ICollection read get_Values; strict protected procedure OnInsert(key: System.Object; value: System.Object); override; procedure OnRemove(key: System.Object; value: System.Object); override; procedure OnSet(key: System.Object; oldValue: System.Object; newValue: System.Object); override; procedure OnValidate(key: System.Object; value: System.Object); override; end; SamplesDictionaryBase = class public class procedure Main; private class procedure PrintKeysAndValues (myCol: ShortStringDictionary); end; { ShortStringDictionary } procedure ShortStringDictionary.Add(key, value: string); begin Dictionary.Add(key, value); end; function ShortStringDictionary.Contains(key: string): Boolean; begin Result := Dictionary.Contains(key); end; function ShortStringDictionary.get_Items(key: string): string; begin Result := (string(Dictionary[key])); end; function ShortStringDictionary.get_Keys: ICollection; begin Result := Dictionary.Keys; end; function ShortStringDictionary.get_Values: ICollection; begin Result := Dictionary.Values; end; procedure ShortStringDictionary.OnInsert(key, value: System.Object); var strValue: string; strKey: string; begin if (key.GetType <> System.Type.GetType('System.String')) then raise ArgumentException.Create ('key must be of type String.', 'key') else begin strKey := (string(key)); if (strKey.Length > 5) then raise ArgumentException.Create ('key must be no more than 5 characters in length.', 'key'); end; if (value.GetType <> System.Type.GetType('System.String')) then raise ArgumentException.Create ('value must be of type String.', 'value') else begin strValue := (string(value)); if (strValue.Length > 5) then raise ArgumentException.Create ('value must be no more than 5 characters in length.', 'value'); end; end; procedure ShortStringDictionary.OnRemove(key, value: System.Object); var strKey: string; begin if (key.GetType <> System.Type.GetType('System.String')) then raise ArgumentException.Create ('key must be of type String.', 'key') else begin strKey := (string(key)); if (strKey.Length > 5) then raise ArgumentException.Create ('key must be no more than 5 characters in length.', 'key'); end; end; procedure ShortStringDictionary.OnSet (key, oldValue, newValue: System.Object); var strValue: string; strKey: string; begin if (key.GetType <> System.Type.GetType('System.String')) then raise ArgumentException.Create ('key must be of type String.', 'key') else begin strKey := (string(key)); if (strKey.Length > 5) then raise ArgumentException.Create ('key must be no more than 5 characters in length.', 'key'); end; if (newValue.GetType <> System.Type.GetType('System.String')) then raise ArgumentException.Create ('newValue must be of type String.', 'newValue') else begin strValue := (string(newValue)); if (strValue.Length > 5) then raise ArgumentException.Create ('newValue must be no more than 5 characters in length.', 'newValue'); end; end; procedure ShortStringDictionary.OnValidate(key, value: System.Object); var strValue: string; strKey: string; begin if (key.GetType <> System.Type.GetType('System.String')) then raise ArgumentException.Create ('key must be of type String.', 'key') else begin strKey := (string(key)); if (strKey.Length > 5) then raise ArgumentException.Create ('key must be no more than 5 characters in length.', 'key'); end; if (value.GetType <> System.Type.GetType('System.String')) then raise ArgumentException.Create ('value must be of type String.', 'value') else begin strValue := (string(value)); if (strValue.Length > 5) then raise ArgumentException.Create ('value must be no more than 5 characters in length.', 'value'); end; end; procedure ShortStringDictionary.Remove(key: string); begin Dictionary.Remove(key); end; procedure ShortStringDictionary.set_Items (key: string; const Value: string); begin Dictionary[key] := value; end; { SamplesDictionaryBase } class procedure SamplesDictionaryBase.Main; var mySSC: ShortStringDictionary; begin // Creates and initializes a new DictionaryBase mySSC := ShortStringDictionary.Create; // Adds elements to the collection mySSC.Add('One', 'a'); mySSC.Add('Two', 'ab'); mySSC.Add('Three', 'abc'); mySSC.Add('Four', 'abcd'); mySSC.Add('Five', 'abcde'); // Tries to add a value that is too long try mySSC.Add('Ten', 'abcdefghij'); except on e: ArgumentException do Console.WriteLine(e.ToString); end; // Tries to add a key that is too long try mySSC.Add('Eleven', 'ijk'); except on e: ArgumentException do Console.WriteLine(e.ToString); end; Console.WriteLine; // Displays the contents of the //collection using the enumerator Console.WriteLine('Initial contents of the collection:'); PrintKeysAndValues(mySSC); // Searches the collection with Contains. Console.WriteLine('Contains "Three": {0}', mySSC.Contains('Three')); Console.WriteLine('Contains "Twelve": {0}', mySSC.Contains('Twelve')); Console.WriteLine; // Removes an element from the collection mySSC.Remove('Two'); Console.WriteLine('New state of the collection:'); PrintKeysAndValues(mySSC); end; class procedure SamplesDictionaryBase.PrintKeysAndValues (myCol: ShortStringDictionary); var myEnumerator: System.Collections.IEnumerator; myDE: DictionaryEntry; begin myEnumerator := myCol.GetEnumerator; while myEnumerator.MoveNext do begin if (myEnumerator.Current <> nil) then begin myDE := (DictionaryEntry(myEnumerator.Current)); Console.WriteLine(' {0,-5} : {1}', myDE.Key, myDE.Value); end; end; Console.WriteLine; end; //test program begin SamplesDictionaryBase.Main; end. |
Example output
System.ArgumentException: value must be no more
than 5 characters in length.
Parameter name: value
at ShortStringDictionary.OnValidate(Object key, Object value)
at System.Collections.DictionaryBase.System.
Collections.IDictionary.Add(Object key, Object value)
at SamplesDictionaryBase.Main()
System.ArgumentException: key must be no more
than 5 characters in length.
Parameter name: key
at ShortStringDictionary.OnValidate(Object key, Object value)
at System.Collections.DictionaryBase.System.
Collections.IDictionary.Add(Object key, Object value)
at SamplesDictionaryBase.Main()
Initial contents of the collection:
One : a
Four : abcd
Three : abc
Two : ab
Five : abcde
Contains "Three": True
Contains "Twelve": False
New state of the collection:
One : a
Four : abcd
Three : abc
Five : abcde
Looking for Delphi for .NET examples? Look no further, Delphi for .NET FCL Examples Reference is what you are looking for!

