[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Is there any general rules to use a UNIT instead of a FORM?

Hello all
What is best practice when using a UNIT?
Why not always use a FORM and put all of the code in it?
Is there any general rules to use a UNIT instead of a FORM?

Lior
0
liorb
Asked:
liorb
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
esoftbgCommented:
There is no reason to be opposed UNIT and FORM because:
1. every FORM .dfm has an related UNIT .pas => in the .dfm file are described all the components dropped on the FORM, but in the .pas file are described all the methods for these components;
2. but it is possible to be used UNIT without related FORM - this UNIT may contain abstract methods that are not related with a known FORM - they could be used with any FORM or any component or more abstract stuff (work with strings, arrays, ....);
0
 
esoftbgCommented:
Every component is developed this way that it could be dropped on every FORM, so during the component development programmers use only UNIT(s). In those UNIT(s) are declared CLASSES, METHODS, CONSTANTS, VARIABLES, PROPERTIES.
0
 
esoftbgCommented:
We use FORMS during a PROJECT development for all the stuff that must be visualized - VCL. If there is some methods that are more common and are not related with a FORM(s), they could be described in a independent UNIT(s) whitout a FORM(s).
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
liorbAuthor Commented:
Well as I understand there is no "General Rule" to spilt the code to a FORM and a UNIT and there is no "MUST".
It just  a way to keep things in order.
All the "visibles" on the FORM, and all the other "things" that help the program to go on are on the UNIT.
Am I right?
0
 
mikelittlewoodCommented:
That is correct liorb.
A Data module can be thought of as similar to a unit as it doesnt have visual components, but it usually holds any data connections, i.e. databases.
0
 
VoodoomanCommented:
Hi

I use seperate Units for re-usable code that I use in all my projects.

I have a unit called usefulFunctions which has some stuff I use all the time and in every project.

Here is a simple procedure I use to show and hide the hourglass - just one of my usefulFunction that I have been using for at least 10 years in all my project!!!!!!!.

Procedure Wait(WState:Boolean);

  Begin

    //Show and hide the hourglass
    //***************************
    if Wstate= True then Screen.Cursor:=crHourglass else Screen.Cursor:=crDefault;


  end;

Wait(true); //shows the hourglass

Wait(false);//hides the hourglass

Likewise to show a Message

Procedure IMessage(IMessage:string);

begin
 
 Screen.Cursor:=crDefault;
 MessageDlg(Imessage, mtInformation, [mbOK], 0);
 Screen.Cursor:=crDefault;

end;

I just call

IMessage('Please enter a title for the new project');

I have lots of others that save effort in other units softwareReg.pas, encrpyt.pas etc etc.

In bigger projects, it is critical to be organised in this way otherwise you will get completely lost when you need to make changes later.

Voodooman







0
 
liorbAuthor Commented:
VOODOOMAN

That is a great idea which I'm plan to adopt.

But I'm a begginer so what you wrote is a little complicated at this moment.

Though the general idea is logical and good to implement.

Thanks,

Lior.
0
 
VoodoomanCommented:
Hi Lior

I have trained several programmers in my small business, all of whom went on to get good jobs.

A little work on getting the basics right now will make programming easy in the future.

You need to have a good stucture in your work like in any other job.

If you were a builder building a house, you would put in the foundations, build the walls, put the roof on etc.  Programming is exactly the same, get the sequence and the foundations right and keep the job tidy and you will get the job right.

Good Luck

Voodooman
0
 
esoftbgCommented:
Hi liorb,
Thank you for the points, but I think I was not clear enough ....

You are not absolutely right .... Why ?
> Well as I understand there is no "General Rule" to spilt the code to a FORM and a UNIT and there is no "MUST".
   1. There is a "General Rule" made by BORLAND DELPHI about the FORMs usage:
   1.1. FORM (.dfm file) is a storage for the information about the PROPERTIES of the Form and all the Visible and not Visible components dropped on it - THAT IS NOT PASCAL CODE - that is some internal code for DELPHI's IDE. You don't write this code directly in the .dfm file, but when you change some componet's property Delphi's IDE saves the change in the .dfm file
  1.2. There is a "MUST" - Every form MUST contain both of them .dfm and .pas files: .dfm - storage, .pas - methods.

  2. There is not a "General Rule" made by BORLAND DELPHI about the UNITs usage:
  2.1. You may write Form-independant methods into UNITs that are related with a FORM;
  2.2. You may write Form-independant methods into UNITs that are NOT related with a FORM - (Separate UNITs)

Emil
0
 
liorbAuthor Commented:
VOODOOMAN
A good foundations make a graet professional.
And I try to earn them now.
I'll be glad to here from you in my future questions and be sure there will be many more :-)

Emil
I understand that this is not a boolean { :D } situation.
And I say as the experience will come your words will be more clear.
What you noted here will be my "General Rules" for the time being.
Until I'll find my own methods or maybe they will be the same as yours.

Regards to you all.

Lior.

0
 
esoftbgCommented:
Lior,
I wish you success in your way ahead to become more experienced.
Nobody know all, me too ;-))
Best Regards,
Emil
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now