Unable to use Print() from within classes

Created at 15 Oct 2022, 10:59
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!
FR

freemoniescapital

Joined 15.10.2022

Unable to use Print() from within classes
15 Oct 2022, 10:59


I am unable to use the Print() method within classes, and if I attempt to do so I get an error saying that an instance of a object is required. How can I fix this?


@freemoniescapital
Replies

Shares4UsDevelopment
18 Oct 2022, 13:51

RE:

freemoniescapital said:

I am unable to use the Print() method within classes, and if I attempt to do so I get an error saying that an instance of a object is required. How can I fix this?

add a reference to your mainclass to the subclass and use 'MainclassReference.print("Hi");'


@Shares4UsDevelopment

freemoniescapital
18 Oct 2022, 14:00

Thanks for your reply

Hi,

Thanks for your reply. Do you have an example on how to implement this? Do you mean creating an instance of the mainclass within the subclass and using the 'ref' keyword? My apologies because I am not strong in C#. Thanks a lot.


@freemoniescapital

PanagiotisChar
18 Oct 2022, 14:55

Hi there,

Print() is a method of the Algo class which is inherited by your Robot class. You need to add a reference to your Robot instance to use it.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

freemoniescapital
19 Oct 2022, 17:15

Hi Panagiotis,

Appreciate your response and apologies for the late reply. If I understand correctly, is the following code correct?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TEST : Robot
    {
        protected override void OnStart()
        {
             PRINTINGCLASS printingClass = new(this);
             printingClass.Print(this);
        }

        protected override void OnTick()
        {

        }

        protected override void OnStop()
        {

        }

        public class PRINTINGCLASS
        {
            public PRINTINGCLASS(Algo algo)
            {
                algo.Print("Hello");
            }
            
            public void Print(Algo algo)
            {
                algo.Print("Hello");
            }
        }
    }
}

Is there a way to do this without having to pass Algo as a method argument?

Thanks!

 


@freemoniescapital

Waxy
19 Oct 2022, 23:52

RE:

Why can't you use Algo as an argument?


@Waxy

PanagiotisChar
20 Oct 2022, 10:06

Hi there,

Appreciate your response and apologies for the late reply. If I understand correctly, is the following code correct?

Did not test it but it's the right approach :)

Is there a way to do this without having to pass Algo as a method argument?

Nope

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

pick
20 Oct 2022, 11:25

RE:

freemoniescapital said:

Hi Panagiotis,

Appreciate your response and apologies for the late reply. If I understand correctly, is the following code correct?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TEST : Robot
    {
        protected override void OnStart()
        {
             PRINTINGCLASS printingClass = new(this);
             printingClass.Print(this);
        }

        protected override void OnTick()
        {

        }

        protected override void OnStop()
        {

        }

        public class PRINTINGCLASS
        {
            public PRINTINGCLASS(Algo algo)
            {
                algo.Print("Hello");
            }
            
            public void Print(Algo algo)
            {
                algo.Print("Hello");
            }
        }
    }
}

Is there a way to do this without having to pass Algo as a method argument?

Thanks!

 

Personally, I would approach it more like this: having a static instance of the helper class.

 

public class TEST : Robot
{
	protected override void OnStart()
	{
		new AlgoHelper(this);
		AlgoHelper.getInstance().print("start");
	}
	
	protected override void OnTick()
	{
		AlgoHelper.getInstance().print("tick");
	}
	
	protected override void OnStop()
	{
		AlgoHelper.getInstance().print("stop");
	}    
}

public class AlgoHelper 
{
	
	private static AlgoHelper thisInstance;
	private Algo algo;
	
	public AlgoHelper(Algo a)
	{
		thisInstance = this;
		algo = a;
	}
	
	public static AlgoHelper getInstance()
	{
		return thisInstance;
	}
	
	public void print(string s)
	{
		algo.print(s);
		
		//You may want to ensure it runs in main thread:
		/*
		algo.BeginInvokeOnMainThread(() =>
		{
			algo.print(s);
		});
		*/
	}
}

You could make the algo variable publicly gettable and access that from any other utility class you may create. However - be warned - you cannot use this solution if you are running two instances of the same indicator on the same chart - as it seems they share an environment. 


@pick

freemoniescapital
25 Oct 2022, 09:00

RE: RE:

Hi pick,

 

I think this works. Thank you so much :)

 

pick said:

freemoniescapital said:

Hi Panagiotis,

Appreciate your response and apologies for the late reply. If I understand correctly, is the following code correct?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TEST : Robot
    {
        protected override void OnStart()
        {
             PRINTINGCLASS printingClass = new(this);
             printingClass.Print(this);
        }

        protected override void OnTick()
        {

        }

        protected override void OnStop()
        {

        }

        public class PRINTINGCLASS
        {
            public PRINTINGCLASS(Algo algo)
            {
                algo.Print("Hello");
            }
            
            public void Print(Algo algo)
            {
                algo.Print("Hello");
            }
        }
    }
}

Is there a way to do this without having to pass Algo as a method argument?

Thanks!

 

Personally, I would approach it more like this: having a static instance of the helper class.

 

public class TEST : Robot
{
	protected override void OnStart()
	{
		new AlgoHelper(this);
		AlgoHelper.getInstance().print("start");
	}
	
	protected override void OnTick()
	{
		AlgoHelper.getInstance().print("tick");
	}
	
	protected override void OnStop()
	{
		AlgoHelper.getInstance().print("stop");
	}    
}

public class AlgoHelper 
{
	
	private static AlgoHelper thisInstance;
	private Algo algo;
	
	public AlgoHelper(Algo a)
	{
		thisInstance = this;
		algo = a;
	}
	
	public static AlgoHelper getInstance()
	{
		return thisInstance;
	}
	
	public void print(string s)
	{
		algo.print(s);
		
		//You may want to ensure it runs in main thread:
		/*
		algo.BeginInvokeOnMainThread(() =>
		{
			algo.print(s);
		});
		*/
	}
}

You could make the algo variable publicly gettable and access that from any other utility class you may create. However - be warned - you cannot use this solution if you are running two instances of the same indicator on the same chart - as it seems they share an environment. 

 


@freemoniescapital