Losing the plot with GetLastError()

 

So in my code, I set a global variable and want to check that it succeded. I call get last error and print the result but get an odd result saying 4002 array index out of range. The previous line uses iMAOnArray() so I decide to check for error in this line... same answer but now the globalvariableset() produces correctly 0 for error code... I can't see any problem with the code for iMAOnArray() so decide to check the line above and get the same error but now both imaonarray and globalvariableset pass with 0 error. Clearly the error code is being generated elsewhere in the code and I need to track it down, but what I don't understand is why would that value be passed to getlasterror after globalvariableset executed without error. Shouldn't getlasterror show as 0 regardless of errors elsewhere in the code? Any thoughts?

The butchered code below will print 4002 regardless of where get last error is first called and works fine after that.

if (crossuplen>crosslentrig)
            {
            err=GetLastError();
            Print("if ",err);
            check("if Failed- ",err);
            crossuparray[0]=crossuplen;
            err=GetLastError();
            Print("fill ",err);
            check("Array fill Failed- ",err);
            crossupavg= iMAOnArray(crossuparray,0,10,0,MODE_SMA,0);
            err=GetLastError();
            Print("imaoa ",err);
            check("iMAOnArray Failed- ",err);
            GlobalVariableSet(globcuplenstring,crossupavg);
            err=GetLastError();
            Print("glob ",err);
            check("Global globcrossuplen Failed- ",err);
            }


void  check(string text,int err)
      {
      if (err!=0)
         {
         Alert (text, ErrorDescription(err));
         }
      return(0);
      }


V

 

Some functions change the value in last_error every time they are called. Examples are OrderSend(), which always changes the content of last_error to the status and GetLastError() which always changes the content of last_error to 0.

Some other functions, including GlobalVariableSet(), if they are successful, do not touch the content of last_error at all.

I'd start moving your error hunt back through your code.

CB

 

Thanks CB... yeah, I've found and fixed my error, I was just surprised when GlobalVariableSet() produced an error code for an error some 30 lines back up the code. The docs for GlobalVariableSet() say to call GetLastError(), but it would appear it will only change last_error if there is actually an error and ignore last_error if it succeded. That seems a bit silly to me, but presumably one needs to check the returned value to see if it failed before asking for an error code. Is there a way to differentiate between those functions that require this check and those that don't. Would it just be better practice to check every returned value before checking for error regardless of function?

V

 

You'll see here which functions obey which behavior pattern.

https://docs.mql4.com/runtime/errors

CB

 

Perfect. Thanks

V

Reason: