Recent

Author Topic: Array index with const  (Read 307 times)

Jake012345

  • Sr. Member
  • ****
  • Posts: 262
  • 'The knowledge is the key.'
    • FPCProjectShare
Array index with const
« on: October 28, 2020, 09:24:47 pm »
Hello!

I wanted to declare an array with constant index, but It doesn't work.

Code: Pascal  [Select][+][-]
  1. unit uMain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons,
  9.   ExtCtrls, Menus, strutils;
  10.  
  11. const
  12.   MaxQuestionNumber: integer = 2000;
  13.  
  14. type
  15.   TScore = Record
  16.     Good:integer;
  17.     Bad:integer;
  18.     Time:integer;
  19.   end;
  20.  
  21. type
  22.  
  23.   { TMainForm }
  24.  
  25.   TMainForm = class(TForm)
  26.     btnStart: TButton;
  27.     btnCheck: TButton;
  28.     editAnswer: TEdit;
  29.     lblOpenedFileName: TLabel;
  30.     lblQuest: TLabel;
  31.     MainMenu: TMainMenu;
  32.     MIOpen: TMenuItem;
  33.     MIFile: TMenuItem;
  34.     MINew: TMenuItem;
  35.     Timer: TTimer;
  36.     procedure btnCheckClick(Sender: TObject);
  37.     procedure btnStartClick(Sender: TObject);
  38.     procedure editAnswerEnter(Sender: TObject);
  39.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  40.     procedure MINewClick(Sender: TObject);
  41.     procedure MIOpenClick(Sender: TObject);
  42.   private
  43.  
  44.   public
  45.     SourceFile: string;
  46.     Data: Text;
  47.     StepCounter:integer;
  48.     q, a: array[1..MaxQuestionNumber] of string;
  49.  
  50.     procedure Step;
  51.     procedure LoadInFile(filename: string = '');
  52.   end;
  53.  
  54. var
  55.   MainForm: TMainForm;
  56.  
  57. implementation    
  58.  

I got an error with 'Can't evaluate constant expresion' for 'q' and 'a' arrays.

I knew it's working. What did I wrong?
Check my ProjectShare site and share your content: FPCShare

josh

  • Hero Member
  • *****
  • Posts: 792
Re: Array index with const
« Reply #1 on: October 28, 2020, 09:45:03 pm »
hi

const should be declared as

Code: Pascal  [Select][+][-]
  1. const MaxQuestionNumber = 2000;

It looks like your using the syntax for defining a var a default value ie
Code: Pascal  [Select][+][-]
  1. var  MaxQuestionNumber: integer = 2000;
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

jamie

  • Hero Member
  • *****
  • Posts: 3797
Re: Array index with const
« Reply #2 on: October 28, 2020, 09:45:31 pm »
const
  MaxQuestionNumber:  = 2000;

 you need compiler intrinsic constants since this is done at compile time..

 if you want this at runtime then you need to use a dynamic array which will resolve the size at runtime using
any form of constant that is integer for example

The only true wisdom is knowing you know nothing

Sieben

  • Full Member
  • ***
  • Posts: 144
Re: Array index with const
« Reply #3 on: October 28, 2020, 11:15:39 pm »
Or once more in just other words: you cannot use a typed constant here since it might be subject to change during runtime. With

Code: Pascal  [Select][+][-]
  1. const
  2.   MaxQuestionNumber = 2000;

it should compile ok.
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32

Jake012345

  • Sr. Member
  • ****
  • Posts: 262
  • 'The knowledge is the key.'
    • FPCProjectShare
Re: Array index with const
« Reply #4 on: October 30, 2020, 01:57:10 pm »
:D

Thanks for the help!
Check my ProjectShare site and share your content: FPCShare

 

TinyPortal © 2005-2018