Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2019-07

specify variable name with string variable

21 messages · 10 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable?

For example:

Var
   MyVariable1 : Word;
   MyVariableName : String;
Procedure ShowVariable(Variablename);
Begin
Writeln(Variablename,' = $', inttohex(    (Somehow get the value of the variable specified by the name here ) ,4));
End;
Begin
MyVariableName:= 'MyVariable1';
ShowVariable(MyVariableName);
End.

Is such a thing possible?

James

Reply by email

zh loza
to James Richters

Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up.

PM James Richters wrote
This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable?

You might want to try using generic dictionaries. E.g. like this:

program Project1;
uses
  SysUtils, Generics.Collections;
type
  TVariableDictionary = specialize TDictionary<String, PWord>;
var
  MyDictionary: TVariableDictionary;
  MyVariable1: Word;
begin
  MyDictionary := TVariableDictionary.Create;
  MyDictionary.Add('MyVariable1', @MyVariable1);
  MyDictionary['MyVariable1']^ := 100;
  WriteLn(MyVariable1);
  WriteLn(MyDictionary['MyVariable1']^);
  FreeAndNil(MyDictionary);
end.

If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).

Reply by email

Dmitry Boyarintsev
to James Richters: This might sound silly, but is it possible to somehow specify a variable with a string…

associative array?

James Richters wrote 27 lines
This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? For example: Var MyVariable1 : Word; MyVariableName : String; Procedure ShowVariable(Variablename); Begin Writeln(Variablename,' = $', inttohex( (Somehow get the value of the variable specified by the name here ) ,4)); End; Begin MyVariableName:= 'MyVariable1'; ShowVariable(MyVariableName); End. Is such a thing possible? James _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Ryan Joseph
to James Richters: This might sound silly, but is it possible to somehow specify a variable with a string…
On Jul 7, 2019, at 3:58 PM, James Richters wrote: This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable?

Does you mean like RTTI so you can get a pointer to a local variable by name? I was actually curious about this recently also, does FPC have local variable RTTI? That would be useful to have actually. I know it exists for classes but not sure about locals.

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to James Richters: This might sound silly, but is it possible to somehow specify a variable with a string…
James Richters wrote 19 lines
This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? For example: Var MyVariable1 : Word; MyVariableName : String; Procedure ShowVariable(Variablename); Begin Writeln(Variablename,' = $', inttohex( (Somehow get the value of the variable specified by the name here ) ,4)); End; Begin MyVariableName:= 'MyVariable1'; ShowVariable(MyVariableName); End. Is such a thing possible?

No.
If you want that you need to declare them as published properties of a class.

Michael.

Reply by email

James Richters
to Michael Van Canneyt

Is there an example of how declare them this way? I only have about 50 variables I wish to do this with.

Earlier message 34 lines
-----Original Message----- From: fpc-pascal On Behalf Of Michael Van Canneyt Sent: Sunday, July 7, 2019 6:20 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] specify variable name with string variable On Sun, 7 Jul 2019, James Richters wrote: > This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? > > For example: > > Var > MyVariable1 : Word; > MyVariableName : String; > > Procedure ShowVariable(Variablename); > Begin > Writeln(Variablename,' = $', inttohex( (Somehow get the value of the variable specified by the name here ) ,4)); > End; > > Begin > MyVariableName:= 'MyVariable1'; > ShowVariable(MyVariableName); > End. > > Is such a thing possible? No. If you want that you need to declare them as published properties of a class. Michael.

Reply by email

James Richters
to zh loza: Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up. On…

Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try!

James

Earlier message 35 lines
-----Original Message----- From: fpc-pascal On Behalf Of zh loza Sent: Sunday, July 7, 2019 4:34 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] specify variable name with string variable Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up. On Sun, Jul 7, 2019 at 10:58 PM James Richters wrote: > > This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? You might want to try using generic dictionaries. E.g. like this: program Project1; uses SysUtils, Generics.Collections; type TVariableDictionary = specialize TDictionary<String, PWord>; var MyDictionary: TVariableDictionary; MyVariable1: Word; begin MyDictionary := TVariableDictionary.Create; MyDictionary.Add('MyVariable1', @MyVariable1); MyDictionary['MyVariable1']^ := 100; WriteLn(MyVariable1); WriteLn(MyDictionary['MyVariable1']^); FreeAndNil(MyDictionary); end. If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).

Reply by email

James Richters
to James Richters

Generics.Collections seems to be something that was added after 3.0.4, I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?

James

