In vielen Fällen reicht auch ein Array das den Variablennamen und dessen Adresse speichert aus.
Zb.
Delphi-Quellcode:
type
TVarName =
packed record
Name:
String;
Addr: Pointer;
end;
TVarNameArray =
array of TVarName;
var
VarNames: TVarNameArray;
function IndexOfVariable(
const Name:
String): Integer;
var
I: Integer;
begin
for I := 0
to High(VarNames)
do
if AnsiCompareText(
Name, VarNames[I].
Name) = 0
then
begin
Result := I;
Exit;
end;
Result := -1;
end;
function RegisterVariable(
const Name:
String; Addr: Pointer): Integer;
var
I: Integer;
begin
I := IndexOfVariable(
Name);
if I < 0
then
begin
I := Length(VarNames);
SetLength(VarNames, I +1);
end;
VarNames[I].
Name :=
Name;
VarNames[I].Addr := Addr;
Result := I;
end;
function FindVariable(
const Name:
String): Pointer;
var
I: Integer;
begin
I := IndexOfVariable(
Name);
if I < 0
then raise Exception.CreateFmt('
Variable %s not registered.', [
Name]);
Result := VarNames[I].Addr;
end;
var
MyVar: Integer = 0;
procedure TestCode;
begin
RegisterVariable('
MyVar', @MyVar);
PInteger(FindVariable('
MyVar'))^ := 1;
end;
Gruß Hagen