How to use custom classes?

Created at 03 Oct 2016, 09:00
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!
CL

cloesd

Joined 28.02.2015

How to use custom classes?
03 Oct 2016, 09:00


 I'm having trouble implementing simple custom classes:

Below is my code, I simply want to test making a seperate class and printing out a variable from that class in the ontick field.

 

It compiles fine, but causes error when run:

03/10/2016 16:50:24.446 | Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

 

I am quite new to this kind of programming .

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        public testcass testie;

        protected override void OnStart()
        {
            testcass testie = new testcass();
        }

        protected override void OnTick()
        {
            testie.test();
        }

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


public class testcass : Robot
{

    private int x = 4;



    public void test()
    {
        Print(x);
    }


}

 


@cloesd
Replies

cloesd
06 Oct 2016, 14:58

RE:

cloesd said:

 I'm having trouble implementing simple custom classes:

Below is my code, I simply want to test making a seperate class and printing out a variable from that class in the ontick field.

 

It compiles fine, but causes error when run:

03/10/2016 16:50:24.446 | Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

 

I am quite new to this kind of programming .

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        public testcass testie;

        protected override void OnStart()
        {
            testcass testie = new testcass();
        }

        protected override void OnTick()
        {
            testie.test();
        }

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


public class testcass : Robot
{

    private int x = 4;



    public void test()
    {
        Print(x);
    }


}

 

Anyone please help, This has put on hold a rather big project I've got planned.


@cloesd

cloesd
07 Oct 2016, 13:48

RE: RE:

cloesd said:

cloesd said:

 I'm having trouble implementing simple custom classes:

Below is my code, I simply want to test making a seperate class and printing out a variable from that class in the ontick field.

 

It compiles fine, but causes error when run:

03/10/2016 16:50:24.446 | Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

 

I am quite new to this kind of programming .

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        public testcass testie;

        protected override void OnStart()
        {
            testcass testie = new testcass();
        }

        protected override void OnTick()
        {
            testie.test();
        }

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


public class testcass : Robot
{

    private int x = 4;



    public void test()
    {
        Print(x);
    }


}

 

Anyone please help, This has put on hold a rather big project I've got planned.

Will offer the first reply, a copy of the finished algo free via email.

 


@cloesd

Jiri
07 Oct 2016, 14:27

Hi, you need to put the class in same namespace. Then you should send the algo as parameter in which it should be printed. See sample below.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        private TestClass testClass;

        protected override void OnStart()
        {
            testClass = new TestClass();
        }

        protected override void OnTick()
        {
            testClass.Test(this);
        }
    }

    public class TestClass
    {
        private int x = 4;

        public void Test(Algo algo)
        {
            algo.Print(x);
        }
    }
}

If you need any further help, feel free to contact me via email.


@Jiri

cloesd
07 Oct 2016, 14:35

RE:

tmc. said:

Hi, you need to put the class in same namespace. Then you should send the algo as parameter in which it should be printed. See sample below.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        private TestClass testClass;

        protected override void OnStart()
        {
            testClass = new TestClass();
        }

        protected override void OnTick()
        {
            testClass.Test(this);
        }
    }

    public class TestClass
    {
        private int x = 4;

        public void Test(Algo algo)
        {
            algo.Print(x);
        }
    }
}

If you need any further help, feel free to contact me via email.

 

 

Thanks heaps!


@cloesd