Earlier message 44 lines
-----Original Message----- From: fpc-pascal On Behalf Of James Richters Sent: Monday, July 8, 2019 6:48 AM To: 'FPC-Pascal users discussions' Subject: Re: [fpc-pascal] specify variable name with string variable Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try! James -----Original Message----- From: fpc-pascal On Behalf Of zh loza Sent: Sunday, July 7, 2019 4:34 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] specify variable name with string variable Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up. On Sun, Jul 7, 2019 at 10:58 PM James Richters wrote: > > This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? You might want to try using generic dictionaries. E.g. like this: program Project1; uses SysUtils, Generics.Collections; type TVariableDictionary = specialize TDictionary<String, PWord>; var MyDictionary: TVariableDictionary; MyVariable1: Word; begin MyDictionary := TVariableDictionary.Create; MyDictionary.Add('MyVariable1', @MyVariable1); MyDictionary['MyVariable1']^ := 100; WriteLn(MyVariable1); WriteLn(MyDictionary['MyVariable1']^); FreeAndNil(MyDictionary); end. If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).

Reply by email

James Richters
to James Richters

I'm on windows....

Generics.Collections seems to be something that was added after 3.0.4, I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?

James

Earlier message 44 lines
-----Original Message----- From: fpc-pascal On Behalf Of James Richters Sent: Monday, July 8, 2019 6:48 AM To: 'FPC-Pascal users discussions' Subject: Re: [fpc-pascal] specify variable name with string variable Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try! James -----Original Message----- From: fpc-pascal On Behalf Of zh loza Sent: Sunday, July 7, 2019 4:34 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] specify variable name with string variable Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up. On Sun, Jul 7, 2019 at 10:58 PM James Richters wrote: > > This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? You might want to try using generic dictionaries. E.g. like this: program Project1; uses SysUtils, Generics.Collections; type TVariableDictionary = specialize TDictionary<String, PWord>; var MyDictionary: TVariableDictionary; MyVariable1: Word; begin MyDictionary := TVariableDictionary.Create; MyDictionary.Add('MyVariable1', @MyVariable1); MyDictionary['MyVariable1']^ := 100; WriteLn(MyVariable1); WriteLn(MyDictionary['MyVariable1']^); FreeAndNil(MyDictionary); end. If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).

Reply by email

Martin Frb
to James Richters
James Richters wrote
I'm on windows.... Generics.Collections seems to be something that was added after 3.0.4, I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?

If it is in 3.2-beta, you can get a 3.2-beta from here: https://sourceforge.net/projects/lazarus-snapshots/files/ You can install as 2ndary install.

Otherwise fpcupdeluxe

Reply by email

Martin Frb
to James Richters: I'm on windows.... Generics.Collections seems to be something that was added after 3.0.4,…
James Richters wrote
I'm on windows.... Generics.Collections seems to be something that was added after 3.0.4, I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?

If it is in 3.2-beta, you can get a 3.2-beta from here: https://sourceforge.net/projects/lazarus-snapshots/files/ You can install as 2ndary install.

Otherwise fpcupdeluxe

Reply by email

Stefan V. Pantazi
to James Richters: I'm on windows.... Generics.Collections seems to be something that was added after 3.0.4,…

You can use the TFPGMap class in fgl unit to implement the dictionary. fgl should be available in 3.0.4. Try to replace the Generics.Collections with fgl and the type definition in the example code from zh loza. The rest should be identical.

[...]

uses fgl;
type
TVariableDictionary = specialize TFPGMap<String, PWord>;

[...]

James Richters wrote 59 lines
I'm on windows.... Generics.Collections seems to be something that was added after 3.0.4, I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code? James -----Original Message----- From: fpc-pascal On Behalf Of James Richters Sent: Monday, July 8, 2019 6:48 AM To: 'FPC-Pascal users discussions' Subject: Re: [fpc-pascal] specify variable name with string variable Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try! James -----Original Message----- From: fpc-pascal On Behalf Of zh loza Sent: Sunday, July 7, 2019 4:34 PM To: FPC-Pascal users discussions Subject: Re: [fpc-pascal] specify variable name with string variable Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up. On Sun, Jul 7, 2019 at 10:58 PM James Richters wrote: > > This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? You might want to try using generic dictionaries. E.g. like this: program Project1; uses SysUtils, Generics.Collections; type TVariableDictionary = specialize TDictionary<String, PWord>; var MyDictionary: TVariableDictionary; MyVariable1: Word; begin MyDictionary := TVariableDictionary.Create; MyDictionary.Add('MyVariable1', @MyVariable1); MyDictionary['MyVariable1']^ := 100; WriteLn(MyVariable1); WriteLn(MyDictionary['MyVariable1']^); FreeAndNil(MyDictionary); end. If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ). _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________________

