Passing the same value to multiple classes (even when one initialises the other)

Back to topics list To post a new topic, please log in or register
avatar
6
quarte 2016.08.11 18:55
 


Hello there,

How can i pass a value (a variable) from the EA to other classes, even when class A (for example) will also initialise class B?

I created a function in a class to display Print or not based on an extern variable set in the EA. While using the function in the EA is a simple exercise of passing the extern value, how do i ensure that another class initialising MUR_Verbose also uses the same bool value set in the EA?

My EA code:

bool UseVerbose = true;

(...)

MUR_Verbose *eaVerbose=new MUR_Verbose(UseVerbose);

(...)

eaVerbose.ShowPrint("Hello world");



Class Verbose

class MUR_Verbose
  {
private:
   bool m_UseVerbose;

public:
   MUR_Verbose(bool UseVerbose);
   MUR_Verbose();
  ~MUR_Verbose();
  void ShowPrint(string TextToPrint, string RealVersion);
 
  };
  
  void MUR_Verbose::ShowPrint(string TextToPrint, string RealVersion = NULL)
  {
       if (m_UseVerbose && TextToPrint != "")
         {
             Print(RealVersion + ": " + TextToPrint);
         }
  }
  


Class OpenOrder

class MUR_Helper
{
   private:
          
   public:
      bool     m_IsVerbose;
      MUR_Helper();
      ~MUR_Helper();

      
};
   
      MUR_Verbose *m_Verbose = new MUR_Verbose();
    
      
MUR_Helper::MUR_Helper()
{
  
};

MUR_Helper::Do_Something()
{
   // I'm doing stuff here, but let's print info
    
      m_Verbose.ShowPrint("Hello again...");
}      


The code above is a cut/edit/paste for illustration purposes, so don't get hooked on mistakes.

I tried passing the value in the constructor but it didn't quite work. Meaning i got an error saying that the variable hadn't been initialised.


Any other suggestions?


Thanks,

avatar
12983
WHRoeder 2016.08.11 19:26 #
 
quarte: I tried passing the value in the constructor but it didn't quite work. Meaning i got an error saying that the variable hadn't been initialised.
  1. There are no mind readers here.
  2. Post the real code with the real error.
avatar
6
quarte 2016.08.12 10:15 #
 
WHRoeder:
quarte: I tried passing the value in the constructor but it didn't quite work. Meaning i got an error saying that the variable hadn't been initialised.
  1. There are no mind readers here.
  2. Post the real code with the real error.


Hi WHRoeder,


I guess my question is more high level in the sense that i'm not sure if what i want to do is possible. I haven't found any examples in the code base and my only attempt wasn't successful.

Besides passing the value via constructor i also tried using GlobalVariables (terminal) but i got stuck on exact the same problem, in this case passing the name of the GlobalVariable to a class 2 when it's being initialised by class  1, for example.

Given your experience and knowledge, is it possible to achieve what i want?

I might be missing some C pattern or OOP solution...
avatar
1109
Ovo 2016.08.12 12:17 #
 

You have to ask properly, no one understands to a phrase like 'pass a value (a variable) from the EA to other classes'. Your example its useless as well, if you do not show implementation of the constructor and talk about an error when using a constructor. Try to express your query once more.

 

avatar
6
quarte 2016.08.12 13:08 #
 

Hi Ovo,

Clearly i don't have the OOP lingo to express myself correctly.

So let me try to explain again.


1. MUR EA takes a bool extern UseVerbose = true


2. MUR EA creates an instance of the Class Verbose (code in the original message) and pass the value of UseVerbose via the constructor to Class Verbose (this works fine)

     The UseVerbose bool is used to control whether Print() statements should be displayed or not.


3. MUR EA creates an instance of Class OpenOrder


4. Class OpenOrder creates an instance of Class Verbose with the same value from item 1. 


As i mentioned on the first message, i tried to accomplished this by passing the value of UseVerbose to the constructor of Class OpenOrder which in turn would pass it to the constructor of Class Verbose. This didn't work. It might be the implementation or it might be (and this is my question) that what i'm trying to do is not possible or not the correct way (i.e. use GlobalVariables, external file, virtual class).



avatar
1197
FMIC 2016.08.12 16:02 #
 

@quarte, You don't seem to be getting it. Both "WHRoeder" and "Ovo" have already asked you to please post the correct code (specifically, the constructors) because what you have posted is incomplete (or has no relevance).

You mention passing a value to the constructors, but your code samples don't show us the implementation of those constructors. How do you expect us to comment on their functionality if we don't know what they do?

You also mention two classes "Verbose" and "OpenOrder", yet your code is for "Verbose" and "Helper" (not "OpenOrder"), and for both you have failed to show the implementation of the constructors. In fact, for "Helper", the constructor definition does not even have a argument/parameter, so how to you expect to pass it a variable?

So, putting my "mind reader" skills to work, maybe what you want is something like this (untested):

extern bool boolUseVerbose = true;

class classVerbose
{
   private:
      bool _boolVerbose;
      
   public:      
      classVerbose( bool boolVerbose ) { _boolVerbose = boolVerbose; };
};

class classOpenOrder
{
   private:
      classVerbose *_objVerbose;

   public:      
      classOpenOrder( bool boolVerbose ) { _objVerbose = new classVerbose( boolVerbose ); };
};

classOpenOrder *objOpenOrder = new classOpenOrder( boolUseVerbose );
avatar
6
quarte 2016.08.15 13:28 #
 
FMIC:

@quarte, You don't seem to be getting it. Both "WHRoeder" and "Ovo" have already asked you to please post the correct code (specifically, the constructors) because what you have posted is incomplete (or has no relevance).

You mention passing a value to the constructors, but your code samples don't show us the implementation of those constructors. How do you expect us to comment on their functionality if we don't know what they do?

You also mention two classes "Verbose" and "OpenOrder", yet your code is for "Verbose" and "Helper" (not "OpenOrder"), and for both you have failed to show the implementation of the constructors. In fact, for "Helper", the constructor definition does not even have a argument/parameter, so how to you expect to pass it a variable?

So, putting my "mind reader" skills to work, maybe what you want is something like this (untested):


Hi FMIC,

Thanks for your example. It seems i was very close to solution, but implemented my constructor in the wrong place.

I believe the trick here is to declare the object variable as a private variable and then use the constructor to assign a value to the object, like in your example. I tried to do all at the same time, as per my code snipped, in one go.

Thanks again for proving that the concept was viable (that was my initial query) and actually showing how it's supposed to be done.


Back to topics list  

To add comments, please log in or register