Get resourcestring identifier from implementation area

579 Views Asked by At

I have a unit which has a resourcestring in its implementation section. How can I get the resourcestring's identifier in another unit?

unit Unit2;

interface

implementation

resourcestring
  SampleStr = 'Sample';

end.

If it is available in the interface section, I can write this:

PResStringRec(@SampleStr).Identifier
1

There are 1 best solutions below

2
On

Anything declared in a unit's implementation section is private to the unit. It CANNOT be accessed directly from another unit. So, you will have to either:

  1. move the resourcestring to the interface section:

    unit Unit2;
    
    interface
    
    resourcestring
      SampleStr = 'Sample';
    
    implementation
    
    end.
    

    uses
      Unit2;
    
    ID := PResStringRec(@Unit2.SampleStr).Identifier;
    
  2. leave the resourcestring in the implementation section, and declare a function in the interface section to return the identifier:

    unit Unit2;
    
    interface
    
    function GetSampleStrResID: Integer;
    
    implementation
    
    resourcestring
      SampleStr = 'Sample';
    
    function GetSampleStrResID: Integer;
    begin
      Result := PResStringRec(@SampleStr).Identifier;
    end;
    
    end.
    

    uses
      Unit2;
    
    ID := GetSampleStrResID;