I have a TestProgram (TestVersions) and a set of three units (ModelUnit, Unit1, Unit2). All the units are 'identical' except for their name (see attached project). I want to develop the ModelUnit so that at any point I can call <UnitName<>.GetVersion, and get the current version of that Unit.
Significant Code is included here (as well as in the attachment - TestVersions.zzip).
ModelUnit
Unit ModelUnit;
Interface
const VERSIONLINE = '*.00.000:000';
const VERSIONDATE = '2025-11-01';
Function GetVersionLine(): string;
Function GetVersionDate() : String;
Implementation
Function GetVersionLine () : string;
begin
GetVersionLine := VERSIONLINE;
end;
Function GetVersionDate () : string;
begin
GetVersionDate := VERSIONDATE;
end;
end.
Unit1 & Unit 2 are identical except for the name and the last digit in the Version string
TestVersion
program TestVersions;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
Cthreads,
{$ENDIF}
Classes, Sysutils, Custapp,
ModelUnit, Unit1, Unit2
{ you can add units after this };
type
{ TVersions }
{Lazarus boilerplate excluded - it's in the attachment}
writeln('Unqialified reference (this might fail!)');
writeln(GetVersionLine());
writeln(GetVersionDate());
writeln();
writeln('Access Unit1');
writeln(Unit1.GetVersionLine());
writeln(Unit1.GetVersionDate());
writeln();
writeln('Access Unit2');
writeln(Unit2.GetVersionLine());
writeln(Unit2.GetVersionDate());
writeln();
write();
readln();
end.
When I run TestVersions I get the identical value from GetVersion - when I hope to get the value from the Unit which is specified.
The problem, as I understand it is that the
first reference to GetVersionLine becomes the only reference available. I think that Ada has something along the lines of
which allows only the qualified reference (as I have in TestVersions).
I hope my goal is clear - is there an easier way to accomplish it?
* I
dowant to have the same identifier in each unit, so that I can simply cycle through the units in any application and pull out the relevant data.
* A certain stubbornness on my part wants to keep the constant definitions as close to the front of a source file as possible, so that its position can be predicted by any tools which use this feature.
I'd appreciate any comments, feedback or suggestions!
Thanks,
Tony