Reply by email

James Richters
to Stefan V. Pantazi

Thank you, using fgl; did work.

Am I correct in assuming that if I want other types of Variables I would change PWord in specialize TFPGMap<String, PWord>;
to something else, maybe PString, PDouble, PLongint.... etc?

James

Earlier message 87 lines
-----Original Message----- From: fpc-pascal On Behalf Of Stefan V. Pantazi Sent: Monday, July 8, 2019 10:54 AM To: Subject: Re: [fpc-pascal] specify variable name with string variable You can use the TFPGMap class in fgl unit to implement the dictionary. fgl should be available in 3.0.4. Try to replace the Generics.Collections with fgl and the type definition in the example code from zh loza. The rest should be identical. [...] uses fgl; type TVariableDictionary = specialize TFPGMap<String, PWord>; [...] On 7/8/19 8:09 AM, James Richters wrote: > I'm on windows.... > > Generics.Collections seems to be something that was added after 3.0.4, I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code? > > James > -----Original Message----- > From: fpc-pascal On Behalf > Of James Richters > Sent: Monday, July 8, 2019 6:48 AM > To: 'FPC-Pascal users discussions' > Subject: Re: [fpc-pascal] specify variable name with string variable > > Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try! > > James > -----Original Message----- > From: fpc-pascal On Behalf > Of zh loza > Sent: Sunday, July 7, 2019 4:34 PM > To: FPC-Pascal users discussions > Subject: Re: [fpc-pascal] specify variable name with string variable > > Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up. > > On Sun, Jul 7, 2019 at 10:58 PM James Richters wrote: >> >> This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? > > You might want to try using generic dictionaries. E.g. like this: > > program Project1; > uses > SysUtils, Generics.Collections; > > type > TVariableDictionary = specialize TDictionary<String, PWord>; > > var > MyDictionary: TVariableDictionary; > MyVariable1: Word; > > begin > MyDictionary := TVariableDictionary.Create; > MyDictionary.Add('MyVariable1', @MyVariable1); > MyDictionary['MyVariable1']^ := 100; > WriteLn(MyVariable1); > WriteLn(MyDictionary['MyVariable1']^); > FreeAndNil(MyDictionary); > end. > > If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ). > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > --

Reply by email

Darius Blaszyk
to James Richters: This might sound silly, but is it possible to somehow specify a variable with a string…
This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable?

Yes it certainly is. I wrote a unit that can handle arbitrary variable types for my project pmake. Pmake it a build tool for pascal.

https://github.com/daar/pmake/blob/master/pmake/pmake_variables.pas

To set a variable, simply use: set_(“myvar”, 3.141). To read it, use: writeln(val_(“myvar”)).

Rgds, Darius

Reply by email

Stefan V. Pantazi
to James Richters: Thank you, using fgl; did work. Am I correct in assuming that if I want other types of…

sure, but you end up with a separate dictionary for each type. Or, you can try variants, as originally suggested, but then you need to declare all your variables as a Variant type.

uses fgl, variants;
type
   TVariableDictionary = specialize TFPGMap<String, PVariant>;
James Richters wrote 103 lines
Thank you, using fgl; did work. Am I correct in assuming that if I want other types of Variables I would change PWord in specialize TFPGMap<String, PWord>; to something else, maybe PString, PDouble, PLongint.... etc? James -----Original Message----- From: fpc-pascal On Behalf Of Stefan V. Pantazi Sent: Monday, July 8, 2019 10:54 AM To: Subject: Re: [fpc-pascal] specify variable name with string variable You can use the TFPGMap class in fgl unit to implement the dictionary. fgl should be available in 3.0.4. Try to replace the Generics.Collections with fgl and the type definition in the example code from zh loza. The rest should be identical. [...] uses fgl; type TVariableDictionary = specialize TFPGMap<String, PWord>; [...] On 7/8/19 8:09 AM, James Richters wrote: > I'm on windows.... > > Generics.Collections seems to be something that was added after 3.0.4, I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code? > > James > -----Original Message----- > From: fpc-pascal On Behalf > Of James Richters > Sent: Monday, July 8, 2019 6:48 AM > To: 'FPC-Pascal users discussions' > Subject: Re: [fpc-pascal] specify variable name with string variable > > Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try! > > James > -----Original Message----- > From: fpc-pascal On Behalf > Of zh loza > Sent: Sunday, July 7, 2019 4:34 PM > To: FPC-Pascal users discussions > Subject: Re: [fpc-pascal] specify variable name with string variable > > Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up. > > On Sun, Jul 7, 2019 at 10:58 PM James Richters wrote: >> >> This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? > > You might want to try using generic dictionaries. E.g. like this: > > program Project1; > uses > SysUtils, Generics.Collections; > > type > TVariableDictionary = specialize TDictionary<String, PWord>; > > var > MyDictionary: TVariableDictionary; > MyVariable1: Word; > > begin > MyDictionary := TVariableDictionary.Create; > MyDictionary.Add('MyVariable1', @MyVariable1); > MyDictionary['MyVariable1']^ := 100; > WriteLn(MyVariable1); > WriteLn(MyDictionary['MyVariable1']^); > FreeAndNil(MyDictionary); > end. > > If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ). > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > _______________________________________________ > fpc-pascal maillist - > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > -- _______________________________________________________ _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________________

