else part of if-statement must cover the case when the variable is empty without explicitly defining it - Mendix Forum

else part of if-statement must cover the case when the variable is empty without explicitly defining it

1

Hello Dear Mendix Community,

 

In order to make dynamic class of a dataGrid2 function one has to specify the variable to not be empty in an if-condition (Mendix Community - Question Details).

My idea is to make it being automatically covered within an 'else' case.

 

Explanatory example:

IS-State:

if value=100 then 'class1'

else 'class2'

(value empty -> neither class is being selected)

 

MUST-State:

if value=100 then 'class1'

else 'class2'

(value empty -> 'class2' is being selected)

 

It's being done like so in many popular programming languages like for example in python and javaScript.

 

It would be of course also very helpful to be able to debug expressions (to put breakpoint inside of expressions to be able to evaluate those expressions).

 

Please consider implementing such behaviour into mendix-expressions.

asked
3 answers

Hi Leonid,

 

Ah I just followed the link to your question and that's different to what you've posted to here.

 

The error you got is an evaluation error, not a logic error.

I'll use numbers but it is the same as dates. When using = or != it's different to how < > <= >= is evaluated.

image.png

 

Based on what you stated in the question, you can see it does correctly evaluate to the the 'y' based on your IS-STATE definition.

 

The issue you have is because you're trying to perform a < on a int / decimal / date time that is set to empty.

Mendix will throw an error for you in the IDE if it detects this pattern, however because you are comparing $Object/attribute it can not check to see if its empty so results in the runtime error instead of this one.

image.png

 

It's the same way you can't perform a string attribute < 100 because that evaluation isn't possible (ie empty <= 100 is not logical)

 

Someone already gave the answer in the comments but the correct statement you should use should be:

if $Object/Attribute != empty and $Object/Attribute <= 100

then 'x'

else 'y'

 

This will resolve your red runtime error and correctly map to y. The general rule is, if you're working with < > operations you just need to think about what the empty case is as well :) you can see why that is useful with

 

if value = empty

then 'gray-box'

else if value >= 80

then 'green-box'

else 'red-box'

Created

Hello Adam,

 

no it will not - I have pinned a question out of mendix community here where it was the case and I also had a ticket because of that (No. 256682).

Created

Hi Leonid,

 

In the first example you gave, if value is empty class2 will be applied.

Created