Delphi blackbox technique

This article explains how to create forms/units independent of other forms/units object names in a delphi project.

Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi project. In Delphi this is easy, just use the "Add file to project (Shift+F11)" to add the unit from the other project,
add name of the unit to your other unit uses implementation and the form is available in this project. But then ... you hit compile ... the compiler starts complaining with errors like

Let's consider the following problem: The application needs a name and description for a new article from the user.

Most Delphi programmers would solve this problem with a form like this:
Create a new Form, give it a new name (frmNewArticle), put a TEdit (editArticleName), a TMemo (memoArticleDescr) and a button (btnOk, modalresult = mrOk) on it, and then call that new form from the main form like this:

This works, no problem... But consider what would happen when a other developer decides to:
Change the names of the edit and memo
- editArticleName to edName
- memoArticleDescr to edDescr

Change the name of the form (and type)
- frmNewArticle to frmBasicInput

This would force you into rewriting parts or your project too. And off course, nobody likes that. But in which way can we program so this doesn't happen? Well, you guessed it, with the blackbox technique.

The first thing you need to do is change the way the input form is addressed by using a function. Place that function in the user input form

Now what would happen if somebody were to change the names of the edit and memo? The compiler would complain too, but it would be in the unit where the names changed, and not in your unit! A good programmer would solve the compiler errors in the function you inserted, and thus solving the problem for you as well.

Now let's extend on this. If we were to change the form and add a edit for a barcode number, and if we wanted this barcode in our main form, we would have to change our function header too. A solution could be :

This is leading to a different problem: the parameters in the header of the function/procedure.

A more adaptable approach for the parameters would be using a TStrings to pass/retreive info. Let's change the function so it has only a TStrings as parameter (you want to change the contents of the TStrings, not the parameter pointer, so no var )

This is a simple demo of a blackbox technique using a function with parameter TStrings. The basic idea is to put the function/procedure in the interface section of the same unit.

If anybody changes names of the form or it's components then the developer will get warnings at compile time about any extra code changes needed. With this you can make units using this form independent of the implementation of the name of the form or its component names.

Using this article for work? Experts Exchange can benefit your whole team.

Experts Exchange is a tech solutions provider where users receive personalized tech help from vetted certified professionals. These industry professionals also write and publish relevant articles on our site.

Ask questions about what you read

If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.