Conditional variable calculation

Dear Community,

I’m very fresh to Survey Solution designing, though I work with an already established questionnaire that needs some additional variables for manual validation. Therefore, this question might seem very simple to you.

The specific problem I’m having is that I want to calculate the standardized area of a plot in ha, based on different units a farmer can provide.
Thus when ar_1_1 is the raw area and ar_1_1_un the applied unit (suppose m²). Then ar_1_1_ha should be ar_1_1 / 10000. Though the difficulty I’m facing is to get several different calculations based on the differing units.
I tried if - else if - … - else conditions like in C# but they do not seem to work and throw a circular reference error.

Code looks like this:

if (ar_1_1_un == 1)
(ar_1_1_ha = ar_1_1);
else if (ar_1_1_un == 2)
(ar_1_1_ha = ar_1_1/2.47105);
(ar_1_1_ha = ar_1_1*ar_1_1_un_ha);

Help would be greatly appreciated.


Hello @DOTTO , your intuition is correct, but you can’t write a program, the program has been already written for you:

double AREA=______________;

and all you have to do is to fill in the gap. That’s why it is an expression, not a program that you are writing, and you can’t use if…else in this context.

Programmers would want to use a ternary operator in this context. See here:

double AREA = (ar_1_1_un==1)?(ar_1_1):( ....)

Statisticians would probably prefer:

COEF=new[]{989898, 1, 2.47105}[ar_1_1_un.Value]

With 2 or 3 units I’d write an expression with the ternary operator, but if the number of units is getting large, or you have several questions like that in the questionnaire - use lookup tables.

Best, Sergiy

PS: did you get it why you need 989898 in the list of coefficients?

Dear @sergiy,
thank you for the answer that seems very to the point and suited for my case! Thank you for taking the time!
Guessing on your post scriptum: If the multi-select question for the unit was not filled it evaluates to 0 as an index and therefore you’ll see an astronomically high area as a check? Though I’m not aware from were C# starts to count indexes :smiley:


No. If the question is not answered its value is null (not zero). You will get an exception at extracting an array element with such an index. Correspondingly the area is going t to have the null value, meaning ‘not calculated’.

This is closer to the point. C# starts indexing in arrays from zero (0), but your options in ar_1_1_un start from 1, so we need a fake element to align the array with the options in your units question. Any value will do (it will never be applied since 0 is not among the choices, so element with index 0 will never be addressed).

We could have avoided that by subtracting 1:

COEF=new[]{1, 2.47105}[ar_1_1_un.Value - 1]

or a different offset if the options numbering starts with a different value, such as 101 (of course if the options are numbered with 101, I wouldn’t place 100 fake numbers in front of the usable ones).

Best, Sergiy

1 Like