How to delete OBJ_TEXT by int deinit() ? - page 2

 
GumRai:

Do you really think that the OP will be able to understand that?
If the OP can't then the OP should learn or go here: Jobs
 

Many of us are here because we want to learn

Being a novice myself, I found that code very difficult to follow, but a good exercise

#define ONDA_BEGINS     0
#define ONDA_CONTAINS   1
void ObjectNameDeleteAll(string name, int where=ONDA_BEGINS, int type=EMPTY){
   for(int iObj=ObjectsTotal()-1; iObj >= 0; iObj--){
      if(type != EMPTY) if( type != ObjectType(on) )  continue;
      string   on = ObjectName(iObj);
      if(name != ""){
         int   iPos  = StringFind(on, name);
         if (iPos < 0)                             continue;
         if (iPos > 0 && where == ONDA_BEGINS)     continue;
      }
      ObjectDelete(on);
   }
}
int deinit(){
   ObjectNameDeleteAll("Point", ONDA_BEGINS, OBJ_TEXT);

I believe that part of my difficulty was this

if(type != EMPTY) if( type != ObjectType(on) )  continue;
string   on = ObjectName(iObj);

Should these 2 lines be reversed to be

string   on = ObjectName(iObj);
if(type != EMPTY) if( type != ObjectType(on) )  continue;

?

Although I realise that having a function to delete objects is a great idea as it can be included in a template, I just felt that it is a bit over-complicated a response to the OP as he seems to be at the same level of ability as myself.

Not as clever, but I believe that it answers the OP's question.

int deinit()
  {
//----
       
    for(int iObj=ObjectsTotal()-1; iObj >= 0; iObj--)
      {
      string obname = ObjectName(iObj);
      if(StringSubstr(obname,0, 5) =="Point" && ObjectType(obname)==OBJ_TEXT )
      ObjectDelete(obname);
      }
//----
   return(0);
  }
 
GumRai: Should these 2 lines be reversed to be
Definitely.
 
GumRai:
Although I realise that having a function to delete objects is a great idea as it can be included in a template, I just felt that it is a bit over-complicated a response to the OP as he seems to be at the same level of ability as myself.
if(StringSubstr(obname,0, 5) =="Point" && ObjectType(obname)==OBJ_TEXT )
Not as clever, but I believe that it answers the OP's question.
How many times (and how long) are you going to look at the two lines below before you figure out why neither works?

if(StringSubstr(obname,0, 5) ==" Point" && ObjectType(obname)==OBJ_TEXT )
if(StringSubstr(obname,0, 5) == "Points" && ObjectType(obname)==OBJ_TEXT )

Now you have hard coded the name. If you need another object name, you now have to write more code, and debug it again.


ObjectNameDeleteAll("Point");

Does it all, whither is the object starts with the name, contains the name, all object types, one object type, and you only have to debug it once, not how many times you need it.

Always factor your code into functions. Even trivial functions

#define HR2400 86400       // 24 * 3600
int      TimeOfDay(datetime when){  return( when % HR2400          );         }
datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );         }
datetime Today(){    return(DateOfDay(Time[0]);                               }
datetime Tomorrow(){ return(Today() + HR2400);                                }
Now when I decide to shift the daily by several hours (eliminate the 1-2 hour Sunday) I can use:
datetime Today(){    return(daily.acr[0][ACR_TIME]);                        }
where daily.acr are synthetic daily bars. No further testing on the remaining code needs to be done. If daily.acr is calculated correctly, so is Today and therefor the code that calls Tomorrow().
 

WHRoeder:
How many times (and how long) are you going to look at the two lines below before you figure out why neither works?

if(StringSubstr(obname,0, 5) ==" Point" && ObjectType(obname)==OBJ_TEXT )
if(StringSubstr(obname,0, 5) == "Points" && ObjectType(obname)==OBJ_TEXT )

About the same amount of times and length of time that I would have to look at

 ObjectNameDeleteAll(" Point", ONDA_BEGINS, OBJ_TEXT);
 ObjectNameDeleteAll("Points", ONDA_BEGINS, OBJ_TEXT);

to figure out why neither works. I don't see your point (no pun intended) :)

Both would return -1 from the StringFind function wouldn't they?

As I already wrote in my post

I realise that having a function to delete objects is a great idea

 

The " Point" wouldn't work but (I think) it's obvious in the function call, not in the IF.

You you created objects called "Points" the function works. The IF won't.

 
WHRoeder:

The " Point" wouldn't work but (I think) it's obvious in the function call, not in the IF.

You you created objects called "Points" the function works. The IF won't.


Well it was immediately obvious to me in the IF code that you posted.

As the OP was trying to delete objects beginning with "Point", not "Points", I don't see the relevance

If using "Points", the code would need to be changed slightly, obviously.

I am not denying that your way of having the function, with a lot more flexibility and ready to go is far superior, just that it is possibly a little bit complicated for novice coders.

 
GumRai: If using "Points", the code would need to be changed slightly, obviously.
if(StringSubstr(obname,0, 5) == "Points" && ObjectType(obname)==OBJ_TEXT )
It's not obvious that the above modified code is broken. You can't just change "Point" to "Points". You'll spend hours or days trying to track down that error.
 
WHRoeder:
It's not obvious that the above modified code is broken. You can't just change "Point" to "Points". You'll spend hours or days trying to track down that error.


When you are quoting my post, please do not add your code within the quote area. My post did not include that code.

Yes, you would have to take into account that "Points" has 6 letters.

Reason: