-4

Maybe I miss something, but is their a way to declare a global var in Delphi with an Initial value?

var MyGlobalVar: integer := 16;

This not work (but do work when the var is inlined in the code)

zeus
  • 12,173
  • 9
  • 63
  • 184
  • 1
    There is a difference (why?) between the syntax used for global variable initialization and local inline variable initialization. The global one uses "=" as assignment operator, the local inline one uses ":=" as assignment operator. – HeartWare Nov 17 '21 at 12:56
  • 3
    (Small) part of the answer: global variables with default values have been part of the language "forever", while inline variable declarations were introduced only a year or two ago. Also, notice that the global variable expression must be a constant expression, while the value given to a new inline-declared variable can be given by any (runtime) expression. – Andreas Rejbrand Nov 17 '21 at 12:59
  • 1
    https://stackoverflow.com/questions/21403628/how-can-i-search-for-delphi-documentation – David Heffernan Nov 17 '21 at 14:09

1 Answers1

7

Yes. From the documentation on variables:

Global variables can be initialized at the same time they are declared, using the syntax:

var identifier: type = constantExpression;

where constantExpression is any constant expression representing a value of type type.

In your case,

var MyGlobalVar: Integer = 16;
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384