case ohne const

Für Fragen zur Programmiersprache auf welcher Lazarus aufbaut
Antworten
Mathias
Beiträge: 7173
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

case ohne const

Beitrag von Mathias »

Folgendes Beispiel verursacht folgenden Fehler:

Code: Alles auswählen

unit1.pas(42,6) Error: Constant Expression expected

Code: Alles auswählen

procedure TForm1.Button1Click(Sender: TObject);
var
  a: byte = 0;
  b: byte = 1;
  c: byte = 2;

  w: byte;
begin
  w := b;
  case w of
    a: begin
      WriteLn(a);
    end;
    b: begin
      WriteLn(b);
    end;
    c: begin
      WriteLn(c);
    end;
  end;
end;
Kann man FPC irgendwie mit einem Compiler-Schalter überreden, das dies geht ?
Ansonsten muss man eine recht unübersichtliches if then Gewurstel machen.
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Benutzeravatar
six1
Beiträge: 840
Registriert: Do 1. Jul 2010, 19:01

Re: case ohne const

Beitrag von six1 »

Code: Alles auswählen

procedure TForm1.Button1Click(Sender: TObject);
var
  a: byte = 0;
  b: byte = 1;
  c: byte = 2;

  w: byte;
begin
  w := b;
  //case w of
  //  a: begin
  //    WriteLn(a);
  //  end;
  //  b: begin
  //    WriteLn(b);
  //  end;
  //  c: begin
  //    WriteLn(c);
  //  end;
  //end;

  if w in [a] then
  begin
      WriteLn(a);
  end else
  if w in [b] then
  begin
      WriteLn(b);
  end else
  if w in [c] then
  begin
      WriteLn(c);
  end else
  begin
    WriteLn('Nothing');
  end;

end;
Gruß, Michael

Benutzeravatar
fliegermichl
Lazarusforum e. V.
Beiträge: 1740
Registriert: Do 9. Jun 2011, 09:42
OS, Lazarus, FPC: Lazarus Fixes FPC Stable
CPU-Target: 32/64Bit
Wohnort: Echzell

Re: case ohne const

Beitrag von fliegermichl »

In der FreePascal Dokumentation steht das u.a. explizit
They have to be constant expressions, i.e. known at compile-time.

Mathias
Beiträge: 7173
Registriert: Do 2. Jan 2014, 17:21
OS, Lazarus, FPC: Linux (die neusten Trunk)
CPU-Target: 64Bit
Wohnort: Schweiz

Re: case ohne const

Beitrag von Mathias »

Danke
Dann muss ich es wohl oder übel mit if then machen.
Mit Lazarus sehe ich grün
Mit Java und C/C++ sehe ich rot

Benutzeravatar
Winni
Beiträge: 1577
Registriert: Mo 2. Mär 2009, 16:45
OS, Lazarus, FPC: Laz2.2.2, fpc 3.2.2
CPU-Target: 64Bit
Wohnort: Fast Dänemark

Re: case ohne const

Beitrag von Winni »

Hi!

Nö, Du kannst a,b und c ja als const deklarieren.
Dann gehts.

Winni

jammernich
Beiträge: 35
Registriert: Di 5. Nov 2024, 22:36
OS, Lazarus, FPC: Win11, Lazarus 4.5, fpc 3.2.3

Re: case ohne const

Beitrag von jammernich »

Code: Alles auswählen

program CaseTest;

{$mode ObjFPC}{$H+}{$J-}

uses
  SysUtils, Classes;

const
  ftAUTODETECT: UInt8 = 0;
  ftAVIF      : UInt8 = 1;
  ftBMP       : UInt8 = 2;
  ftCUR       : UInt8 = 3;
  ftGIF       : UInt8 = 4;
  ftICO       : UInt8 = 5;
  ftJPG       : UInt8 = 6;
  ftJXL       : UInt8 = 7;
  ftLBM       : UInt8 = 8;
  ftPNG       : UInt8 = 9;

