Solved

Boolean Question

Posted on 2004-10-06
14
459 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now