Boolean case statement

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Boolean case statement

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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Boolean case statement

Vincent Snijders
Terry Kemp schreef:

> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Boolean case statement

Bugzilla from daniel.mantione@freepascal.org
In reply to this post by Terry Kemp


Op Wed, 6 Dec 2006, schreef 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 :(
with engmode do
  begin
    if manualop then
      manualoprun;
    if leaktest then
      leaktestrun;
    if drainflushop then
      drainflushrun;
  end;

Daniƫl Mantione
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Boolean case statement

Steve Williams-3
In reply to this post by Terry Kemp
Terry Kemp wrote:

> 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.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Boolean case statement

Terry Kemp
In reply to this post by Vincent Snijders
On Tue, 2006-12-05 at 22:44 +0100, Vincent Snijders wrote:
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal