Solved

Boolean Question

Posted on 2004-10-06
14
457 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

24 Experts available now in Live!

Get 1:1 Help Now