Version

UltraCalcFunction Class

Base class for formula functions.
Syntax
'Declaration
 
Public MustInherit Class UltraCalcFunction 
public abstract class UltraCalcFunction 
Example
Following code shows how to use a custom function in formulas.

Imports Infragistics.Shared
Imports Infragistics.Win
Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win.CalcEngine
Imports Infragistics.Win.UltraWinCalcManager


    ' <summary>
    ' Calcuate the factorial of a number.
    ' </summary>
    Public Class Function_Factorial
        Inherits Infragistics.Win.CalcEngine.UltraCalcFunction

        Public Sub New()
        End Sub

        ' <summary>
        ' This is used in the formula designer and has no relavance to the calculation logic.
        ' </summary>
        Public Overrides ReadOnly Property ArgDescriptors() As String()
            Get
                Return New String() {"Input into factorial, will mulitply each integer from 2 to Me number together."}
            End Get
        End Property

        ' <summary>
        ' This is used in the formula designer and has no relavance to the calculation logic.
        ' </summary>
        Public Overrides ReadOnly Property ArgList() As String()
            Get
                Return New String() {"integer"}
            End Get
        End Property

        ' <summary>
        ' This is used in the formula designer and has no relavance to the calculation logic.
        ' </summary>
        Public Overrides ReadOnly Property Category() As String
            Get
                Return "Mathematical"
            End Get
        End Property

        ' <summary>
        ' This is used in the formula designer and has no relavance to the calculation logic.
        ' </summary>
        Public Overrides ReadOnly Property Description() As String
            Get
                Return "Compute the factorial of the passed in number"
            End Get
        End Property

        ' <summary>
        ' Gets if the function is considered always dirty and therefore need to be called every time an evaluation is done.
        ' </summary>
        ' <remarks>
        ' Most functions return false except for functions like Rand.
        ' </remarks>
        Public Overrides ReadOnly Property IsAlwaysDirty() As Boolean
            Get
                Return False
            End Get
        End Property

        ' <summary>
        ' Specifies the maximum number of arguements Me function takes.
        ' </summary>
        Public Overrides ReadOnly Property MaxArgs() As Integer
            Get
                Return 1
            End Get
        End Property

        ' <summary>
        ' Specifies the minimum number of arguements Me function takes.
        ' </summary>
        Public Overrides ReadOnly Property MinArgs() As Integer
            Get
                Return 1
            End Get
        End Property

        ' <summary>
        ' Specifies the function name. This is the name you will use in formulas. For example "median(5, 3, 1)".
        ' </summary>
        Public Overrides ReadOnly Property Name() As String
            Get
                Return "factorial"
            End Get
        End Property

        Protected Overrides Function Evaluate(ByVal numberStack As Infragistics.Win.CalcEngine.UltraCalcNumberStack, ByVal argumentCount As Integer) As UltraCalcValue

            Dim param As UltraCalcValue = numberStack.Pop()

            If param.IsError = True Then
                Return New UltraCalcValue(param.ToErrorValue())
            End If

            Dim val As Int64

            val = param.ToInt64()

            If (val < 0) Then Return New UltraCalcValue(New UltraCalcErrorValue(UltraCalcErrorCode.Num, "Can't calculate factorial of a negative number."))

            Return New UltraCalcValue(Me.CalculateFactorial(val))

        End Function

        Private Function CalculateFactorial(ByVal value As Int64) As Int64

            If (value = 0) Then Return 1

            If (value < 3) Then Return value

            Dim factorial As Int64 = 2

            Dim i As Int64

            For i = 3 To value
                factorial *= i
            Next

            Return factorial
        End Function

    End Class

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
        ' Register the factorial function we defined above.
        Me.UltraCalcManager1.RegisterUserDefinedFunction(New Function_Factorial())

        ' Use it into a formula. You refer to it using the string returned by the Name 
        ' property of the user defined function.
        Dim f10 As Double = Me.UltraCalcManager1.Calculate("factorial(10)").ToDouble()
        MessageBox.Show(Me, "factorial(10) = " & f10)
    End Sub
using Infragistics.Shared;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using System.Diagnostics;
using Infragistics.Win.CalcEngine;
using Infragistics.Win.UltraWinCalcManager;


		/// <summary>
		/// Calcuate the factorial of a number.
		/// </summary>
		public class Function_Factorial : Infragistics.Win.CalcEngine.UltraCalcFunction
		{
			public Function_Factorial( )
			{
			}
			/// <summary>
			/// This is used in the formula designer and has no relavance to the calculation logic.
			/// </summary>
			public override string[] ArgDescriptors
			{
				get
				{
					return new string[] { "Input into factorial, will mulitply each integer from 2 to this number together." };
				}
			}

			/// <summary>
			/// This is used in the formula designer and has no relavance to the calculation logic.
			/// </summary>
			public override string[] ArgList
			{
				get
				{
					return new string[] { "integer" };
				}
			}

			/// <summary>
			/// This is used in the formula designer and has no relavance to the calculation logic.
			/// </summary>
			public override string Category
			{
				get
				{
					return "Mathematical";
				}
			}

			/// <summary>
			/// This is used in the formula designer and has no relavance to the calculation logic.
			/// </summary>
			public override string Description
			{
				get
				{
					return "Compute the factorial of the passed in number";
				}
			}

			/// <summary>
			/// Gets if the function is considered always dirty and therefore need to be called every time an evaluation is done.
			/// </summary>
			/// <remarks>
			/// Most functions return false except for functions like Rand.
			/// </remarks>
			public override bool IsAlwaysDirty
			{
				get
				{
					return false;
				}
			}

			/// <summary>
			/// Specifies the maximum number of arguments this function takes.
			/// </summary>
			public override int MaxArgs
			{
				get
				{
					return 1;
				}
			}

			/// <summary>
			/// Specifies the minimum number of arguments this function takes.
			/// </summary>
			public override int MinArgs
			{
				get
				{
					return 1;
				}
			}

			/// <summary>
			/// Specifies the function name. This is the name you will use in formulas. For example "median(5, 3, 1)".
			/// </summary>
			public override string Name
			{
				get
				{
					return "factorial";
				}
			}

			/// <summary>
			/// Evaluate method is called to perform the actual calculations.
			/// </summary>
			/// <param name="numberStack">Formula number stack containing function arguments</param>
			/// <param name="argumentCount">Denotes the number of function arguments pushed onto the number stack.</param>
			/// <returns>The result as an UltraCalcValue.</returns>
			protected override UltraCalcValue Evaluate(Infragistics.Win.CalcEngine.UltraCalcNumberStack numberStack, int argumentCount)
			{
				UltraCalcValue param = numberStack.Pop();

				if ( param.IsError )
					return new UltraCalcValue( param.ToErrorValue() );
 
				Int64 val = param.ToInt64();

				if ( val < 0 )
					return new UltraCalcValue( new UltraCalcErrorValue( UltraCalcErrorCode.Num, "Can't calculate factorial of a negative number."));

				return new UltraCalcValue(this.CalculateFactorial(val));
			}

			private Int64 CalculateFactorial( Int64 value )
			{
				if ( value == 0 )
					return 1;

				if ( value < 3 )
					return value;

				Int64 factorial = 2;

				checked		
				{
					for ( Int64 i = 3; i <= value; i++ )
					{
						factorial *= i;
					}
				}

				return factorial;
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			// Register the factorial function we defined above.
			this.ultraCalcManager1.RegisterUserDefinedFunction( new Function_Factorial( ) );

			// Use it into a formula. You refer to it using the string returned by the Name 
			// property of the user defined function.
			double f10 = this.ultraCalcManager1.Calculate( "factorial(10)" ).ToDouble( );
			MessageBox.Show( this, "factorial(10) = " + f10 );
		}
Requirements

Target Platforms: Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also