Solved

Boolean Question

Posted on 2004-10-06
14
462 Views
Last Modified: 2008-03-17
When I try to compile this code:

Boolean boolTransferCallback;

if(boolTransferCallback == null) {
}

I get the following error:

Operator '==' cannot be applied to operands of type 'bool' and '<null>'

This make sense for the primitive bool type, but I thought I was declaring the Boolean OBJECT type, which I thought could be treated with object semantics.

In case you are wondering why I used Boolean at all, I am retrieving the value from a Hashtable which I belive can only store objects.
0
Comment
Question by:derekl
  • 6
  • 5
  • 3
14 Comments
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12241393
from the Help:
"Instances of this type have values of either true or false."

you cannot have a null value in that variable, you can have nulls in database fields, but that's just a local C# variable, you can't have nulls in that
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12241423
Nope you can't.

But you can store a 'true' or 'false' value in a Hashtable... :)
0
 

Author Comment

by:derekl
ID: 12241487
Wait a minute.  I obviously can have a null local variable:

public void foo()
{
    string strBar = null;
}

I guess I was working under the assumption that Boolean was a first rate object in .NET.  I was assuming that because a hashtable stores only objects that the following code:

    this.m_hash[key] = false;

promotes the false value to an object wrapping the value before storing it.  If not, what happens when I try to get the key out using something like the following:

    object obj = this.m_hash[key];

I am assuming I do indeed get an object in return, which I can test for null.

It sounds like when I cast it to a (bool) or (Boolean) I lose the right to query it foir null?  Is this correct?

I guess what it really boils down to is what is a 'false' value stored as in a hashtable.
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12241514
basically value types (int, bool, double) cannot be assigned null, they are pretty much like primitive types in that aspect
0
 

Author Comment

by:derekl
ID: 12241551
How are they stored in the hashtable then?
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12241571
I assume that when you store a boolean value like 'false' in your hashtable it will be wrapped to the Boolean object which are basically the same. And yeah, you cannot check it for a null value because a boolean type can never be null anyway :)
0
 

Author Comment

by:derekl
ID: 12241653
But according to the compiler I cannot test a Boolean object for null.  This leads me to believe it's not a first rate object.  I know in the docs it says Boolean is a structure.  Are these different than objects in that they cannot have a null value, but can be stored in a hashtable?  This would not make sense sinse Hashtable::Get returns an object reference so I would assume the Boolean type does have object semantics.
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12241669
that's the "benefit" of strict type checking languages like Delphi and C#

for how they are stored in the HashTable, see the Boolean.GetHashCode, that's what is used to be stored in the HashTable
0
 

Author Comment

by:derekl
ID: 12241691
I think GetHashCode is used if an object is to be a key in a hashtable, but not if it simply a value.
0
 

Author Comment

by:derekl
ID: 12241801
From what I can tell, bool and Boolean are value types which may be stored in a hashtable but which cannot have a null value.

Obviously this line fails to compile:

Boolean bTest = null;

Cannot convert null to 'bool' because it is a value type

But what happens if I mistakenly attempt to cast a null reference to a boolean at runtime:

Boolean bTest = (Boolean) m_hash[key];

Would I get an exception or would the null silently be coerced into a 'false' value?
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12241826
why don't you just try?

I think it would silently go to false if there was null, but I haven't tried
0
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 50 total points
ID: 12241862
never mind... you'll get a pretty:
 Object reference not set to an instance of an object.

makes sense...
0
 

Author Comment

by:derekl
ID: 12242033
OK, here is what I beliieve is the answer.

Value types such as bool and Boolean or any user defined structs have object semantics in that the methods of object can be called on them.  As value types however, it makes no sense that they have a null value and therefore cannot be cast from a null reference.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 12242105
Like BlackTigerX says, it'll be Object reference not set to an instance of an object. when you attempt to get a non-existent key, or a key with a null-value. Or a 'Specified cast is not valid' for any other invalid type.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question