Solved

Boolean Question

Posted on 2004-10-06
14
460 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in script 11 58
What is "public object" in C#? 3 47
Reference issues in Visual Studio 3 30
Where did System.Data.Objects go? 2 28
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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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