Because there are two uses in Delhi

Asked

Viewed 956 times

6

Because there are two uses in the Units of Delphi ?

Just below interface and soon another in implementation, what the purpose?

3 answers

5

The top to use system resources and components, such as: System.Classes, System.SysUtils, Vcl.Dialogs, Data.DB etc, etc..

The bottom serves to use another’s resources Units as: ufuncoes, uTelaInicial etc etc etc.

The pattern is like this, but nothing prevents you from using the bottom uses up there or vice versa! But... not all are flowers, can give both compilation error of execution, so by default, use the top for System Uses Declarations and Components, and the bottom for uses of Units of your project!

Remembering that declaring the same uses above and below will cause a Duplicate Declaration error!

  • 2

    I disagree somewhat with his statement. Don’t necessarily use it from interface is for system resources and components. Until because if I have a simple class, where it is necessary to make a StrToInt, I won’t need to use the SysUtils in interface. And another, I may have a method where the return is a specific Object, in which case I would have to declare my class in the Interface to use it

  • 1

    You may disagree with "Thinking"! For, just open a simple project and add components that use system resources such as a Dbgrid or others, the automatic declaration will be in the Interface. And when using Alt+F11 will be added in Implementation, The Default that "Affirmed" is this!

  • I’m gonna create a record helper String, where I will implement 2 methods, SaveToFile and LoadFromFile. For this I will use TStringStream. No need to declare System.Classes and System.SysUtils in Interface

5


I believe that the leaner the uses in Interface, the better.

This is because you’ll be able to make more explicit what your interface really needs, and what your code will implement

The classes defined in the uses form a link between the Units where they say what they can do. Basically that’s where they communicate.

On the interface you say what they can do.

The implementation is where you say what you need to do.

  • Just to complement, I found this link http://stackoverflow.com/questions/10125641/differences-in-uses-clause

-3

Organization only. The first one at the beginning is for Delphi libraries that the project needs. The second is for the files drives of your project, Unit of a form for example.

If you only use the first way it works the same way.

In summary the first for Delphi Units the second for your Units.

Okay. I hope I helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.