Difference between revisions of "User:Nhollm"

From Free Pascal wiki
Jump to navigationJump to search
(Replaced content with "{{Test Title}} <syntaxhighlight lang="pascal"> </syntaxhighlight>")
Tag: Replaced
Line 4: Line 4:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
  
{$mode objfpc}{$H+}{$J-}
 
 
program Example;
 
 
uses
 
Generics.Collections, sysutils;  //import the generics-library of the RTL
 
 
type
 
TmyCoolClass = class
 
Name: String;
 
end;
 
 
TStringClassDictionary = specialize TDictionary<string, TmyCoolClass>;  //specialize a new dictionary and set the data-type of key and value
 
TStringStringDictionary =  specialize TDictionary<string, string>;
 
 
var
 
  MyDictionary : TStringClassDictionary;
 
  TestClass : TmyCoolClass;
 
  KeyString: string;
 
  KeyValuePair : TStringClassDictionary.TDictionaryPair;
 
  SearchedValue : TmyCoolClass;
 
begin
 
 
  //create the dictionary
 
  MyDictionary := TStringClassDictionary.Create;
 
 
 
  TestClass := TmyCoolClass.Create;
 
  TestClass.Name := 'Hi';
 
 
  SearchedValue := TmyCoolClass.Create;
 
  SearchedValue.Name := 'HIIII';
 
 
  //Add Element or update its value to new (key, value)
 
  MyDictionary.AddOrSetValue('Key1', TestClass);
 
 
  //Remove Element
 
  MyDictionary.AddOrSetValue('Key2', TestClass);
 
  MyDictionary.Remove('Key2');
 
 
 
  //Clear entire dictionary
 
  //MyDictionary.Clear;
 
 
  // Total number of Items in dictionary (count)
 
  writeln('Items in Dictionary: ' + IntToStr(MyDictionary.Count));
 
 
  //Get Value by Key
 
  writeln(MyDictionary.Items['Key1'].Name);  //If the provided key does not exist, this will throw a Error :'Dictionary Key does not Exist'
 
 
 
  //----------------------Trying------------------
 
 
 
  //TryAdd  returns True if added successfully, False otherwise
 
    writeln('TryAdd: ' + BoolToStr(MyDictionary.TryAdd('Key2',TestClass ),'True', 'False'));
 
 
  //'TryGetValue'takes two Arguments ->(key, value) and returns a boolean.
 
  //If found, it returns true and the provided 'value' variable becomes the value of the searched KeyValuePair
 
  //if not found,the function returns False and the provided 'value'-variable is nil
 
  writeln(BoolToStr(MyDictionary.TryGetValue('Key', SearchedValue)));
 
  if MyDictionary.TryGetValue('Key', SearchedValue) then
 
        writeln('Key found. Its Value is ' + SearchedValue.Name )
 
  else
 
      if SearchedValue = Nil then
 
          writeln('the provided Value is Nil now');
 
 
 
  //----------------------Loop through the Dictionary------------------
 
 
  //Loop Keys
 
  for KeyString in MyDictionary.Keys do
 
        writeln('Found key:' + KeyString);
 
 
 
  //Loop Values
 
  for TestClass in MyDictionary.Values do
 
        writeln('Found Value:' + TestClass.Name);
 
 
 
  //Loop Key-Value-Pairs
 
  for KeyValuePair in MyDictionary do
 
        writeln('Found a Pair: Key:' + KeyValuePair.Key + ' Value:' + KeyValuePair.Value.Name);
 
 
 
 
    //----------------------Check if exists in whole Dictionary------------------
 
 
  //Check if Key exists
 
  if MyDictionary.ContainsKey('Key1') then
 
      writeln('key exists')
 
  else
 
      writeln('key not found');
 
 
 
  //Check if Value exists
 
  if MyDictionary.ContainsValue(TestClass) then
 
      writeln('value exists')
 
  else
 
      writeln('value not found');
 
 
 
  //Accessing Items by a Index is not possible, (like in Delphi)!
 
 
 
 
 
  //Errors
 
  //Events
 
  //Miscelangeous
 
 
  readln()
 
end.                                                                           
 
  
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 01:39, 26 November 2022