delphidelphi-5toolsapi

Delphi 5 component that will automatically add "db.pas" unit to uses clause?


I'm writing my own component, and I wish it to add "db" unit to the interface uses clause when ever I drop it on the form, since it has a published event like:

TMyDBEvent = procedure(Sender: TObject; DataSet: TDataSet) of object;

TDataSet is declared in db.pas, and I need to add this unit manually, which I want to avoid.

I have seen this solution:

How are Delphi units automatically added when a component is added to a form?

And this:

Can I make a custom Delphi component add multiple units to the uses clause?

That use RegisterSelectionEditor, but Delphi 5 (I know...) seems to not have this unit.

What are my options?


Solution

  • You can alias the type within your component's unit using an identical name:

    type
      TDataSet = Db.TDataSet;
    

    Whenever you drop your component on a form, Delphi should add its unit to the uses clause. Then whether or not you use Db in that form, there's a valid TDataSet equivalent to the one from Db.

    Could there be any side effects/implications/conflicts issues to such type aliasing?

    From a language perspective, no.

    Generally it's ill-advised to have types with the same name as this can cause problems when 2 pieces of code (sometimes even within the same unit) seem to use the same type, but they are actually different types internally. However, in this case the types are actually the same.

    From an IDE perspective, not much.

    Are there any other known components that uses this trick?

    I don't know of any components using the technique. But I've used aliasing for various reasons on quite a few occasions. Primarily just to avoid forcing knock-on dependencies on client units.