Delphi 5, which will automatically add the "db.pas" block to the uses section?

I am writing my own component, and I want it to add the "db" block to the interface uses clause when I ever throw it in the form, since it has a published event like:

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

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

I saw this solution:

How are Delphi blocks automatically added when adding a component to a form?

And this:

Can I create a Delphi component to add multiple units to a uses clause?

This is using RegisterSelectionEditor , but Delphi 5 (I know ...) does not seem to have this device.

What are my options?

+5
source share
1 answer

You can pseudonize a type inside your component using an identical name:

 type TDataSet = Db.TDataSet; 

Whenever you drop your component on the form, Delphi must add its block to the uses clause. Then, regardless of whether you use Db in this form, a valid TDataSet equivalent to 1 << 21>.

Could there be any side effects / consequences / conflict problems for such type aliases?

In terms of language, no.

As a rule, it is not recommended to have types with the same name as this can cause problems when two pieces of code (sometimes even within the same device) seem to use the same type, but they are actually different types inside. However, in this case, the types are actually the same .

From the point of view of the IDE, not so much.

  • An insignificant problem is that for “Declaration Search” in the IDE, an additional step is required to get the base type.
  • Another problem is code completion. I experienced that some versions of Delphi will sometimes struggle with code completion when this slightly unusual <unit-name>.<type-name> binding method is used. (I don’t remember how (or even if) Delphi 5 was affected.)

Are there other known components that use this trick?

I do not know any components using this technique. But I used aliasing for various reasons quite often. First of all, to avoid forced dependencies on client blocks.

+2
source

Source: https://habr.com/ru/post/1266223/


All Articles