Use custom code from the template

Example: String replacement function

In the following example, you create a function to replace the word "Bike" with "Bicycle".

  1. In the Report Properties, select the Code page.

  2. Enter a valid Visual Basic function. For example:

    Public Function ChangeWord(ByVal s As String) As String

    Dim strBuilder As New System.Text.StringBuilder(s)

    If s.Contains("Bike") Then

    strBuilder.Replace("Bike", "Bicycle")

    Return strBuilder.ToString()

    Else : Return s

    End If

    End Function

  3. Add a text field to your template, and edit the value. Code defined in the Report Properties will be listed in the Expression Editor under Custom Code. Use the following expression:

    =Code. ChangeWord("I like Bike")

     

 

In the report preview, the text will be shown as "I like Bicycle".

Example: Store a string value in a variable

Calculation variables can store only numeric values. The following example shows how to store a string value in a variable.

  1. In the Report Properties, select the Code page.

  2. Define the variable, and create functions to set and to get the value.

    Public Function ChangeWord(ByVal s As String) As String

    Dim MyText As String = ""

     

    Function SetMyText(text as String)

    MyText = text

    Return MyText

    End Function

     

    Function GetMyText() As String

    Return MyText

    End Function

  3. To set the variable, use the following expression in a field or column instead of the original value.

    E.g. use =code.SetMyText(Signal_ID) instead of =Signal_ID

  4. To use the variable later in the template, use the following expression:

    =code.GetMyText()

×