Reply by email

Santiago A.
to James Richters: This might sound silly, but is it possible to somehow specify a variable with a string…
James Richters wrote 19 lines
This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? For example: Var MyVariable1 : Word; MyVariableName : String; Procedure ShowVariable(Variablename); Begin Writeln(Variablename,' = $', inttohex( (Somehow get the value of the variable specified by the name here ) ,4)); End; Begin MyVariableName:= 'MyVariable1'; ShowVariable(MyVariableName); End. Is such a thing possible?

What is it for?

What about:

procedure ShowVariable(const Variablename:string; const value:word);
begin
  Writeln(Variablename,' = $'+inttohex(value ,4));
end;
ShowVariable('myVar',myVar);

And as far as I understand, you want to call
ShowVariable('myVar',myVar), but not wasting time writing the name enclosed in quotes, let the program get the identifier name in runtime, or the other way around, let the program get the value from a name passed in runtime.

Bad luck, Pascal is not a script language, it is not aware of identifier names in runtime there is no "eval". Thanks to that it gets rids of all the burden of having to keep in runtime structures to know the name, type and value of variable. It generate fast native programs.

Nevertheless, there are ways, you can program that in Freepascal using classes, properties, RTII etc. It is more flexible and powerful. But obviously it has a price in complexity. Instead or using the standard operators and reference a simple variable, you must use a more convoluted syntax.

As far as I see, you don't intend to let the program create new vars in runtime, you just want to write the identifiers of the vars you have hardcoded.  Is it worth?

(I hope it is not just for debugging)

Saludos Santiago A.

Reply by email

James Richters
to Santiago A.

Well what it’s for, is not just debugging, I’m trying to manage outputs to ports that are defined by port numbers and pins in an ini file, the ports and pins can really be anything but I need to keep track of the output states of all the pins by specific port value because I can only output all 8 bits of the port at one time. I can just read back the port and then use AND or OR with what I want and write it back out, but that’s slow, and it also is not necessarily accurate because some ports will give what’s on the pin itself, not what output it is trying to send out… you can be outputting a 1 and the user can force it to 0 and it will read in a 0 now when you write to it, it’s outputting a 0 and if the user removes the external condition forcing it to 0 it will not go back to 1.. the ports are 1/O ports and are designed to be operated in this manner. Anyway I made an array of ports and the data each should contain, and then I also have an array of each I/O pin (defined with a string of the name of the variable that holds the pin number) and the port it is looking for… and then I have procedures and functions that use these arrays to set the stored value of each port correctly, then I just output the entire 8 bits from the storage array. It’s working well, but I’m tired of doing things like this:

SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', Ini_Opto_Enable_Positive_Pin,True); Which calls a procedure like this:

Procedure SetStoragePinbyname(SSPBN_Var_Name:String; setpinnum:Byte; Pin_State:Boolean);

So in quotes I have the name of the variable that contains the name of the variable in the array, so it can figure out the position in the port array, then the actual contents of the actual variable being used… so I know what pin number I am outputting to, and then the state I want to set… So I have a TON of exactly what you describe, calls to functions and procedures with the name of the variable in quotes AND the variable itself. Variable names are not assigned at runtime, they are pre-defined. I did not need to use the name of the variable in my pin-list array, but it makes it less confusing if the variable name is what I search for in the array than some kind of cryptic code that would be difficult to maintain.

So yes, the only reason to want to do this is so I could hopefully shorted the call to just

               SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ;

And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin'

Hmmmm now that I’m typing this all out, I think I can just put the value of the pin in another array also referenced by the same string and then I could make my calls like that.

James.

