ObjectName(i) returns empty string??

 

Has anyone seen this before. I have just 5 objects drawn on a chart 4 Text objects and 1 vertical line. When I try to use the following:

  int    obj_total=ObjectsTotal();
  string name;
  for(int i=0;i<obj_total;i++)
    {
     name=ObjectName(i);
     ObjectDelete(name);
    }

The last two text objects are not deleted. GetLastError() returns 4202 and the log shows an error for ObjectDelete() complaining that the string is empty.

A further Print(name); confirms empty strings when Object Properties in the terminal shows names and descriptions.

ObjectDeleteAll() works fine however I want to add another condition for ObjectType() but cant get past this issue.

What am I missing..

Any help greatly appreciated.

 

it is probably because you are incrementing the counter while deleting the objects that are being counted therefore after the 3rd object is deleted the counter "i" is at 3 while the amount of objects left is 2 so i is no longer smaller than objects total so the cycle ends before the last 2 are deleted.

try

for (int i=obj_total; i>0; i--) 
 

I agree with SDC

I use the following code

 

// How do you combine text & code in same post ???

int DeleteAllMyObj(string pPrefix)
{
int res = 0;
string tags[];
ArrayResize(tags, ObjectsTotal());
int ixTags = 0, ix;
for(ix = 0; ix < ObjectsTotal(); ix++)
if(StringFind(ObjectName(ix), pPrefix) == 0)
{
tags[ixTags] = ObjectName(ix);
ixTags++;
}
for(ix = 0; ix < ixTags; ix++)
ObjectDelete(tags[ix]);
return (ixTags);
}
 
brewmanz:

// How do you combine text & code in same post ???

There's an src button at the top of a new post with all the text formatting buttons
 
SDC:

it is probably because you are incrementing the counter while deleting the objects that are being counted therefore after the 3rd object is deleted the counter "i" is at 3 while the amount of objects left is 2 so i is no longer smaller than objects total so the cycle ends before the last 2 are deleted.

try


Thanks SDC you were spot on! All is working just fine now..

 
brewmanz:

// How do you combine text & code in same post ???


Reason: