[fpc-pascal] Selecting Records with a variable

Luca Olivetti luca at ventoso.org
Sun Dec 20 16:11:19 CET 2020


El 20/12/20 a les 16:02, James Richters via fpc-pascal ha escrit:
> What I’m hopping to accomplish isn't to get it to work... it's to consolidate a whole bunch of
> functions and procedures that are all exactly the same except for which axis I am working with...
> 
> I have 9 possible Axis, X,Y,Z,W,L.R,A,B,C

....

> ...
> Same thing 7 more times
> ...
> 
> 
> This is a tedious situation.  If I want to change something I have to copy and paste it 9 times then go fix all the variables 9 times.. and it's easy to make a mistake.

Then change your data model.

Instead of

Axis_record = record
   X,Y,Z,A,B,C    : Double;
End;

use

AxisName = (X,Y,Z,A,B,C);
Axis_record = array[AxisName] of double;

then it's easy to do what you want.

procedure DoSomething(var Axis:Axis_record; const which:AxisName);
begin
   Axis[which]:=Axis[which]/2;
end;

Bye
-- 
Luca


More information about the fpc-pascal mailing list