Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-12

Boolean case statement

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Terry Kemp

Hi All

how do you do a case statement on a record of booleans e.g...

Type
 EngModeRec = Record
  ManualOp: Boolean;
  LeakTest: Boolean;
  DrainFlush: Boolean;
 end;
var
 EngMode: EngModeRec;

I want to have...

 Case True of
  EngMode.ManualOp: ManualOpRun;
  EngMode.LeakTest: LeakTestRun;
  EngMode.DrainFlush: DrainFlushRun;
 end;

but of course this wont work :(

Thanks Terry

errors...
Running tool: Build Arm

Target OS: Linux for ARM
Compiling mflow.pp
2 68/832 Kb Used
Compiling mrun.pp
Compiling mgldec.pp
100 4.039/4.736 Kb Used
Assembling mgldec
Compiling mstages.pp
100 4.158/4.864 Kb Used
Compiling mglproc.pp
100 5.166/5.920 Kb Used
200 5.417/6.336 Kb Used
300 5.712/6.560 Kb Used
Assembling mglproc
200 5.418/6.720 Kb Used
300 5.598/6.848 Kb Used
400 5.783/6.912 Kb Used
500 5.954/7.008 Kb Used
600 6.163/7.168 Kb Used
Assembling mstages
100 7.653/9.120 Kb Used
200 8.105/9.472 Kb Used
mrun.pp(289,19) Error: Constant Expression expected mrun.pp(290,19) Error: Constant Expression expected mrun.pp(290,19) Error: duplicate case label
mrun.pp(291,21) Error: Constant Expression expected mrun.pp(291,21) Error: duplicate case label
300 8.383/9.472 Kb Used
mrun.pp(384) Fatal: There were 5 errors compiling module, stopping mrun.pp(6,6) Fatal: Compilation aborted

Exited: 256

Vincent Snijders
to Terry Kemp
Terry Kemp wrote 25 lines
Hi All how do you do a case statement on a record of booleans e.g... Type EngModeRec = Record ManualOp: Boolean; LeakTest: Boolean; DrainFlush: Boolean; end; var EngMode: EngModeRec; I want to have... Case True of EngMode.ManualOp: ManualOpRun; EngMode.LeakTest: LeakTestRun; EngMode.DrainFlush: DrainFlushRun; end; but of course this wont work :(
You cannot use it like that. You would have to use an nested if then
statement like:

if EngMode.ManualOp then ManualRun
else if EngMode.ManualOp then LeakTest
else etc.

I would use an enum:

type
   TEngMode = (emManualOp, emLeakTest, emDrainFlush);
var
   EngMode : TEngMode;
case EngMode of
   emManualOp: ManualRun;
   emLeakTest: LeakTestRun;
   emDrainFlush: DrainFlushTest;
end;

if several EngMode can occur at the same time (like can happen with EngModeRec) you can use

type
   TEngModeSet = set of TEngMode;

But then you cannot use case anymore to do the tests.

HTH,
Vincent

Daniël Mantione
to Terry Kemp: Hi All how do you do a case statement on a record of booleans e.g... Type EngModeRec =…

Op Wed, 6 Dec 2006, schreef Terry Kemp:

Show quoted text 23 lines
Hi All how do you do a case statement on a record of booleans e.g... Type EngModeRec = Record ManualOp: Boolean; LeakTest: Boolean; DrainFlush: Boolean; end; var EngMode: EngModeRec; I want to have... Case True of EngMode.ManualOp: ManualOpRun; EngMode.LeakTest: LeakTestRun; EngMode.DrainFlush: DrainFlushRun; end; but of course this wont work :(
with engmode do
  begin
    if manualop then
      manualoprun;
    if leaktest then
      leaktestrun;
    if drainflushop then
      drainflushrun;
  end;

Daniël Mantione

Steve Williams
to Terry Kemp: Hi All how do you do a case statement on a record of booleans e.g... Type EngModeRec =…
Terry Kemp wrote 24 lines
Hi All how do you do a case statement on a record of booleans e.g... Type EngModeRec = Record ManualOp: Boolean; LeakTest: Boolean; DrainFlush: Boolean; end; var EngMode: EngModeRec; I want to have... Case True of EngMode.ManualOp: ManualOpRun; EngMode.LeakTest: LeakTestRun; EngMode.DrainFlush: DrainFlushRun; end; but of course this wont work :(

Of course it won't work. It is invalid syntax. You need to test each member using an if statement.

if EngMode.ManualOp then ManualOpRun;
if EngMode.LeakTest then LeakTestRun;
if EngMode.DrainFlush then DrainFlushRun;
Sly This message and its attachments may contain legally privileged or confidential information. This message is intended for the use of the individual or entity to which it is addressed. If you are not the addressee indicated in this message, or the employee or agent responsible for delivering the message to the intended recipient, you may not copy or deliver this message or its attachments to anyone. Rather, you should permanently delete this message and its attachments and kindly notify the sender by reply e-mail. Any content of this message and its attachments, which does not relate to the official business of the sending company must be taken not to have been sent or endorsed by the sending company or any of its related entities. No warranty is made that the e-mail or attachment(s) are free from computer virus or other defect.
Terry Kemp
to Vincent Snijders: You cannot use it like that. You would have to use an nested if then statement like: if…
Vincent Snijders wrote 38 lines
Terry Kemp schreef: > Hi All > > how do you do a case statement on a record of booleans e.g... > You cannot use it like that. You would have to use an nested if then statement like: if EngMode.ManualOp then ManualRun else if EngMode.ManualOp then LeakTest else etc. I would use an enum: type TEngMode = (emManualOp, emLeakTest, emDrainFlush); var EngMode : TEngMode; case EngMode of emManualOp: ManualRun; emLeakTest: LeakTestRun; emDrainFlush: DrainFlushTest; end; if several EngMode can occur at the same time (like can happen with EngModeRec) you can use type TEngModeSet = set of TEngMode; But then you cannot use case anymore to do the tests. HTH, Vincent

Hey! I like this enum method - the 'states' are to be mutually exclusive so this works great.

thanks alot