Solved

Weird Delphi variable question

Posted on 2011-09-12
5
446 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 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:Ephraim Wangoya
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

749 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