Using Custom Methods

Learn how to define and use custom methods in your C# code actions

In addition to the Execute() method, you can also define your own custom methods within the CPHInline class.

Adding Custom Methods

To add a custom method, simply define a new method within your CPHInline class.

You can call your custom method from within the Execute() method or from any other methods in the same class. This includes Lifecycle Methods, or even other custom methods you have defined.

Example.cs
public bool Execute()
{
    CustomMethod(); // Call your custom method
    int sum = Add(5, 10); // Call another custom method that takes parameters
    return true;
}

public void CustomMethod()
{
    CPH.LogInfo("This is a custom method!"); // You can even use CPH methods here!
}

public int Add(int a, int b)
{
    return a + b; // This method takes two integers and returns their sum
}

This allows you to organize your code better, especially if you have repetitive tasks or complex logic that you want to encapsulate in separate methods.

Sharing Methods Between Actions

You can also expose your custom methods to other actions, allowing you to execute them with from a sub-action, or even call them directly from other C# code actions.

To do this, your method must be declared as outlined below:

  • Visibility must be public
  • Method must not take any parameters
  • Return type must be bool
    • Return true to indicate success
    • Return false to indicate failure and stop executeion of further sub-actions
Example.cs
public bool CustomMethod()
{
    CPH.LogInfo("This is a custom method that can be called from other actions!");
    return true; // Return true to indicate success
}
You must define a Name in your Execute C# Code sub-action settings for it to be discoverable by other actions.
Read more in API > Sub Actions > Core > C# > Execute C# Code

Execute Method Sub-Action

To call your custom method from another action, you can use the Execute C# Method sub-action.

Read more in API > Sub Actions > Core > C# > Execute C# Method

C# ExecuteMethod

You can also call your custom method directly from another C# code action using the ExecuteMethod() method of the CPH class.

Example.cs
public bool Execute()
{
    CPH.ExecuteMethod("YourActionName", "CustomMethod");     return true;
}
Replace YourActionName with the Name defined in your Execute C# Code sub-action settings and CustomMethod with the name of your C# method.