GetLastError() result always calculates as 0>, even when it's zero...?

 

Hi all,

I'm trying to introduce some logic using GetLastError(), and having trouble with it.

Here's the bit of code I'm working with right now

if(GetLastError() == 0) PlaySound("news.wav");
else Print("Open order error: " + errortext(GetLastError())); PlaySound("expert.wav");

My order is going through fine, but the else condition is always triggered. Confusingly, my printed error result here is 0.

I fumbled around with this for a previous segment of this EA and abandoned it, but I'd like to include this part. Any ideas what my problem is?

 
rrsch:

Hi all,

I'm trying to introduce some logic using GetLastError(), and having trouble with it.

Here's the bit of code I'm working with right now

My order is going through fine, but the else condition is always triggered. Confusingly, my printed error result here is 0.

I fumbled around with this for a previous segment of this EA and abandoned it, but I'd like to include this part. Any ideas what my problem is?


GetLastError() resets error code when invoked - you can call it only once.

If you want to access error code multiple times, use _LastError variable and after that call ResetLastError() to clear the error code.


In your sample code, when GetLastError() is called as part of the if condition, _LastError variable is cleared, and in else you don't have error code any more.

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

GetLastError() resets error code when invoked - you can call it only once.

If you want to access error code multiple times, use _LastError variable and after that call ResetLastError() to clear the error code.

Or put GetLastError into a variable and access that multiple times.

 
Great, thanks for the help guys. I was banging my head on the table with that one! Should have read the doc more carefully.