Assigning a Value to a Variable

This topic specifically concerns Variables, and not other types of model identifier such as Constants.

The easiest way to think of a variable is as a virtual cup or other type of object capable of holding something inside. The variable's Name is a unique term that helps distinguish it from similar containers, while its Value represents its contents.

When a variable is Initialised, it receives a value. This value can be changed during the run-time of your application by Assigning the variable a new value. By doing this, you replace the virtual container's existing contents with something else.

Simplified illustration of variable value assignment, showing the values of three variables after initial and replacement assignment.

This illustration depicts three variables: Var1, Var2 and Var3. Each of these variables was initialised with a value of zero:

  • Firstly, a value of 2 is assigned to Var2. Its value changes to 2, while the values of Var1 and Var3 stay at 0.
  • Next, a value of 4 is assigned to Var2. Even though a value was assigned to Var2 in the previous step, a variable can only hold one value at a time, so its existing value is replaced with 4.
  • At the same time, a value of 1 is assigned to Var3, whose default value of 0 is replaced with this new value.
  • After all of the assignments are complete, Var2 has had two values assigned to it, but only retains the most recent assignment of 4. Var3's value was changed once by the assignment of the number 1. Var1 did not have any values assigned to it, and therefore retains its initial value of 0.

An Assignment Statement is a short KnowledgeKube expression, this type of statement is written using the following syntax:

VariableName:= "This is a string";

Replace VariableName in the example above with the name of the intended variable, and replace the generic string with the value you want to assign.

Assignment statements can be added to any expression. As with all statements, remember to use a semicolon where necessary to ensure the expression works as expected.

There are two methods of changing the value of a KnowledgeKube variable. Refer to the following topics for more information:

Description Further Reading
Associate a design-time variable with a question, causing the question's response to be assigned to the variable on postback. Assignment Using Target Model Variables
Write an expression that assigns a value to a variable. This method is also used to create run-time variables. Assignment Using an Expression