boolean multi filter

I have to do some code in the following conditions in the on aftergetrecord trigger of a report VAR: cond1 type boolean cond2 type boolean cond3 type boolean The user is free to select the conditions in the request form of a report. My code looks like this: //BEGIN block1 IF (COND1 = TRUE) AND (COND2 = FALSE) AND (COND3 = FALSE) THEN BEGIN IF some code THEN BEGIN some other code END; END; //END block1 // BEGIN block2 IF (COND1 = TRUE) AND (COND2 = TRUE) AND (COND3 = FALSE) THEN BEGIN IF some code THEN BEGIN some other code END; END; //END block2 This kind of (bad) code works only if I write only one block and not with both of them. So I think my code behave a logical bug. (I have never been logical :)) Thanks

John, could you be a bit more specific about your problem? The code looks ok, so what do you mean with “works only if I write only one block”?

Block1 and block2 are now independent. Consider a scenario were condition 1 and 2 are true. The way the code is written it will execute first block1 (because condition 1 is true) and then block2 (because condition 1 and 2 are true). If block1 changes a boolean to false and block2 changes the same boolean to true it will apear as if block1 has never been executed because block2 overwrites the changes from block1. If this is the case you should make block2 dependent on the execution of block1 with an else statement.

quote:


Originally posted by Marno
Consider a scenario were condition 1 and 2 are true. The way the code is written it will execute first block1 (because condition 1 is true) and then block2 (because condition 1 and 2 are true).


Not quite. If cond 1 and 2 are both true, block 1 will not be executed, because its condition requires cond 2 to be false. Of course, if block 1 changes one of the conds somewhere, this will lead to unexpected results. That’s why I asked for more details…

Heinz, I only means by “block1, block2” that if I delete one of the blocks the other work. The cond1,cond2,cond3 variables are corresponding respectively to entry type Purchase,sale,Positive adjustement I only want to insert the item number in the recstat table if the the entry type is one of the choosed types by cond1, cond2, cond3. IF (sale = TRUE) AND (purchase = FALSE) AND (positiveAdjustement = FALSE) THEN BEGIN IF “Entry Type” IN [“Entry Type”::Purchase,“Entry Type”::Sale] THEN BEGIN recstat.“Item no.” := “Item No.”; recstat.“entry type” := FORMAT (“Entry Type”); recstat.“entry no.” := “Entry No.”; IF recstat.INSERT THEN; recstat.MODIFY; END; END;

And what happens if block 2 is also present? Is block 2 the same code as block 1 (except for the different condition, of course)? By the way: Is recstat.“entry type” really of type Text?

Well I think CASE Structure will work perfactly in this scenario. Example CASE TRUE OF (COND1 = TRUE) AND (COND2 = FALSE) AND (COND3 = FALSE): BEGIN END; (COND1 = TRUE) AND (COND2 = TRUE) AND (COND3 = FALSE): BEGIN END; (COND1 = TRUE) AND (COND2 = TRUE) AND (COND3 = TRUE): BEGIN END; (COND1 = FALSE) AND (COND2 = FALSE) AND (COND3 = FALSE): BEGIN END; END; Hope this will help, Naveen Jain

Yes, I think so, too, Naveen. Note that CASE TRUE OF will only execute the first condition statement evaluating to TRUE and then exit the CASE structure. So the order of the condition statements (not the conditions within one statement) can be important in some cases. You can also write CASE TRUE OF Purchase AND NOT Sales AND NOT PosAdjmt: BEGIN END; Purchase AND Sales AND NOT PosAdjmt: BEGIN END; ELSE //All other combinations: BEGIN END; END; This style is used in Navision standard. I find it more readable, especially if the booleans are nicely named.

Thanks for your answers

Why do you use 3 booleans ? It looks to me that they cannot be all true at the same time ? Then you could use option instead of booleans and make the code simpler…

I still fail to see the problem with the original version, and the difference between the original version and the alternative solutions… the conditions of block1 and block2 are clearly mutually exclusive. But then, I’m probably a bit dull this week [xx(]

As I wrote, Case will work in your case, Only in one situation this will be true. Since you are using AND not OR . IF you use OR, then it can be true in multiple seniario and then probably order will be important.