%d Formatting Issues

 

Morning all,

Why am I having formatting issues with my debug printing?

Despite reading the following documentation: https://docs.mql4.com/common/printformat and despite having the following code:

PrintFormat("Error Code: # %d",GetLastError());

the Journal is returning: 

2022.06.06 10:24:26.922 xxx EURAUD,M1: Error Code: #0 //This is in an external MQH document which has been attached to my EA

Despite my MessageBox() function returning an actual error code (4200 - I know why the error is returning, I'm testing the diagnostics of the EA), by applying the following: 

         MessageBox("There was an error with the ClosePosition function. Please report #"+IntegerToString(GetLastError()),"Object Create Error",MB_OK | MB_ICONERROR);

Many thanks,

OOP in MQL5 by Example: Processing Warning and Error Codes
OOP in MQL5 by Example: Processing Warning and Error Codes
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
 

When invoked, GetlastError() function resets last error code.

You can call it only once.

If you need error code multiple times, either create a variable and save the value of the GetLastError() or use _LastError variable and when you are finished displaying error code, you should call ResetlastError()

GetLastError - Checkup - MQL4 Reference
GetLastError - Checkup - MQL4 Reference
  • docs.mql4.com
GetLastError - Checkup - MQL4 Reference
 
Drazen Penic #:

When invoked, GetlastError() function resets last error code.

You can call it only once.

If you need error code multiple times, either create a variable and save the value of the GetLastError() or use _LastError variable and when you are finished displaying error code, you should call ResetlastError()

Perfect, I've applied the following:


if(!ObjectCreate(0,"spread_indicator",OBJ_LABEL,0,0,0 )){
      
            SendMail("Error Code #"+IntegerToString(_LastError)+" has occured","The Error Code #"+IntegerToString(_LastError)+" has unfortunately occured on ticket number #"+IntegerToString(OrderTicket(),0)+".\n\nFor a complete list of MQL4 Error Codes, visit https://docs.mql4.com/constants/errorswarnings/errorcodes");
      
               PrintFormat("Error Code for Spread Indicator #%d ", _LastError);
      
                  MessageBox("There was an error with the Spread Indicator function. Please report #"+IntegerToString(_LastError),"Object Create Error",MB_OK | MB_ICONERROR);
      
                     ResetLastError();
           
      }

All seems to be working now.

Much appreciated. 

 
TheHonestPrussian #: All seems to be working now.
SendMail("Error Code #"+IntegerToString(_LastError)+" has occured","The Error Code #"+IntegerToString(_LastError)+" has unfortunately occured on ticket number #"+IntegerToString(OrderTicket(),0)+".\n\nFor a complete list of MQL4 Error Codes, visit https://docs.mql4.com/constants/errorswarnings/errorcodes");

PrintFormat("Error Code for Spread Indicator #%d ", _LastError);

Now you are printing the error code for SendMail.

 
William Roeder #:

Now you are printing the error code for SendMail.

Yes. Yes I am....

Reason: