C# Language Basics

Learn the foundations of the language we use to write expressions for controlling the questionnaire logic.

Available C# Namespaces

Survey Solutions uses C# language for writing the expressions, such as enabling or validation conditions. The C# language organizes classes of objects into namespaces. To use a class from a namespace the C# program must have access to the corresponding namespace. Survey Solutions provides access to the following namespaces:

You can click on the name of the namespace to see which classes, functions, etc. the namespace avails for the user. System.Linq.Queryable is technically a class, documented here.

July 25, 2023

Forbidden type error

Survey Solutions uses C# language for writing the expressions, such as enabling or validation conditions. Yet, where we thought the language features may be misused to create vulnerabilities, we’ve restricted its use by disallowing certain C# data types, such as accessing files or network.

If you are getting a “Forbidden type” error (corresponding error codes are WB0272, WB0273, WB0274, WB0275) the expression you’ve written is probably following the correct syntax, but is not going to compile for the reason of security.

July 20, 2021

Syntax Guide: Using LINQ Expressions for Conditions in Rosters

Rosters are common survey instruments (ie household roster, assets roster, food consumption roster). Using Language Integrated Query (LINQ) and lambda expressions in the C# language, we are able to code enabling and validation conditions for rosters that can both:

  • Horizontally: Refer to the values of other variables for the current occurrence
  • Vertically: Refer to the values of the same variable for other items in the roster

In this article, we will cover the general syntax, common operators, provide a few examples on how to write enabling and validation conditions for rosters. Additionally, this article also provide links to more resources on LINQ expressions.
 

November 21, 2016

Syntax Guide: System Generated Variables

These are variables that are created by Designer to allow the user to refer to certain variables or lines in a roster in enabling conditions or validation conditions. More detail is provided below about useful system variables: self, @rowcode, @optioncode, and @current.
 

self

For validation conditions, a very useful system generate variable is self. This variable denotes the value of the question being validated. Using self instead of the question’s variable name in a question’s validation conditions is advised because you will not have to change the variable name in the validation condition if the variable name for the question is changed.

August 16, 2016

Syntax Guide: Operators

Designer has built-in operators that can be used in enabling conditions or validations. This article explains the following operators that you can use in your instrument:

 

Arithmetic Operators

 

The following table shows the arithmetic operators supported by Survey Solutions. Arithmetic expressions are evaluated from left to right. For the example, assume A = 20 and B = 10

 

OperatorDescriptionExample
+Addition: returns the sumA + B = 35
-Subtraction: returns the differenceA - B = 10
*Multiplication: returns the productA * B = 200
/Division: returns the quotientB / A = 2
%Modulus: returns the remainder after an integer divisionB % A = 0
(The remainder of 20 divided by 10 is 0)

 

August 15, 2016

Dealing with exceptions

An exception is a situation where the computer can’t proceed with a normal flow of commands.
A common example is a division by zero. The result of this operation is not defined. In statistics a concept of a missing value is introduced, and we normally don’t bother about the process. We know the result of X divided by zero ;will be a missing.

In C# division by zero will cause the program to abort with an exception. The programmer can envelop the risky code into a wrapper, similar to how you capture { } a certain code in Stata. In Survey Solutions we always do this behind the scenes for the users, so when an exception occurs:

August 10, 2016

Data types

Survey Solutions uses C# language for enabling and validation conditions. C# is a contemporary general purpose object-oriented programming language, widely documented. When writing expressions in C# language, and especially when using standard C# and custom Survey Solutions functions it is important to keep in mind the definitions of the various Survey Solutions objects.

Below the types of Survey Solutions objects are explained in terms of standard C# classes and types. Links are provided to the corresponding definitions on Microsoft’s site.

August 9, 2016