?
Solved

Weird Delphi variable question

Posted on 2011-09-12
5
Medium Priority
?
450 Views
Last Modified: 2012-05-12
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
Comment
Question by:jasonkrueger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Author Comment

by:jasonkrueger
ID: 36525995
any help is appreciated
0
 
LVL 46

Expert Comment

by:aikimark
ID: 36526226
@jasonkrueger

I think your posting of code is hurting your description of your problem.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 36526493

var
  FileName: string;
begin
  ....
  FileName := Query.FieldByName('FieldName').AsString + '.XML';
  ...
End;
0
 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 36526514
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
 

Author Closing Comment

by:jasonkrueger
ID: 36529420
i went with the TStringList route. Thanks to everyone that took time to respond.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question