Solved

Weird Delphi variable question

Posted on 2011-09-12
5
436 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
5 Comments
 

Author Comment

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

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:ewangoya
ID: 36526493

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

Accepted Solution

by:
epasquier earned 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now