Complex If-Then-ElseIf example
This example primarily focuses on describing in greater detail the use of the If-Then-Else Heater and to demonstrate a much more elegant possibility of how such relatively complex structures (9 heaters in this case) can be replaced using a single .Net Script Heater.
Each example is supplied as a completed Definition and comprises an element of FlowHeater that can be directly opened in the Designer window.
What we want to achieve
Here we see how a complex If-Then-ElseIf condition can be defined by making use of the If-Then-Else Heater. The example inputs the following small text file on the READ side:
1;DE
2;EN
3;CA
4;something completely wrong
The first field is simply an incremental ID. We are only interested in the second field here. This field is intended to represent country codes. According to its contents, country names are derived by the Definition.
The method using the If-Then-Else Heater
The description follows the structure from left to right. The "country code" field is first plugged into the If-Then-Else Heater and once again into the Clone Heater. The use of the Clone Heater is perforce necessary here, because no single heater or field can be directly connected twice to another heater. The If-Then-Else Heater evaluates the first incoming connection as the condition ("if" see picture on the right ) and the output is determined as either the second incoming connection ("then" when condition is true) or the optional third incoming connection ("else" when condition is false).
Note: The interpretation of the incoming parameter sequence can be interrogated or amended using the context menu (right mouse click on the Heater icon).
If the first condition is met, the static value in the first X-Value Heater is taken, otherwise the original input value is passed on by the Clone Heater to the next If-Then-Else Heater. Thereafter it proceeds in like wise in the next If-Then-Else Heater. The output of the first condition becomes the input for the second If-Then-Else Heater. In addition, the output is passed on to another Clone Heater, and so on.
The method using the .NET Script Heater
Do not be dismayed by this example. Even if you have no programming skills, it is easy to understand by non-programmers and is simple to modify for your own uses.
Here you can see how a C# Script Code is employed. The code can be displayed by a double click on the .Net Script Heater.
First we validate whether there is any incoming data available, if not we raise an error with the directive throw new ArgumentException and FlowHeater processing ceases.
Next we assign a temporary variable "cmp" from the first input parameter with the statement string cmp = (string)InValues[0].GetString(). Then the switch statement tests the value in the cmp variable, comparing it with the static values of DE, EN and CA. Whichever of the cases applies, determines the "ret" string variable to return. To round off the choices, a further catch-all path (default) is implemented. If none of the static cases defined are met, then the value "not available" is returned. As a further alternative the .NET Script Heater can be supplied with a second input parameter, which would then be passed on in place of the static "not available" in default case of the switch. Try it for yourself. Simply insert into the Definition a further X-Value Heater and connect the output of this with the input of the .Net Script Heater.
public object DoWork()
{
if (InValues.Length == 0)
throw new ArgumentException("min. 1 input parameters expected!");
// the cast is necessary, because the GetString function normally return an Object data type
string cmp = (string)InValues[0].GetString();
string ret = String.Empty;
switch (cmp)
{
case "DE":
ret = "Germany";
break;
case "EN":
ret = "England";
break;
case "CA":
ret = "Canada";
break;
default:
// If none of the above cases apply and a second input parameter is supplied to the .Net Script Heater
// then the value of the second parameter will be returned instead of the static text "not available".
if (InValues.Length > 1)
ret = (string)InValues[1].GetString();
else
ret = "nicht vorhanden";
break;
}
return ret;
}