Deleting Objects

 

I was writing a deinit() to try and delete all the objects associated with the indicator that created them on close of that indicator.

All the objects created by the indicator start with the letters T16 followed by some other identifiers including a number for each arrow.

I wrote this short code to test if it would find the objects starting with "T16"

int deinit()
  {int i, ot=ObjectsTotal();
   string id;

   for(i=ot;i>=0;i--)
     {id=ObjectName(i);
      if(StringSubstr(id,1,3)=="T16")
      Alert("Object name is ",ObjectName(i));
     }
   return(0);
  }

The strange this is, it is finding the objects plus other objects that are not even listed in the objects list I get when right clicking the chart and select Objects List here is the Alerts showing it found these objects

So anyway, then I wrote this code:

int deinit()
  {int i, ot=ObjectsTotal();
   string id;
   for(i=ot;i>=0;i--)
     {id=ObjectName(i);
      if(StringSubstr(id,1,3)=="T16")
      ObjectDelete(id);
     }
   return(0);
  }

This does not delete all the objects. when I close the indicator a lot of them remain on the screen why would this happen? Here is the objects list after I loaded and then deleted that indicator, so none of these were deleted you can see they all start with T16:

 
SDC:

I was writing a deinit() to try and delete all the objects associated with the indicator that created them on close of that indicator.

All the objects created by the indicator start with the letters T16 followed by some other identifiers including a number for each arrow.

I wrote this short code to test if it would find the objects starting with "T16"

The strange this is, it is finding the objects plus other objects that are not even listed in the objects list I get when right clicking the chart and select Objects List here is the Alerts showing it found these objects

So anyway, then I wrote this code:

This does not delete all the objects. when I close the indicator a lot of them remain on the screen why would this happen? Here is the objects list after I loaded and then deleted that indicator, so none of these were deleted you can see they all start with T16:


Try "GetLastError()" function after deleting ....

for(i=ot;i>=0;i--) must not be for(i=ot-1;i>=0;i--)?

brgds

 

Yess your right I should have used ObjectsTotal()-1

I also found the other part of the problem, I thought StringSubstr(id,1,3) would get the first three letters of the name as 1 is the first letter, it seems this was wrong, I ran a test to see what StringSubstr(id,1,3); would do, it returns "16" instead of T16. It appears I should have used

StringSubstr(id,0,3);

So now this should work correctly I believe.

int deinit()
  {
//----
   int i, ot=ObjectsTotal()-1;
   string id;
//----
   for(i=ot;i>=0;i--)
    {id=ObjectName(i);
     if(StringSubstr(id,0,3)=="T16")
      {ObjectDelete(id);
      }
    }
   return(0);
  }