Adding a user function

Maybe you have already tried to run one of the tasks. In this case you will have noticed that it didn't work. The error message that is generated says that the function GetCheckSum is not known.

This is not a Tricentis TDM Studio error because this function is not part of the standard Tricentis TDM Studio installation. Checksums can be calculated in many different ways and it would not make sense to provide a separate function for every eventuality. However, Tricentis TDM Studio offers you the possibility to integrate and use your own user-defined functions.

One way to create an new function is to use the context menu of the Functions node in Project Explorer. The New Function command creates a framework for your function and adds this under Functions to the project tree.

The new file can be re-named immediately, in the same way as new templates can be:

Changing the name of a template in the project tree

The file extension .cs is added automatically. The actual name of a function file is irrelevant with regard to calling the function. However, you should select the name in such a way that it indicates the type of function contained in the file. For this reason, we shall call the file Cryptography.cs in this example.

After you add the function, an editor window opens which contains the framework for the function. The function that is needed for this example must be called GetCheckSum and it must receive a string and return a string. The complete code for the function looks as follows:

using System;

namespace ToscaTDM.UserFunctions

{

public partial class Functions

{

public string GetCheckSum( string number )

{

int num = 0;

for( int i = 0; i < number.Length; ++i )

{

if( (i + 1) % 2 == 0 )

num += int.Parse(number.Substring(i, 1)) * 3;

else

num += int.Parse(number.Substring(i, 1));

}

num = (10 - (num % 10)) % 10;

return num.ToString();

}

}

}

As a rule, user functions should be defined as public so that they can be re-used. In addition, it will also be shown in the IntelliSense of the template editor.

As mentioned previously, the C# code will not be explained here. What is more important is how this function can be used in Tricentis TDM Studio. If you now execute a task, you will see a message that the user-defined functions have been modified. If you answer Yes to the question whether the functions should be translated, then the newly created function is integrated into Tricentis TDM Studio and can be used straight away in your project.