Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 891
zig Avatar asked Oct 30 '22 09:10

zig


1 Answers

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.

  • A minor issue is that "Find Declaration" in the IDE will require an extra step to reach the underlying type.
  • The other issue is related to code completion. I have experienced that some versions of Delphi would occasionally struggle with code completion when this slightly unusual referencing technique <unit-name>.<type-name> is used. (I don't recall how (or even if) Delphi 5 was affected though.)

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.

like image 53
Disillusioned Avatar answered Nov 29 '22 09:11

Disillusioned