This will most likely work, but it's best not to do it. You request the same error from the terminal 2 times and do it in two different ways. At the very least, this is not logical and may confuse you in the future. By storing the GetLastError value in a variable, you are guaranteed to use the same value in both the if statement and the Print function.
_LastError is a global variable, you can read it as often as you want, it has zero influence on the actual value.
GetLastError () is a function, and it will reset the value in _LastError.
@anuj71 used both GetLastError and _LastError. This is why I suggested using a variable. If @anuj71 explicitly reset the error using GetLastError, then ok.
In any case, I think it's better to use either GetLastError only or _LastError only. And I would suggest adding ResetLastError before OrderModify
[EDIT]
By "GetLastError only" I mean assigning the value of GetLastError to a variable
Yes, I think ResetLastError before OrderModify would be appropriate. Now you can be sure that if you find an error (in an if statement), that error occurred in OrderModify
Yes, I think ResetLastError before OrderModify would be appropriate. Now you can be sure that if you find an error (in an if statement), that error occurred in OrderModify
The point is, GetLastError will call ResetLastError implicit. That's why the variable _LastError is cleared after a call to GetLastError.
So if you need the error code multiple times, you can simply use _LastError, as it is a variable.
If you want to reset the current error code, you either call GetLastError, which will give you the error code in return AND reset the value, or you just call ResetLastError, which will not give you the last error code, and simply reset the code to zero.
There is no requirement for a secondary variable to store the error code, except maybe for some special cases, which is not given here.
So, if you want to first compare the current error code, you use _LastError, and if you want to print and reset the error code, you can safely use GetLastError.
This will most likely work, but it's best not to do it. You request the same error from the terminal 2 times and do it in two different ways. At the very least, this is not logical and may confuse you in the future. By storing the GetLastError value in a variable, you are guaranteed to use the same value in both the if statement and the Print function.
I agree.
I got this and thanks for this code but what the issue with my this code :
Is this correct, here i am not calling GetLastError on if statement
Perhaps you should read the manual. GetLastError - Checkup - MQL4 Reference
How To Ask Questions The Smart Way. (2004)
How To Interpret Answers.
RTFM and STFW: How To Tell You've Seriously Screwed Up.
The second call always returns zero.
After the function call, the contents of _LastError are reset.the __FUNCTION__ is always equal.
Thanks for this information.
This code is perfect?
Forum on trading, automated trading systems and testing trading strategies
GetLastError with Function name using if statement
anuj71, 2024.02.19 10:20
@anuj71 used both GetLastError and _LastError. This is why I suggested using a variable. If @anuj71 explicitly reset the error using GetLastError, then ok.
In any case, I think it's better to use either GetLastError only or _LastError only. And I would suggest adding ResetLastError before OrderModify
[EDIT]
By "GetLastError only" I mean assigning the value of GetLastError to a variable
I would suggest adding ResetLastError before OrderModify
Like this?
Yes, I think ResetLastError before OrderModify would be appropriate. Now you can be sure that if you find an error (in an if statement), that error occurred in OrderModify
Yes, I think ResetLastError before OrderModify would be appropriate. Now you can be sure that if you find an error (in an if statement), that error occurred in OrderModify
Thanks for this information.
This code is perfect?