Recent

Author Topic: Where to initialize a global variable?  (Read 1960 times)

JamesSci

  • New Member
  • *
  • Posts: 30
Where to initialize a global variable?
« on: February 23, 2020, 11:47:06 pm »
I have a variable/constant that I want used between forms and I only want to initialize it once. So, to do this, I thought this would do the trick:

Code: Pascal  [Select][+][-]
  1. var
  2.   Usr: string = GetEnvironmentVariable('USERNAME');

But apparently that's an illegal expression. How should I do this instead?

Also, is an environment variable likely to be a variable or a constant? [EDIT: I just realized how stupid that sounds, but I'll leave it in]

Thanks!
« Last Edit: February 23, 2020, 11:50:50 pm by JamesSci »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Where to initialize a global variable?
« Reply #1 on: February 23, 2020, 11:56:12 pm »
You can use the initialization section of the unit that variable is in.

Bart

JamesSci

  • New Member
  • *
  • Posts: 30
Re: Where to initialize a global variable?
« Reply #2 on: February 24, 2020, 02:07:10 am »
Thank you!

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Where to initialize a global variable?
« Reply #3 on: February 24, 2020, 03:12:26 am »
I have similar issue. Problem is, I'd like to use the same unit in different projects, and the values of the variable are different between projects. In this case, I cannot define it in the initialization section of the unit.  So, I'm thinking setting it at the project file, which starts with

Code: Pascal  [Select][+][-]
  1. program MyProject1;
  2. uses ......
  3. begin
  4.     Application.Initialize;
  5.     Application.CreateForm (TForm1, form1);
  6.     MyGlobalVariable := 'Your name';  // A Variable defined in TForm1, or unit1, etc.
  7.     Application.Run;  
  8. end.

But I'm not sure whether this is a good idea.  Up to now there is no problem. But still waiting for better idea or advise.

Or what about defining variable below the program, which is available to all units within the project?  (maybe in addition to conditional compiler directives?)

Wayne Dixon

  • Newbie
  • Posts: 4
Re: Where to initialize a global variable?
« Reply #4 on: February 24, 2020, 03:45:43 am »
Hello. I have an answer to your question as to where to put your global variables. I am currently am writing my own desktop suite using the Lazarus IDE complex. I am also am writing a Programmers Tool Box program at the same time. What I did when i began to write my address book program application. I created a separate blank file just to store all of the common variables that is used commonly throughout this program. Since the program i am writing has its own file manager. I placed all of the common variables i used in that file. Thus if I were to use this same file within another part of the project then I would specify that file in the uses clause of that program or via unit file. I would not put it in the main lpr file as it would become problematic as portion of program is in need of accessing it. If your program is a one file program than there would be no problem.

I am hoping to this answers your question.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Where to initialize a global variable?
« Reply #5 on: February 24, 2020, 04:17:11 am »
Quote
I created a separate blank file just to store all of the common variables that is used commonly throughout this program.

This could be a solution. I have a separate file containing all constants shared by all the units within a project. I may put global variables here as well.

Wayne Dixon

  • Newbie
  • Posts: 4
Re: Where to initialize a global variable?
« Reply #6 on: February 24, 2020, 04:40:26 am »
After two program applications in development this has worked without issues. Later i am going ot take all of those commonly used variables and create a class just for them just to distinguish them from local variables. After having my second program as having a count of 21000 lines of code it worked successfully unabated. It is one of some nine apps that is to accompany this small desktop suite.

 

TinyPortal © 2005-2018