Examples
User Function Examples
This section provides a sample user function for reference.
Example 1: Computing the sum of a vector
This example will sum the non-null values of a vector and return the results as a scalar. There are a few nuances worth pointing out with this example. First, all values are cast to integer. If you wish to sum a vector of floating point values, this function will create significant precision loss. To accommodate input values of different types, check the type returned by IVector.GetValue before doing your processing. The second item worth noting is error handling. DPL Studio is tolerant of user functions throwing exceptions; however, no data will be returned if an exception is thrown. For this reason, be sure that your user functions are as robust as possible.
C#
using System;
using Data Process Logic.Connectors.ConnectorInterfaces;
public class Sum
{
public int Execute(IVector v)
{
//int rowCount = v.GetRowCount();
ITableIterator iter = v.GetIterator();
int sum = 0;
for (int i = 0; iter.Read(); i++)
{
object currentVal = v.GetValue(iter);
if (System.DBNull.Value == currentVal)
{
continue;
}
sum += Convert.ToInt32(currentVal);
}
return sum;
}
}
Visual Basic .NET
Imports System
Imports Data Process Logic.Connectors.ConnectorInterfaces
Public Class ExampleSum
Public Function Execute(v As IVector) As Int32
Dim iter As ITableIterator = v.GetIterator()
Dim sum As Int32 = 0
While iter.Read()
Dim currentVal As Object = v.GetValue(iter)
If Not currentVal.Equals(System.DBNull.Value) Then
sum += Convert.ToInt32(currentVal)
End If
End While
Return sum
End Function
End Class
JScript
import System;
import Data Process Logic.Connectors.ConnectorInterfaces;
public class ExampleSum
{
public function Execute(v : IVector) : long
{
var rowCount : int = v.GetRowCount();
var sum : long = 0;
for (var i = 0; i < rowCount; i++)
{
var currentVal : Object = v.GetValue(i);
if (System.DBNull.Value == currentVal)
{
continue;
}
sum += Convert.ToInt64(currentVal);
}
return sum;
}
}