Difference between revisions of "User:Nhollm"

From Lazarus wiki
Jump to navigationJump to search
(Replaced content with "Subject to change")
Tag: Replaced
Line 1: Line 1:
{{Warning|All of this is subject to change. Do '''NOT''' use it until its finished. thx}}
+
Subject to change
 
 
Todo:
 
Exceptions
 
Event Notification
 
Dictionarys with Classes (TObjectDictionary)
 
 
 
==Basic syntax==
 
 
 
<syntaxhighlight lang="pascal">
 
{$mode objfpc}
 
program Example;
 
 
 
uses
 
Generics.Collections;  //import the generics-library of the RTL
 
 
 
type
 
TStringStringDictionary =  specialize TDictionary<string, string>; //specialize a new dictionary and set the desired data-type of key and value
 
 
 
var
 
  MyDictionary : TStringStringDictionary;
 
begin
 
  MyDictionary := TStringStringDictionary.Create;
 
  try
 
    //do stuff with your dictionary..
 
  finally
 
    MyDictionary.Free;
 
  end;
 
end.
 
 
 
</syntaxhighlight>
 
 
 
==Usage:==
 
 
 
<syntaxhighlight lang="pascal">
 
</syntaxhighlight>
 
 
 
===Add===
 
Add Entry or update its current value (key, value)
 
<syntaxhighlight lang="pascal">
 
MyDictionary.AddOrSetValue('Key1', 'Value1');
 
</syntaxhighlight>
 
 
 
There is the Function '''Add'''. But AddOrSetValue is recommended. Add will throw an error if the Key already exists. 'Dublicate Not Allowed in Dictionary' and will not overwrite the value.<br>
 
 
 
 
 
===Remove===
 
<syntaxhighlight lang="pascal">
 
MyDictionary.Remove('Key2');
 
</syntaxhighlight>
 
If the Key does not exist, no Error is thrown.
 
===Clear===
 
<syntaxhighlight lang="pascal">
 
MyDictionary.Clear;
 
</syntaxhighlight>
 
 
 
===Get Value===
 
<syntaxhighlight lang="pascal">
 
WriteLn(MyDictionary.Items['Key1']);
 
</syntaxhighlight>
 
If the provided key does not exist, this will throw an Error: 'Dictionary Key does not Exist'. You can use 'TryGetValue' instead.
 
 
 
===Item Count===
 
Total number of Items in dictionary (count)
 
<syntaxhighlight lang="pascal">
 
WriteLn('Items in Dictionary: ' + IntToStr(MyDictionary.Count));
 
</syntaxhighlight>
 
 
 
===Loops===
 
<syntaxhighlight lang="pascal">
 
//Loop Keys
 
for KeyStr in MyDictionary.Keys do
 
  WriteLn('Found key:' + KeyStr);
 
 
 
 
 
//Loop Values
 
for ValueStr in MyDictionary.Values do
 
  WriteLn('Found Value: ' + ValueStr);
 
 
 
 
 
//Loop Key-Value-Pairs
 
//var KeyValuePair : TStringStringDictionary.TDictionaryPair;
 
for KeyValuePair in MyDictionary do
 
  WriteLn('Found a Pair: Key:' + KeyValuePair.Key + ' Value:' + KeyValuePair.Value);
 
</syntaxhighlight>
 
 
 
===Check If Key/Value Exists===
 
 
 
<syntaxhighlight lang="pascal">
 
//Check if Key exists
 
if MyDictionary.ContainsKey('Key1') then
 
  WriteLn('key exists')
 
else
 
  WriteLn('key not found');
 
 
 
 
 
//Check if Value exists
 
if MyDictionary.ContainsValue("Searched value") then
 
  writeln('value exists')
 
else
 
  writeln('value not found');
 
</syntaxhighlight>
 
 
 
 
 
===Trying===
 
If you want to get a feedback, then use these functions. They return a boolean.<br>
 
True if sucessfull, False if not. No Error is thrown, so they are a good alternative to GetValue or Add.
 
 
 
====TryAdd====
 
TryAdd returns True if added successfully, False otherwise.
 
<syntaxhighlight lang="pascal">
 
if MyDictionary.TryAdd('Key','TestValue') then
 
  writeln('added successfully')
 
else
 
  writeln('not sucessfull');
 
</syntaxhighlight>
 
TryAdd does not work when you try to update a existing keys value. Use AddOrSetValue instead.
 
====TryGetValue====
 
TryGetValue takes two Arguments ->(key, value) and returns a '''boolean'''.<br>
 
If found, it returns '''True''' and the provided 'value' variable becomes the value of the searched KeyValuePair.<br>
 
If not found,the function returns '''False''' and the provided 'value'-variable is empty or nill.
 
 
 
<syntaxhighlight lang="pascal">
 
 
 
if MyDictionary.TryGetValue('Key', SearchedValue) then
 
  WriteLn('Key found. Its value is: ' + SearchedValue)
 
else
 
  WriteLn('Could not get value');
 
 
 
//writeln(BoolToStr(MyDictionary.TryGetValue('Key', SearchedValue)));
 
</syntaxhighlight>
 
 
 
 
 
 
 
 
 
==Source Code==
 
 
 
 
 
<syntaxhighlight lang="pascal">
 
 
 
{$mode objfpc}
 
 
 
program Example;
 
 
 
uses
 
Generics.Collections  //import the generics-library of the RTL
 
 
 
type
 
 
 
TStringStringDictionary =  specialize TDictionary<string, string>; //specialize a new dictionary and set the data-type of key and value
 
 
 
var
 
  MyDictionary : TStringStringDictionary;
 
  TestClass : TmyCoolClass;
 
  //
 
  KeyString: string;
 
  KeyValuePair : TStringStringDictionary.TDictionaryPair;
 
  SearchedValue : string;
 
begin
 
 
 
  //create the dictionary
 
  MyDictionary := TStringStringDictionary.Create;
 
 
 
  //Add Element or update its value to new (key, value)
 
  MyDictionary.AddOrSetValue('Key1', 'Value1');
 
 
 
  //Remove Element
 
  MyDictionary.AddOrSetValue('Key2', 'Another Value');
 
  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 ValueStr in MyDictionary.Values do
 
        writeln('Found Value:' + ValueStr);
 
 
 
 
 
  //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');
 
 
 
 
 
  readln() //for Windows-console-users
 
end.
 
 
 
 
 
 
 
</syntaxhighlight>
 

Revision as of 03:15, 13 May 2023

Subject to change