Is there any way to set an optional parameter to a default value in Silk Test 4Test?
The usage pattern for SetDefault is illustrated in the following example, which was extracted from a set of utilities for accessing Excel spreadsheets from SilkTest:
[-] string Select (list of string lsColumn, string sSheet optional) [ ] SetDefault (sSheet, "Sheet1") [ ] // create part of SQL string for Excel access [ ] // etc [ ] return sSheet //for illustration only
Whenever a function, method or testcase has optional parameters, the declarations should immediately be followed with SetDefault statements corresponding to each optional parameter. This ensures that each optional parameter is given a default, and is also clearly readable as setting that default. This is easier to manage and maintain than if-statements.
The SetDefault function itself is fairly simple, although some care is needed to get the right combination of parameter attributes.
Here is the code:
[+] SetDefault (inout anytype aValue null, anytype aDefaultValue) [-] if isNull (aValue) [ ] aValue = aDefaultValue