Skip to main content

Undefined Values

Undefined Values

ObjectScript variables do not need to be explicitly declared or defined. As soon as you assign a value to a variable, the variable is defined. Until this first assignment, all references to this variable are undefined. You can use the $DATA function to determine if a variable is defined or undefined.

$DATA takes one or two arguments. With one argument, it simply tests if a variable has a value:

 WRITE "Does ""MyVar"" exist?",! 
 IF $DATA(MyVar) {     
         WRITE "It sure does!"  
 } ELSE {      
         WRITE "It sure doesn't!"
 }   
     
 SET MyVar = 10  
 WRITE !,!,"How about now?",! 
 IF $DATA(MyVar) {
        WRITE "It sure does!" 
 } ELSE {
        WRITE "It sure doesn't!"
 }

$DATA returns a boolean that is True (1) if the variable has a value (that is, contains data) and that is False (0) if the variable has no value (that is, contains no data). With two arguments, it performs the test and sets the second argument’s variable equal to the tested variable’s value:

 IF $DATA(Var1,Var2) {
    WRITE "Var1 has a value of ",Var2,".",!  
 } ELSE {
     WRITE "Var1 is undefined.",!
 }
 
 SET Var1 = 3
 IF $DATA(Var1,Var2) {
    WRITE "Var1 has a value of ",Var2,".",!  
 } ELSE {
     WRITE "Var1 is undefined.",!
 }
FeedbackOpens in a new tab