Anatomy of a Delphi Unit




In the Beginning...

Back in 1994 or so, Borland began working on a RAD tool that it code-named Delphi. When it was decided that the component model architecture was the best way to implement RAD, it was then necessary to settle on the programming language that would be the heart of the system.

At that time, Borland was the only compiler vendor mass marketing a Pascal compiler. Borland was known as the company that produced the best Pascal tools. If you were a Pascal programmer, you probably used Borland's TurboPascal in one flavor or a nother. Borland more or less "owned" Pascal. Although Borland didn't own the Pascal language in a legal sense, it no doubt felt that because of its position in the Pascal world, it could take considerable liberties in implementing new language features and enhancements. In addition, there was no Pascal standards committee, nor even a written standard defining the Pascal language. So Borland created Delphi using Pascal as the base language (the Borland internal code name stuck and became the official product name).

Before Delphi came into being, Borland had already modified the Pascal language in positive ways. For example, Borland had already extended Pascal by creating a new language called Object Pascal. It can be said that Object Pascal is to Pascal what C++ is to C. Object Pascal added classes to Pascal, thereby hurling Pascal into the world of object-oriented programming (OOP) languages. As Delphi was being developed, new language behavior and keywords were added to deal with the component model. Keywords such as published and property were added, as were others. This enabled Borland to fully implement the power of the component model. By modifying the Pascal language to suit the component model, Borland was able to implement RAD the right way. In essence, the Object Pascal language was modified as needed when design issues came up during the development of the then-unknown product called Delphi. The result is a language that works seamlessly with the component model.

Although modifying the Pascal language could be considered a bold step for Borland, it was not without precedent. Previously, Microsoft had taken the BASIC language and modified it to produce a new language called Visual Basic. This new language was nearly unrecognizable when compared to the original BASIC language that served as its base.

Borland took a risk in modifying Pascal. After all, it had a loyal base of customers that might not take kindly to enhancements to the language they had come to know and love. S till, Borland was in a solid position in the Pascal market and went ahead with its plans. The result was a smash hit, of course.

Make no mistake about it, Object Pascal is a powerful programming language, and I don't make that statement lightly. I have a C/C++ background and, like other C/C++ programmers, I viewed Delphi with a bit of skepticism at first. I found out quickly, though, that the Object Pascal language is very capable. In fact, in the hands of the average programmer there is almost no difference in the two languages in terms of power. Object Pascal is unique in that it is both powerful and relatively easy to learn. I don't in any way want to leave the impression that Object Pascal is a not a full-featured programming language. Pascal has often been knocked as a less-than-serious programming language. That has never been true, and is even less true with today's Object Pascal.

 

Object Pascal enables you to take advantage of object-oriented programming to its fullest. OOP is not just a buzzword. It has real benefits because it enables you to create objects that can be used in your current program and reused in future programs.

Types of Units

A Delphi GUI application will contain at least two units. The project source unit contains the project source code. Project source code units have an extension of DPR. You can view the project source unit by choosing Project | View Source from the main menu. It is not normally necessary to modify the project source unit. In fact, you shouldn't modify the project source unit unless you know exactly what you are doing. If you accidentally modify the project source unit in undesirable ways, you might find that your applic ation won't compile anymore. (Certain advanced programming techniques require modification of the project source code, but that's not something you need to be concerned with at this time.)

Anatomy of a Delphi Unit

Delphi units must follow a predefined format. This shouldn't come as a surprise to you. The unit has to be in a predefined format so that the compiler can read the unit and compile the unit's code.

A Delphi project unit contains the program keyword followed by the name of the unit and a code block marked by the begin and end keywords. You can see how a basic unit looks by choosing View | Project Source from the Delphi main menu.

Let's take a look at another basic Pascal unit. Choose File | New from the main menu. When the New Items dialog comes up, locate the icon labeled Unit and double-click it. Delphi will create a new unit and display it in the Code Editor. Listing 1.2 shows the code generated for this unit.

The interface Section

Notice that this listing has a section marked by the interface keyword. This keyword marks the start of the interface section for the unit.

The interface section is the section of a unit in which identifiers exported from this unit are declared. An exported identifier is one that can be accessed by other units in the project.

Most units will contain code that other units use. The code might be implemented as a class, a procedure, a function, or a data variable. Any objects that are available to other units from this unit must be declared in the interface section. You could say that the interface section contains a list of items in this unit that other units can use. The interface section starts with the interface keyword and ends at the implementation keyword.



Поделиться:




Поиск по сайту

©2015-2024 poisk-ru.ru
Все права принадлежать их авторам. Данный сайт не претендует на авторства, а предоставляет бесплатное использование.
Дата создания страницы: 2016-04-11 Нарушение авторских прав и Нарушение персональных данных


Поиск по сайту: