Home > database >  Accessing types defined in the implementation part of another unit
Accessing types defined in the implementation part of another unit

Time:11-05

I'm in Delphi Firemonkey subclassing the FMX.Calendar.Style in order to add more functionality to the TCalendar component. I want to add 'drag select' multiple days (a day range). I have a rough working proof of principle. I'm subclassing TStyledCalendar in FMX.Calendar.Style in another unit.

In order to extract a selected day, I need to cast to TDayItem. The problem is that the type TDayItem in defined in the implementation part and therefore not accessible in the other unit with the new class.

Is there a way to access it anyway?

unit FMX.Calendar.Style;

interface

{$SCOPEDENUMS ON}

uses
  System.Classes, .., ..

type

{ TStyledCalendar }

  TStyledCalendar = class(TStyledPresentation)
  public const
    CountOfVisibleDays = 6 * 7;
..
..
..
  end;

implementation

uses
  System.SysUtils, .. ..

type
  TDayItem = class(TListBoxItem)
  private
    FDate: TDateTime;
  public
    property Date: TDateTime read FDate write FDate;
  end;

..

..

CodePudding user response:

Unfortunately, there is simply no way for a unit to access a type (or a variable, or a constant) that is declared in another unit's implementation section. Only items that are declared in a unit's interface section are accessible to other units. A unit's implementation section is accessible only to the unit which it belongs to.

In this situation, you have two choices:

  • copy the declaration of TDayItem into your own unit, and then type-cast the ListBox items to that type as needed. And then check FMX's declaration of TDayItem for changes in every new release/patch so you can update your code accordingly.

  • make a copy of FMX.Calander.Style.pas and add it to your project. Then, you can freely alter the copy's implementation (but not its interface!) however you want. During compiling, your copied unit will take priority over the original unit. Note: this approach only works if you are NOT building with Runtime Packages enabled.

  • Related