Spread for ASP.NET 13 Product Documentation
FarPoint.Web.Spread Assembly / FarPoint.Web.Spread.Model Namespace / DefaultSheetDataModel Class / AddCustomFunction Method
FunctionInfo object that contains the user-defined function to add
Example


In This Topic
    AddCustomFunction Method (DefaultSheetDataModel)
    In This Topic
    Adds a new user-defined custom function to the model for use in formulas.
    Syntax
    'Declaration
     
    Public Sub AddCustomFunction( _
       ByVal functionInfo As FunctionInfo _
    ) 
    'Usage
     
    Dim instance As DefaultSheetDataModel
    Dim functionInfo As FunctionInfo
     
    instance.AddCustomFunction(functionInfo)
    public void AddCustomFunction( 
       FunctionInfo functionInfo
    )

    Parameters

    functionInfo
    FunctionInfo object that contains the user-defined function to add
    Exceptions
    ExceptionDescription
    No function specified; function is a null reference (Nothing in Visual Basic)
    Remarks

    A user-defined function (FunctionInfo object) must be marked with the Serializable attribute or an exception occurs if the data model is saved to view state, a file, or otherwise serialized.

    For more information on using custom functions in formulas, refer to the Formula Reference.

    Example
    This example adds a custom function to the data model.
    <Serializable()>
        public class CubeFunctionInfo : FarPoint.CalcEngine.FunctionInfo 
        { 
            public override string Name { get { return "CUBE"; } } 
            public override int MinArgs { get { return 1; } } 
            public override int MaxArgs { get { return 1; } } 
            public override object Evaluate (object[] args) 
            { 
                double num = FarPoint.CalcEngine.CalcConvert.ToDouble(args[0]); 
                return num * num * num; 
            } 
        } 
        
    private void PageLoad(object sender, System.EventArgs e)
    {
        FarPoint.Web.Spread.Model.DefaultSheetDataModel dataModel = new FarPoint.Web.Spread.Model.DefaultSheetDataModel(5, 5); 
        dataModel.AddCustomFunction(new CubeFunctionInfo()); 
        dataModel.SetFormula(1, 1, "CUBE(4)"); 
        FpSpread1.ActiveSheetView.DataModel = dataModel;    
    }
    
    <Serializable()> 
    Public Class CubeFunctionInfo
    Inherits FarPoint.CalcEngine.FunctionInfo
    
    Public Overrides ReadOnly Property Name() As String
    Get
    Return "CUBE"
    End Get
    End Property
    
    Public Overrides ReadOnly Property MinArgs() As Integer
    Get
    Return 1
    End Get
    End Property
    
    Public Overrides ReadOnly Property MaxArgs() As Integer
    Get
    Return 1
    End Get
    End Property
    
    Public Overrides Function Evaluate(ByVal args() As Object) As Object
    Dim num As Double = FarPoint.CalcEngine.CalcConvert.ToDouble(args(0))
    Return num * num * num
    End Function
    
    End Class
    
    Private Sub PageLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim dataModel As New FarPoint.Web.Spread.Model.DefaultSheetDataModel(5, 5)
    dataModel.AddCustomFunction(New CubeFunctionInfo)
    dataModel.SetFormula(1, 1, "CUBE(4)")
    FpSpread1.ActiveSheetView.DataModel = dataModel
    End Sub
    See Also