1 (edited by reteinformatica 2023-05-06 20:51:37)

Topic: Library for multiple projects

Good morning everyone and thank you for taking an interest in my request.
Going forward I realize that there are necessary pieces of code that can be used on various projects and that they are identical. A practical case could be that of translating messagebox.
According to an easy approach, a copy and paste would suffice.
According to a stylistic approach, however, it would seem more appropriate to create a library file that can be recalled in each project. Tell me if I'm talking nonsense.
Considering the Pascal course I'm following and also my hanging skills, if I understand correctly I should create a .pas file and declare it in the project in 'uses'.
Can you help me understand what the method is?
Thank you and greetings.

Re: Library for multiple projects

As you probably already know, My Visual Database allows you to place script sources in several files, which is very convenient if the number of lines of code exceeds a couple of thousand. In order to connect a module (a file with the .pas extension) to the main code, you must specify it in the uses:

uses
   // sort by degree of dependency: first those that do not refer to anyone, then dependent
   'ConstVar.pas', // global constants and application variables
   'System\Utils.pas', // system procedures
   'Tools\Tools.pas', // tools
   'VClass\VClass.pas', // virtual classes and extensions
   'Forms\Forms.pas', // forms
   'UserApp.pas'; // general procedures and application functions

A prerequisite is that the module file is located inside the Script folder, nested storage of module files in subfolders is also allowed.


Unfortunately, the built-in MVDB editor does not support editing modules, but the solution was easily found in the person of the universal editor Notepad ++, which is used by many programmers. As you can see from the project file tree, there are quite a lot of them, and only six are present in the uses command. The remaining modules are connected in the modules described above, they also contain the uses command for files. For example, the Forms.pas file is used solely to register modules that store handlers.


Read more: https://k245.ru/en/mvdb-en/butterfly-effect.html

Визуальное программирование: блог и телеграм-канал.

Re: Library for multiple projects

Hi k245,
thanks for the reply, actually i thought so but i didn't know for sure that you could put scripts in other files.
I didn't quite understand, however, if it is as I think, i.e. these files must have the .pas extension. From what you told me I think I understood the correctness of what I thought, that these files must be declared in 'uses'.
What then is not clear to me what is the code to call these files.

Re: Library for multiple projects

There is no need to "call" anything in any special way. It is enough that you have specified an external file in the uses section.
When compiling the program, all scripts are collected into one final file script.dcu.


A little more difficult to deal with nesting. As I understand it, the MVDB tries to consistently check identifiers, and if an identifier is declared in a module, a link to which it has not yet managed to find, then there will be an error. Circular references are not allowed. The order in which references are declared is also important: independent modules must be declared first, and then modules that depend on previously declared ones.

Визуальное программирование: блог и телеграм-канал.

Re: Library for multiple projects

I forgot to say that modules do not limit the scope: all declarations (procedures, functions, variables and constants) are global.

Визуальное программирование: блог и телеграм-канал.

Re: Library for multiple projects

I created a file called 'trad.pas' inside the 'scripts' folder.
Then I wrote in a project this:

uses trad;

It gives me this error: Identifier expected at 1:5.
Cursor in front of 'Trad'.

Re: Library for multiple projects

Hi Fabio, Konstantin,
I'm guessing that 'trad' is short for 'tradurre' and you're going to put all your translations in a separate script.
Enter it as follows:

uses  'trad.pas';

Regards,
Derek.

8 (edited by reteinformatica 2023-05-08 21:11:53)

Re: Library for multiple projects

Hi Derek, it's just as you guessed: I created a 'trad.pas' file where trad stands for 'tradurre', where I put the code for all the translations.
Now it works I was missing the '' and then I also had to put the .pas extension otherwise it would tell me it couldn't find the file.
Thank you so much derek, as always with the solution ready.
What do you think, for projects with little code is a library file better or a simple copy and paste would be better?

Re: Library for multiple projects

Hi Fabio,
You will probably end up with a combination of code that is:
a)  stored in separate 'uses xxx.pas' files (if my applications were in any language other than English, I would probably use a 'trad.pas' as you are thinking about doing to hold all the translations).
b)  is copied and pasted from other scripts (either your own or taken from messages in the Forum)
c)  is written specifically for each project.
And I don't see any issue with doing this - perhaps it's not 'theoretically correct' but this 'mixed approach' is a practical one.
What I also find helpful is to make a copy of an empty (template) application (no data schema and no script) that just has 'standard' forms with all of the components that I commonly use already configured to how I like them. 
For example
*  most of the applications all have the same 'look and feel' with regard to layout
*  labels use the same font size, color etc
*  tablegrids have a pre-set header size, footer size, font size etc
*  buttons are all the same size and use consistent icons / symbols where possible
*  buttons are configured with 'hints' already written
*  the default cursor is set to 'crhand' when you place the mouse on buttons 
*  data dictionary tables are all accessed via a 'form set-up' using editable tablegrids
*  the 'template' application comes with an 'about' screen already set up.
*  I use a form just while I'm writing an application that contains most of the objects I need so I can just copy and paste them as required.
It saves so much time and means you can concentrate on the things that are specific to your new project - but I'm sure many of us already do something similar.
It also means that if people already use some of your other applications, then they should also feel comfortable and familiar with your new program. 
Attached is my 'template' which might give you some ideas if you want to create one of your own. 
Regards,

Post's attachments

Attachment icon template.zip 569.73 kb, 160 downloads since 2023-05-09 

Re: Library for multiple projects

Hi Derek, Thanks for the usual clear and thorough explanations.
As often happens, you anticipate my questions, in fact I was just thinking about how it should have been done for a form model, the ones that are often used as the information form. Thank you very much!

11 (edited by reteinformatica 2023-05-09 19:15:20)

Re: Library for multiple projects

Now a doubt has come to me: In the .pas file I created I had it start with 'begin' and end with 'end.'
Is this the correct way or should it be ended with end;? Or without begin and end?
end. could not go to interfere with the end. of the main project?

Re: Library for multiple projects

Each file must contain a begin - and section. With a dot at the end, this is very important!

begin
end.
Визуальное программирование: блог и телеграм-канал.