var 
  AFileType : UInt8 = 9;
  temp : String;

begin
  //ftAVIF := 7;                         //Error

  Case AFileType Of
    1     : temp := 'YIPPIEH';         //ok
    ftJPG : temp := IntToStr(ftJPG);   // <==== ?
    ftPNG : temp := IntToStr(ftPNG);
    ftJXL : temp := IntToStr(ftJXL);
  end;
  writeln(temp);
  readln;
end.
Hallo. Ich habe auch verschiedene Fehler beim kompilieren.
FPC 3.2.3+ Win11
Was mach ich falsch :?

Code: Alles auswählen

[b]Free Pascal Compiler version 3.2.3-59-g86b5f9db8e [2025/11/29] for x86_64
Copyright (c) 1993-2025 by Florian Klaempfl and others
Target OS: Win64 for x64
Compiling D:\Casetest.pas
Casetest.pas(29,11) Error: Constant Expression expected
Casetest.pas(30,11) Error: Constant Expression expected
Casetest.pas(30,11) Error: duplicate case label
Casetest.pas(31,11) Error: Constant Expression expected
Casetest.pas(31,11) Error: duplicate case label
Casetest.pas(35,4) Fatal: There were 5 errors compiling module, stopping
Fatal: Compilation aborted[/b]

jammernich
Beiträge: 35
Registriert: Di 5. Nov 2024, 22:36
OS, Lazarus, FPC: Win11, Lazarus 4.5, fpc 3.2.3

Re: case ohne const

Beitrag von jammernich »

https://forum.lazarus.freepascal.org/in ... l#msg74742

Oh ein Alter Hut. Typisierte Konstanten sind keine Konstanten ? Wie macht ihr das so? Gibt es ein workaround?

Benutzeravatar
Zvoni
Beiträge: 516
Registriert: Fr 5. Jul 2024, 08:26
OS, Lazarus, FPC: Windoof 10 Pro (Laz/FPC fixes)
CPU-Target: 64Bit
Wohnort: BW

Re: case ohne const

Beitrag von Zvoni »

Code: Alles auswählen

program Project1;
{$mode objfpc}{$H+}
Uses Sysutils, classes;
Type
  {$packEnum 1}
  TMyEnum = (ftAUTODETECT=0,ftAVIF=1,ftBMP=2,ftCUR=3,ftGIF=4,ftICO=5,ftJPG=6,ftJXL=7,ftLBM=8,ftPNG=9);
Var
  SomeFileType:UInt8;
  temp:String;
Function CheckFileType(Const AFileType:UInt8):String;
Begin
  If Ord(TMyEnum(AFileType))=1 Then
    Result:='YIPPIEH'
  Else
    Result:=Ord(TMyEnum(AFileType)).ToString;
End;
begin
  SomeFileType:=1;
  temp:=CheckFileType(SomeFileType);
  Writeln(temp);
  SomeFileType:=9;
  temp:=CheckFileType(SomeFileType);
  Writeln(temp);
  Readln;
end.
Ein System sie alle zu knechten, ein Code sie alle zu finden,
Eine IDE sie ins Dunkel zu treiben, und an das Framework ewig zu binden,
Im Lande Redmond, wo die Windows drohn.

PascalDragon
Beiträge: 1023
Registriert: Mi 3. Jun 2020, 07:18
OS, Lazarus, FPC: L 2.0.8, FPC Trunk, OS Win/Linux
CPU-Target: Aarch64 bis Z80 ;)
Wohnort: München

Re: case ohne const

Beitrag von PascalDragon »

jammernich hat geschrieben: Do 29. Jan 2026, 22:41 Was mach ich falsch :?
Lass die : UInt8 weg, um untypisierte Konstanten zu erzeugen, welche auch in case-Blöcken genutzt werden können.
FPC Compiler Entwickler

Antworten