Aug
19

2

Nullable Boolean

Strange I find myself writing about the primative, Boolean. Its a primative, why would you ask any more of it? As it stands, the Boolean has two states; true and false. On more than one accasion I have needed boolean to support one more value; null. Lets suppose you have a form and you want users to complete it. If the user has not answered a true / false question, null is a valid value. You can not suppose that unaswered questions are false. Let me present to you the NullableBoolean. Instead of using the variable name to compare against, you use the ‘value’ field. By adding a ‘value’ field I am able to make the object answer true / false / null.

Happy Coding

package ca.components
{
    
public class NullableBoolean
    {
        
protected var isNull Boolean;
        protected var 
boolean Boolean;
        
        public function 
NullableBooleanarg : * = null )
        
{
            isNull 
= new Boolean(true);
            
value arg;
        
}
        
        
public function set valuearg : * ) : void
        {
            
if( arg is Boolean )
            
{
                isNull 
false;
                
boolean arg;
                return;
            
}
            
            
if( arg is String )
            
{
                isNull 
false;
                
arg Stringarg ).toLowerCase();
                
                if( 
arg == "true" )
                
{
                    boolean 
true;
                    return;
                
}
                
                
if( arg == "false" )
                
{
                    boolean 
false;
                    return;
                
}
            }
            
            isNull 
true;    
        
}
        
        
        
public function get value() : Object 
        {
            
if( !isNull )
            
{
                
return boolean;    
            
}
            
            
return null;
        
}
    }     


The code below is used to show how the NullableBoolean works.

public function testarg NullableBoolean ) : void
    {
        
var NullableBoolean = new NullableBoolean();
        
        
b.value true;
        
whatIsMyValue);
        
        
b.value false;
        
whatIsMyValue);
        
        
b.value "true";
        
whatIsMyValue);
        
        
b.value "false";
        
whatIsMyValue);
        
        
b.value null;
        
whatIsMyValue);
    
}
    
    
    
public function whatIsMyValuearg NullableBoolean ) : void
    {
        trace
("The value is: ");
        
        if( 
arg.value == true )
        
{
            trace
(" True");    
        
}
        
        
if( arg.value == false )
        
{
            trace
(" False");    
        
}
        
        
if( arg.value == null )
        
{
            trace
(" Null");    
        
}
        
    } 

Comments

Good work. Thank you.

Posted by Steve Bianucci on Aug 28, 2009

Yep, encountered the same problem multiple times. This is a nice ActionScript solution; the only thing I’d add is that if objects containing NullableBooleans are persisted using a different (likely server-side) technology, then you probably need to address the same issue on that side of the fence as well. Still, I like this better than using, say, uint values of 0, 1 and 2, denoted by constants, because in this case you can still use the object where a the expression is evaluated as a Boolean (e.g. ‘if (!myNullableBoolean) hideTheThing()’ will work because Boolean(null) == Boolean.FALSE)

Posted by Chris Luebcke on Oct 23, 2009

Enter a comment - moderated

Add Comment