# Creating a Custom Function

Learn how you can create custom functions using Spread for ASP.NET.  A custom function takes priority over the built-in function.

## Content

If you have functions that you use on a regular basis that are not in the built-in functions or if you wish to combine some of the built-in functions into a single function, you can do so by defining your own custom functions. They can be called as you would call any of the built-in functions.
A custom function can have the same name as a built-in function. The custom function takes priority over the built-in function. Custom functions are dynamically linked at evaluation time. Thus, the application can redefine an existing custom function.
For more information about formulas, refer to the [Formula Reference](https://developer.mescius.com/spreadnet/docs/latest/online-formula/overview.html).

## Using Code

1. Define the custom function(s).
2. Register the function(s) in the sheet using the [AddCustomFunction](/spreadnet/api/latest/online-asp/FarPoint.Web.Spread/FarPoint.Web.Spread.Model.DefaultSheetDataModel.AddCustomFunction.html) method.
3. Use the custom function(s).

## Example: Creating, Registering, and Using Three Custom Functions

The first step is to create the custom functions. In this example, we create three functions: a cube mathematical function, an XOR logical function, and a null string function. The following code implements the custom functions.
The CUBE custom function raises a number to the third power. That is, CUBE(x) is equivalent to POWER(x,3).

```csharp
public class CubeFunctionInfo : 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 = CalcConvert.ToDouble(args[0]);
return num * num * num;
}
}
```

The XOR custom function performs an exclusive OR operation on two boolean values. This is similar to the "^"operator in C or the XOR operator in VB.

```csharp
public class XorFunctionInfo : FunctionInfo
{
public override string Name { get { return "XOR"; } }
public override int MinArgs { get { return 2; } }
public override int MaxArgs { get { return 2; } }
public override object Evaluate (object[] args)
{ 
bool arg0 = CalcConvert.ToBool(args[0]);
bool arg1 = CalcConvert.ToBool(args[1]);
return (arg0 || arg1) && (arg0 != arg1);
}
}
```

The NULL function returns the constant value null [i.e. similar to how FALSE() function returns the constant value false].

```csharp
public class NullFunctionInfo : FunctionInfo
{
public override string Name { get { return "NULL"; } }
public override int MinArgs { get { return 0; } }
public override int MaxArgs { get { return 0; } }
public override object Evaluate (object[] args)
{
return null;
}
}
```

The second step is to register the custom functions as this code does.

```csharp
DefaultSheetDataModel dataModel = FpSpread1.ActiveSheetView.DataModel as FarPoint.Web.Spread.Model.DefaultSheetDataModel;
if( dataModel != null ) {
    dataModel.AddCustomFunction(new CubeFunctionInfo());
    dataModel.AddCustomFunction(new XorFunctionInfo());
    dataModel.AddCustomFunction(new NullFunctionInfo());
}
```

The third step is to use the custom functions in formulas, as shown in this code.

```csharp
FpSpread1.ActiveSheetView.SetFormula(0, 0, "CUBE(5)");
FpSpread1.ActiveSheetView.SetFormula(1, 0, "XOR(FALSE,FALSE)");
FpSpread1.ActiveSheetView.SetFormula(1, 1, "XOR(TRUE,FALSE)");
FpSpread1.ActiveSheetView.SetFormula(1, 2, "XOR(FALSE,TRUE)");
FpSpread1.ActiveSheetView.SetFormula(1, 3, "XOR(TRUE,TRUE)");
FpSpread1.ActiveSheetView.SetFormula(2, 0, "CHOOSE(1,100,NULL(),300)");
FpSpread1.ActiveSheetView.SetFormula(2, 1, "CHOOSE(2,100,NULL(),300)");
FpSpread1.ActiveSheetView.SetFormula(2, 2, "CHOOSE(3,100,NULL(),300)");
```

## See Also

[SetFormula Method](/spreadnet/api/latest/online-asp/FarPoint.Web.Spread/FarPoint.Web.Spread.SheetView.SetFormula.html)
[AddCustomFunction Method](/spreadnet/api/latest/online-asp/FarPoint.Web.Spread/FarPoint.Web.Spread.Model.DefaultSheetDataModel.AddCustomFunction.html)