Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a variable by its name(string)?

Tags:

delphi

rtti

I have some global string variables.

I have to create the function that I could pass & store them in some structure. Later I need to enumerate them and check their values.

how can this be easily achieved?

(I think I would need some kind of reflection, or store array of pointers). Anyway, any help will be appreciated.

Thanks!

like image 822
John Avatar asked Jul 18 '11 08:07

John


3 Answers

First of all you can't use Delphi's RTTI for that purpose, because Delphi 7's RTTI only covers published members of classes. Even if you were on Delphi XE, there's still no RTTI for global variables (because RTTI is tied to Types, not to "units").

The only workable solution is to create your own variable registry and register your globals using a name and a pointer to the var itself.

Example:

unit Test;

interface

var SomeGlobal: Integer;
    SomeOtherGlobal: string;

implementation
begin
  RegisterGlobal('SomeGlobal', SomeGlobal);
  RegisterGlobal('SomeOtherGlobal', SomeOtherGlobal);
end.

were the RegisterXXX types would need to be defined somewhere, probably in there own unit, like this:

unit GlobalsRegistrar;

interface

procedure RegisterGlobal(const VarName: string; var V: Integer); overload;
procedure RegisterGlobal(const VarName: string; var V: String); overload;
// other RegisterXXX routines

procedure SetGlobal(const VarName: string; const Value: Integer); overload;
procedure SetGlobal(const VarName:string; const Value:string); overload;
// other SetGlobal variants

function GetGlobalInteger(const VarName: string): Integer;    
function GetGlobalString(const VarName:string): string;
// other GetGlobal variants

implementation

// ....

end.
like image 85
Cosmin Prund Avatar answered Sep 20 '22 09:09

Cosmin Prund


You could also have a global TStringList variable holding a list of name-value pairs.

like image 25
Ondrej Kelle Avatar answered Sep 22 '22 09:09

Ondrej Kelle


On Delphi 7, I would follow Cosmin's idea for the interface, and for the implementation, I would use a dictionary type based on Julian Bucknall's excellent data structures code for Delphi, ezDSL.

Later versions of delphi like XE not only have more advanced RTTI they also include a pretty great dictionary type, using generics, so the dictionary can contain any type you like. The esDSL dictionary is pretty easy to use but since it's pointer based, it isnt as type safe as the delphi generics dictionary.

Since what you need to do is look up string "variable names" in very fast time (O(1) we like to call it), what you need is a string-to-variable dictionary. You could have Strings for the keys, and Variants as the values in the dictionary, and just get rid of the original global variables, or you could attempt some rather complex pointers-to-globals logic, but I really think you'd be better off with a simple dictionary of <string,variant> key,value tuples.

like image 22
Warren P Avatar answered Sep 20 '22 09:09

Warren P