English 中文(简体)
Nested record information hiding in Delphi
原标题:

I have a relatively complicated data structure to model. I would like to do this with a record structure in Delphi, and the structure is complicated enough to justify splitting this into nested records. A simplified example:

    type
      TVertAngle = record
      strict private
        fDecDegrees: Double;
        fDegrees: integer;
        fMinutes: integer;
        fDeciSeconds: integer;
        function GetAngle: Double;
        function GetRadians: Double;
      public
        Valid: Boolean;
        procedure SetAsString(const Value: string; const AngleType: TInfoUnits);
        property DecDegrees: Double read GetAngle;
        property Radians: Double read GetRadians;
      end;

~~~~ other sub record declarations ~~~~~~

  TDataRecord = record
  strict private
    fHorzDistance: Double;
    fLeicaData: TRawMessageData;
    fUpdateTime: TDateTime;
    function DecodeGsi8(GsiWord: string): TGSiWord;
    function DecodeGsi16(GsiWord: string): TGSiWord;
  public
    GsiWord: TGSiWord;
    Valid: Boolean;
    InputMode: TDataModes;
    HorzAngle: THorzAngle;
    VertAngle: TVertAngle;
    HorzRange: TDistance;
    SlopeRange: TDistance;
    PrismOffset: TConstants;
~~~~ other sub record instances~~~~~~
    function SetMessage(RawMessage: string): Boolean;
~~~~ more stuff ~~~~~~

I currently have all this declared in the Interface section of the unit. I would prefer if only the main record structure was visible to anything using the unit, and at the moment all the sub records are also visible. If I move the record declarations into the Implementation section then I get compiler errors. How do I restructure so that I declare the sub-records prior to the main record but the sub-records are not published?

最佳回答

You can do one of the following:

1) Declare "sub records" in a separate "sub unit", so the "sub record" types are available only if the "sub unit" is declared in the "uses" clause. That is not exactly what you are looking for, since the "sub records" can be made visible for other units, but it provides some degree of hiding since the "sub unit" must be explicitely declared to reach "sub record" definitions.

2) Declare "sub records" as private nested types as follows:

type
  TMainRec = record
    private type
      TSubRec = record
        FSubField: Integer;
        procedure SubMethod;
      end;
    private
      FSubRec: TSubRec;
  end;

implementation

{ TMainRec.TSubRec }

procedure TMainRec.TSubRec.SubMethod;
begin
...
end;
问题回答

You can t do that, because even if the information doesn t need to be directly available to other code outside the unit, the compiler still needs to know about the sub-record types if you re going to use the main record somewhere.

One thing you could try is declaring the other types in another unit and then not using that other unit anywhere else. That doesn t really solve the problem, though; it just hides it a little.





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签