Delete object if exists

Caio shared this question 2 years ago
Answered

I want to delete an object if it exists using IsDefined() but even using this command I get an error saying: undefined variable a ..

I'm trying this If(IsDefined(a), Delete(a)) hope someone help me please


Thanks in advance

Comments (5)

photo
3

Write without any condition:

Delete(a)

nothing is happen when "a" not exist. After Delete(a) you can be sure that "a" not exist.

-----------------------------------------------------------------------------------------------

Note1: IsDefined() is

- false when the object exist and the value is undefined

- true when the object exist and the value is not undefined

- go to an error if object not exist (see also exception Note2)

only in JavaScript (not in ggb-scripts) you can ask "exists(String objName)"

.

Note2: When a table-cell is empty (not exist)

after IsDefined(<empty-cell>) no error is happen but now the cell is not empty (the object exists) and the value of the cell is undefined, so the answer is false.

photo
1

This really worked! Was exactly what I needed thank you !!

photo
1

It is really annoying to not have such a function check for object existence till today. Thanks for your hint on the javascript trick.

photo
1

Try IsDefined(Object("A"))


You have to to very careful with the use of Object() though, in particular the order in the construction protocol

photo
1

@Michael, the following should assign '1' to "d" if "AA" is defined and '0' if not,

d=If(IsDefined(Object("AA")),1,0)

However, I can't find any value assigned to "d"

/ZmaFTWtPudEAAAAASUVORK5CYII=

photo
© 2021 International GeoGebra Institute