Forum > Beginners

How to use the contents of a string as a variable name

(1/6) > >>

Jiří Huňáček:
Hi,

I may have a silly question, but I need to know if it is possible to use the contents of a string as a variable name.

For example, as follows


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  VarName: integer;  VarNameStr: string; begin       VarName:=99;       VarNameStr:='VarName';  
then I want to write something like WriteLn(VarNameStr); so that the result is 99.

Thank you for your help and advice

Jirka

marcov:
This is generally not possible in compiled languages. The names are placeholders for human consumption, in the final compiled binary (EXE), they are replaced by addresses.

There are some ways around that for special cases, but generally that is more for deep in frameworks usage, and not for own code.

And for this specific example there is none.

Maybe you have more success if you describe your actual problem rather than what you think you need to solve it.

lucamar:

--- Quote from: jamie on August 13, 2021, 03:33:44 pm ---Varnamestr : = inttostr(varname);

Does that work for you?
--- End quote ---

That's not what he wants. He wants something like e.g.:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  VarOne: Integer = 999;  VarTwo: Integer = 274; {function ValueFromName(const AVarName: String): Integer;begin  Result := ??????end;} begin    WriteLn(ValueFromName('VarOne'))    WriteLn(ValueFromName('VarTwo'))end.
How would you implement ValueFromName()? Remember, the point is that you pass a variable name and return its value. ;D

Remy Lebeau:
In this case, you can use a Dictionary like TFPGMap:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  ..., fgl; var  Dict: TFPGMap<string, integer>;  Value: Integer; begin  Dict := TFPGMap<string, integer>.Create;  try     Dict.Add('VarName', 99);    ...    Value := Dict['VarName'];    ...  finally    Dict.Free;  end;end;
Or, if you don't mind storing only strings, a TStringList of 'name=value' pairs will work, too:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  ..., Classes; var  SL: TStringList;  Value: Integer; begin  SL := TStringList.Create;  try     SL.Add('VarName=99');    ...    Value := StrToInt(SL.Values['VarName']);    ...  finally    SL.Free;  end;end;

engkin:
I believe it's doable with custom variant, with usage like:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  v:variant;begin  v:=TSomeClass.newInstance;   v.VarOne:=111;  v.VarTwo:=222;   WriteLn(v.FromName('VarOne'));  WriteLn(v.FromName('VarTwo'));

Navigation

[0] Message Index

[#] Next page

Go to full version