Static Variables

Created at 01 Apr 2019, 16:44
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!
bienve.pf's avatar

bienve.pf

Joined 19.09.2018

Static Variables
01 Apr 2019, 16:44


Hello, when I create 2 instances of the same robot with different symbols, the static variables are shared and the encapsulation of each instance is broken. Is there any solution to this problem?
Why are not clean instances of static variables released?

This problem is solved if I open 2 instances of cTrader. One for each symbol of the robot. But this is tedious.

With this problem I can not use static variables.


@bienve.pf
Replies

PanagiotisCharalampous
02 Apr 2019, 11:59

Hi bienve.pf,

Why do you thing this is a problem? This behavior is the definition of static variables.

Best Regards,

Panagiotis


@PanagiotisCharalampous

bienve.pf
02 Apr 2019, 14:27

 public class MyPosition : Position
 {
      public static List<MyPosition> Instances = new List<MyPosition>();

  }

In this case multiple instances of my robot share "Instances" and that gives access problems in the thread.

The reason for this variable is to be able to check if another MyPosition class has already been created to be able to access globally with MyPosition.Instances

How could I solve this without other threads to share the static variables?

Regards!


@bienve.pf

PanagiotisCharalampous
02 Apr 2019, 17:32

Hi bienve.pf,

I am not sure why you need to give global access if you need this variable only inside this instance. 

Best Regards,

Panagiotis


@PanagiotisCharalampous

bienve.pf
02 Apr 2019, 18:22

I do not want to have global access. But only from within the robot instance. So that other instances with other symbols do not interfere with that static variable.


@bienve.pf

bienve.pf
02 Apr 2019, 18:35

Okay. I found the solution I was looking for. In case it serves to someone. [threadstatic] makes the static variable only accessible and modifiable from the same instance

 

public class MyPosition : Position
{
     [threadstatic]
     public static List<MyPosition> Instances = new List<MyPosition>();
 
 }

 


@bienve.pf