Earlier message 59 lines
From: fpc-pascal On Behalf Of Santiago A. Sent: Monday, July 8, 2019 3:40 PM To: Subject: Re: [fpc-pascal] specify variable name with string variable El 07/07/2019 a las 21:58, James Richters escribió: This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable? For example: Var MyVariable1 : Word; MyVariableName : String; Procedure ShowVariable(Variablename); Begin Writeln(Variablename,' = $', inttohex( (Somehow get the value of the variable specified by the name here ) ,4)); End; Begin MyVariableName:= 'MyVariable1'; ShowVariable(MyVariableName); End. Is such a thing possible? What is it for? What about: procedure ShowVariable(const Variablename:string; const value:word); begin Writeln(Variablename,' = $'+inttohex(value ,4)); end; ShowVariable('myVar',myVar); And as far as I understand, you want to call ShowVariable('myVar',myVar), but not wasting time writing the name enclosed in quotes, let the program get the identifier name in runtime, or the other way around, let the program get the value from a name passed in runtime. Bad luck, Pascal is not a script language, it is not aware of identifier names in runtime there is no "eval". Thanks to that it gets rids of all the burden of having to keep in runtime structures to know the name, type and value of variable. It generate fast native programs. Nevertheless, there are ways, you can program that in Freepascal using classes, properties, RTII etc. It is more flexible and powerful. But obviously it has a price in complexity. Instead or using the standard operators and reference a simple variable, you must use a more convoluted syntax. As far as I see, you don't intend to let the program create new vars in runtime, you just want to write the identifiers of the vars you have hardcoded. Is it worth? (I hope it is not just for debugging) -- Saludos Santiago A.

Reply by email

Sven Barth
to James Richters

James Richters schrieb am Mo., 8. Juli
2019, 22:44:

SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ; And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin

That indeed sounds like something where you could use classes together with published properties. At least if you're willing to spend a bit of effort.

Regards,
Sven

Reply by email

James Richters
to Sven Barth

I’m curious how use classes together with published properties would work, is there a sample of how to use this somewhere?

James

Earlier message 23 lines
From: fpc-pascal On Behalf Of Sven Barth via fpc-pascal Sent: Monday, July 8, 2019 5:31 PM To: FPC-Pascal users discussions Cc: Sven Barth Subject: Re: [fpc-pascal] specify variable name with string variable James Richters < > schrieb am Mo., 8. Juli 2019, 22:44: SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ; And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin That indeed sounds like something where you could use classes together with published properties. At least if you're willing to spend a bit of effort. Regards, Sven

Reply by email

Stefan V. Pantazi
to James Richters

Haev a look at rtti and the functionality available in the typinfo unit. The tests included in fpc are a good resource. Have a look at the trtti*.pp tests:
https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/tests/test/

Start with trtti4.pp and work your way up to more complex examples.

James Richters wrote 36 lines
I’m curious how use classes together with published properties would work, is there a sample of how to use this somewhere? James From: fpc-pascal On Behalf Of Sven Barth via fpc-pascal Sent: Monday, July 8, 2019 5:31 PM To: FPC-Pascal users discussions Cc: Sven Barth Subject: Re: [fpc-pascal] specify variable name with string variable James Richters < > schrieb am Mo., 8. Juli 2019, 22:44: SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ; And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin That indeed sounds like something where you could use classes together with published properties. At least if you're willing to spend a bit of effort. Regards, Sven _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________________

Reply by email

Santiago A.
to James Richters: Well what it’s for, is not just debugging, I’m trying to manage outputs to ports…
James Richters wrote
ports that are defined by port numbers and pins in an ini file,

Well, then they are dynamic. They are not hardcoded.

I would use something like:

TPortIdentifier=(Pin_1, pin_2, Port_x, Port_y...);
TIOPort=object
        private
           FPortPin:boolean;
           FPortByte:boolean;
           procedure SetPortPin(value:boolean);
           procedure SetPortByte(value:byte);
        public

          OtherData:... // pin, port etc or other data

           PortAdress:PWord; // pointer to memory
           Name:string;
           constructor Init(aName:name;aPword:pWord);
           property  PortPin:boolean read fPortPin write SetPortPin;
           property  PortByte:byte read fPortByte write SetPortByte;
         end;
   TPortList=array[TPortIdentifier] of TIOPort;
var
  PortList:TPortList;
begin
   // fill the array with name and values from Ini file
    PorList[Pin_1].init('Pin-1',@xxxx);
    PorList[Pin_2].init('Pin-2',@xxxx);
   // call
   PortList[pin_1].PortPin:=true;
   PortList[pin_2].PortPin:=false;
end.

Or you can use a TFPGMap from the standard library fgl.

Saludos Santiago A.

Reply by email