Can I use a Const as the upper bound of an array ? lazarus / 61-Beginners ---------------------------------------------------------------------- Dasun · 2016-06-28 02:58 All, Appreciate any feedback ... I was wanting to use a constant to set the upperbound of a static array but the compiler does not allow. Is the following - or something similar possible? Const     Upperbound : Integer = 10 ; Type AnArray = Array [1 .. Upperbound ] of Variant ; ---------------------------------------------------------------------- molly · 2016-06-28 03:40 Code: [Select]Const     Upperbound = 10; Type   AnArray = Array [1 .. Upperbound ] of Variant ; The reason for your snippet not working is that using a typed constant is being treated like it is a (special kind of) variable, e.g. using a typed constant allows you to actually change the value of the constant. The compiler cannot handle a type definition based on a number that can vary during execution of the program. ---------------------------------------------------------------------- Dasun · 2016-06-28 04:10 Doh! Thank you Molly .. and I thought I was being good by specifying the type!! ---------------------------------------------------------------------- molly · 2016-06-28 05:53 @Dasun: In theory, pascal being strong typed and all that, you are correct. However, for pascal it is not common to use/have typed constants (only typed variables). The moment you see/use a typed constant you must think to yourself that something 'fishy' is going on. You probably won't notice the difference between a typed constant and a variable immediately, especially not when defined in a global scope. The moment you define a typed constant inside a function, procedure, method or otherwise you'll find that a typed constant keeps/restores its last value on entry of such routine while a variable would (re)initializes itself. You can try the following program to see for yourself how these things behave differently (showing the 'fishy' part, going on inside procedure LocalModify() ) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program varconsting; const  globalconst : integer = 10; var  globalvar   : integer = 10;  procedure GlobalModify;var i: Integer;begin  WriteLn('globalconst.before = ', globalconst);  WriteLn('globalvar.before   = ', globalvar);   for i := 1 to 10 do   begin    inc(globalconst);    inc(globalvar);  end;  WriteLn('globalconst.after  = ', globalconst);  WriteLn('globalvar.after    = ', globalvar);end;  procedure LocalModify;const  localconst : integer = 10;var  localvar   : integer = 10;var   i: Integer;begin  WriteLn('localconst.before  = ', localconst);  WriteLn('localvar.before    = ', localvar);   for i := 1 to 10 do   begin    inc(localconst);    inc(localvar);  end;   WriteLn('localconst.after   = ', localconst);  WriteLn('localvar.after     = ', localvar);end; begin  GlobalModify;  WriteLn;  GlobalModify;  WriteLn;WriteLn;WriteLn;  LocalModify;  WriteLn;  LocalModify;end. ---------------------------------------------------------------------- howardpc · 2016-06-28 09:16 Object Pascal offers two different sorts of consts: 'proper' consts that are constant (as you might expect), and typed consts that are initialized variables. Naming the latter sort 'const' is a frequent source of confusion for newcomers to Pascal, but it was introduced by Borland, and FPC preserves the same syntax for compatibility with Delphi. The constant sort of const is always declared using Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  identifier = value;without a type. The compiler assigns an appropriate  type automatically, although you can override the compiler's choice of type by using a typecast in the declaration. But most situations do not need that level of control over the type of a const. The initialized variable sort of const, declared using Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  identifier: type = value;and most often termed a typed constant will always declare an initialized variable (in spite of the const declaration name). It is possible to treat this initialized variable always as constant by using the compiler directive Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$J-} or {$WRITEABLECONST OFF} Then if you inadvertently assign a new value to your const, the compiler will stop with an error pointing out that you wanted that value to be constant. It is internally still an initialized variable. For Delphi compatibility the FPC default setting is {$J+}, which means that unless you change the default all typed constants you declare are writeable. Because typed constants are not true constants the compiler disallows their use where true constants are required (such as in setting the bounds of a static array).