Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Weird Delphi variable question

Posted on 2011-09-12
5
Medium Priority
?
452 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

636 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