Hello There! How do we call method in main class from another class

Created at 25 Apr 2021, 08:11
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
IM

imrealfighter

Joined 09.11.2020

Hello There! How do we call method in main class from another class
25 Apr 2021, 08:11


Hello There ! 

How do we call method that we created in main class (a class that contain OnTick() OnStart() OnStop()) 

from a class that we create by ourselves. 

thank you.  


@imrealfighter
Replies

amusleh
25 Apr 2021, 11:14

Hi,

Those are methods with "protected" access modifier, you can't call a protected method from another class, only the class itself or its derived classes can call a protected method.

Here is an example that might help you:

using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Test : Robot
    {
        protected override void OnStart()
        {
            var botController = new BotController(this);
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }

    public class BotController
    {
        private readonly Robot _robot;

        public BotController(Robot robot)
        {
            _robot = robot;
        }

        public void Stop()
        {
            _robot.Stop();
        }

        public IAccount GetAccount()
        {
            return _robot.Account;
        }
    }
}

These kind of topics aren't related to cTrader automate API, you have to learn C# and Google for similar issues in C#, you will find a lot of solutions.

There is a way to call protected methods via Reflection, Google and you will find lots of code examples for it.

 


@amusleh