Does local variable exist?

K.V.

Can we check to see whether or not a local variable exists, without throwing an error if it does not?

I.e., say we have a script with the local variable: object.

If I put:

if(TypeOf(object) = null){
  return(false)
}
else{
  return(true)
}

... it throws an error if object does not exist.

Error running script: Error compiling expression 'TypeOf(object)': Unknown object or variable 'object'

...but, if i create object setting it to null, it works:

object = null
msg(TypeOf(object))

NOTE: I can check for an attribute on an object with TypeOf(), and it will return null if the attribute has never been created at all.


Anyhoo, I think I read through all the docs and threads concerning this, and I've found no way to get around a local variable which has never existed inside the script.

Anyone know a trick?


if (IsDefined("object")) {
  msg ("'object' is a "+TypeOf(object))
}
else {
  msg ("There's no variable 'object'")
}

K.V.

Hello, mrangel!

I already tried that one:

if (not IsDefined(object)) {
  msg ("object does not exist")
}

Error running script: Error compiling expression 'not IsDefined(object)': Unknown object or variable 'object'


I never thought of using quotation marks, though. That works!

Thank you!!!


Yeah, IsDefined takes a string, the name of a variable. It can't take a direct variable as its argument, because the "unknown variable" error is generated before it attempts to run the function.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums