Class declaration issue

 

Hello everyone,


I'd like some help regarding a class declaration, I basically want to declare a new class on a global scope to be used in different functions, but this class uses variables as parameters that may not be correctly initialised until the OnInit() function, is there a way to declare a class in a local scope and make it a global class ? 

Or maybe can I declare the class on the global scope and then Initialise it in the local scope, I mean if a class is declared, it will automatically be initialised I suppose.


extern string InpChannelName = "";
extern string InpBotToken = "";

string Channel_Name = InpChannelName;                                  // DECLARATION OF CHANNEL NAME
string Bot_Token = InpBotToken;                                        // DECLARATION OF TOKEN API
Bot Telegram(Channel_Name, Bot_Token);                                 // TELEGRAM OBJECT CREATION

//+------------------------------------------------------------------+
//| Expert initialization function                                     |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
        CheckInputs();                                                     // CHECK IF INPUTS ARE BY DEFAULT OR PERSONAL
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|CHECK INPUTS                                                      |
//+------------------------------------------------------------------+
void CheckInputs()
  {
   if(Channel_Name == "")                                           // USE DEFAULT CHANNEL IF EMPTY IN INPUTS
     {
      Channel_Name = "@xxxx";                                    // DEFAULT CHANNEL TO CHANGE IN LATER STAGE IF NEEDED
     }
   if(Bot_Token == "")                                                  // USE DEFAULT BOT TOKEN IF EMPTY IN INPUTS
     {
      Bot_Token = "xxxxx";                                      // DEFAULT BOT TOKEN TO CHANGE IN LATER STAGE IF NEEDED
     }
  }

Here is my example, I basically declare and initialise my Bot class before the OnInit() function but obviously the Channel_name and Bot_token variables may not be correct as it hasn't gone through the CheckInputs() function yet. I am scratching my head here to find a solution, if someone could advise me on this that would be amazing!!


Thank you very much.

 

Use a pointer.

string Channel_Name = InpChannelName;                                  // DECLARATION OF CHANNEL NAME
string Bot_Token = InpBotToken;                                        // DECLARATION OF TOKEN API
Bot *Telegram;                                                                  // TELEGRAM POINTER DECLARATION
//+------------------------------------------------------------------+
//| Expert initialization function                                     |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
        CheckInputs();                                                     // CHECK IF INPUTS ARE BY DEFAULT OR PERSONAL
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|CHECK INPUTS                                                      |
//+------------------------------------------------------------------+
void CheckInputs()
  {
   if(Channel_Name == "")                                           // USE DEFAULT CHANNEL IF EMPTY IN INPUTS
     {
      Channel_Name = "@xxxx";                                    // DEFAULT CHANNEL TO CHANGE IN LATER STAGE IF NEEDED
     }
   if(Bot_Token == "")                                                  // USE DEFAULT BOT TOKEN IF EMPTY IN INPUTS
     {
      Bot_Token = "xxxxx";                                      // DEFAULT BOT TOKEN TO CHANGE IN LATER STAGE IF NEEDED
     }
   Telegram = new Bot(Channel_Name, Bot_Token); // Object creation
  }

Not tested.

Don't forget to delete the pointer in OnDeInit().

 
Alain Verleyen #:

Use a pointer.

Not tested.

Don't forget to delete the pointer in OnDeInit().

Brilliant !

Thanks for your quick answer Alain, saved me some head scratching there !

Will test it tomorrow.