Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

Weird Delphi variable question

I have a question about whether or not an idea I have for making some code dynamic is actually possible.

In my code I'm building a filename by piecing together elements I receive back from a SQL query. The query would return a 'variable name' which would match up to an actual variable in my code. Can I somehow access the value of the REAL variable based on what name is returned from the query?

Example:
gsDCN := '123456789';

sReturnedValue := 'gsDCN'; //would actually be returned from SQL query

sOutputString := <value from the variable named in sReturnValue> + '.XML';

Is this possible? Maybe another method to accomplish this?

Thanks for any time spent on this.
Jason
0
jasonkrueger
Asked:
jasonkrueger
1 Solution
 
jasonkruegerAuthor Commented:
any help is appreciated
0
 
aikimarkCommented:
@jasonkrueger

I think your posting of code is hurting your description of your problem.
0
 
Ephraim WangoyaCommented:

var
  FileName: string;
begin
  ....
  FileName := Query.FieldByName('FieldName').AsString + '.XML';
  ...
End;
0
 
epasquierCommented:
no, it is not possible, not without a specific data structure.

Once your code is compiled, all variable names are lost.

BUT :
You could create your own list of name/value pair. You would put there ALL the "variables" that can be accessed in such a way, thus creating a kind of self-awareness of your application in regard of its key variables.

If all such variables are string, then you can immediately use a TStringList as storage , thanks to its Values property (check in Delphi Help)
VarList:=TStringList.Create;
VarList.Values['VAR1']:='Hello';  // initialize your parameters
VarList.Values['VAR2']:='World';

sOutputString := Format ('%s.xml', [ VarList.Values['VAR1'] );

Open in new window


If you can have different types, then you'll have to create object wrappers around those types.
To avoid multiplying the definition of such wrapper objects, you should use 'Objectized' variants, that can hold any elementary type, or arrays of elementary types. That should be enough in most cases.


 TVariantObject=class(TObject)
  private
   _Value:Variant;
  public
   Constructor Create(V:Variant);

   property Value:Variant read _Value write _Value;
  end;

Open in new window


Then you still use TStringList as base class for storage of pairs Name/Objects, thanks now to its functionality to associate an object with a string (that can be sorted to allow quick finding)
TVariableList = class(TStringList)
public
  Constructor Create; 
  Destructor Destroy; override;
  
  procedure SetVar(VarName:String;VarValue:Variant);
  function  GetVar(VarName:String):Variant;
end;

Open in new window


Implementation :


Constructor TVariantObject.Create(V:Variant);
begin
 _Value:=V;
end;

Constructor TVariableList.Create;
begin
 inherited;
 Sorted:=True;
 CaseSensitive:=True; // or False if you want var1 and VAR1 to be different variables
end;

Destructor TVariableList.Destroy;
Var
 i:integer;
begin
 for i:=0 to Count-1 do Objects[i].Free;
 inherited;
end;

procedure TVariableList.SetVar(VarName:String;VarValue:Variant);
Var
 ObjVar:TVariantObject;
 i:integer;
begin
 i:=IndexOf(VarName);
 if i>=0 Then
  begin
   // Var already existing
   ObjVar:=TVariantObject(Objects[i]);
   ObjVar.Value:=VarValue;
  end else
  begin
   // Not existing : creating a new entry
   ObjVar:=TVariantObject.Create(VarValue);
   AddObject(VarName,ObjVar);
  end;
end;

function TVariableList.GetVar(VarName:String):Variant;
Var
 ObjVar:TVariantObject;
 i:integer;
begin
 i:=IndexOf(VarName);
 if i>=0 Then
  begin
   // Var already existing
   ObjVar:=TVariantObject(Objects[i]);
   Result:=ObjVar.Value;
  end else Result:=Null;
end;

Open in new window

0
 
jasonkruegerAuthor Commented:
i went with the TStringList route. Thanks to everyone that took time to respond.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now