Mam taki oto kod operujący na słowniku
procedure DictionaryTest();
var dic : TDictionary<string, string>;
s : string;
begin
dic := TDictionary<string, string>.Create();
try
dic.Add('Klucz 1', 'Wartosc 1');
dic.Add('Klucz 2', 'Wartosc 2');
for s in dic.Keys do
Writeln(Format('Klucz ze slownika: %s', [s]));
for s in dic.Values do
Writeln(Format('Wartosc ze slownika: %s', [s]));
finally
if (Assigned(dic)) then dic.Free;
end;
end;
I moje pytanie jest następujące, jak sprawdzić czy zmienna typu TDictionary została zainicjalizowana.
Przykładowa poniższa procedura zgłasza AV:
procedure DictionaryTest2();
var dic : TDictionary<string, string>;
begin
//dic := TDictionary<string, string>.Create();
try
//jakieś operacje na słowniku
finally
if (Assigned(dic)) then dic.Free;
end;
end;
Pomoc dołączona do Delphi pisze:
Description
Tests for a nil (unassigned) pointer or procedural variable.
Use Assigned to determine whether the pointer or procedure referenced by P is nil. P must be a variable reference of a pointer or procedural type. Assigned(P) corresponds to the test P<> nil for a pointer variable, and @P <> nil for a procedural variable.
Assigned returns False if P is nil, True otherwise.
Note: Assigned cannot detect a dangling pointer--that is, one that is not nil but no longer points to valid data. For example, in the code example for Assigned, Assigned does not detect that P is not valid.
Jest tu notka kiedy Assigned nie działa ale nie wiem co to jest "dangling pointer"