473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Boolean, Int32

Hi

is it it correct that variables of types like Boolean and Int32 cannot be
null?

It appears these types are structs, and not classes and therefore variables
cannot be assigned the value null (like DateTime as well I think). Is there
anyone else who finds this annoying?

Is the only way around this to wrap these types in your own class? (Why have
Boolean actually, when it really is only a bool primitive and not a class?)
Thanks
Peter
Nov 25 '05 #1
4 1948
Peter Kirk wrote:
is it it correct that variables of types like Boolean and Int32 cannot be
null?
Yes.
It appears these types are structs, and not classes and therefore variables
cannot be assigned the value null (like DateTime as well I think).
Indeed - as documented on MSDN. (Look at the docs for any of these
types and you'll see that they're structs.)
Is there anyone else who finds this annoying?
Not very often, to be honest. Then again, I don't do much data binding
etc.
Is the only way around this to wrap these types in your own class?
Not quite. You could wrap them in your own structs which have an extra
field specifying whether they're "null" or not - that would make them
keep value semantics.

For 1.1, there's a NullableTypes project on SourceForge:
http://nullabletypes.sourceforge.net/

In C#/.NET 2.0, there's the concept of Nullable<T> built into the
language:
int? x = null;
(Why have Boolean actually, when it really is only a bool primitive and not a class?)


What do you mean here? There's just as much cause to have Boolean as a
type as there is to have Int32. Note that "bool" is just a C# shorthand
for "System.Boolean".

Jon

Nov 25 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.com> skrev i en meddelelse
news:11**********************@g49g2000cwa.googlegr oups.com...
Peter Kirk wrote:
It appears these types are structs, and not classes and therefore
variables
cannot be assigned the value null (like DateTime as well I think).
Indeed - as documented on MSDN. (Look at the docs for any of these
types and you'll see that they're structs.)
Is there anyone else who finds this annoying?


Not very often, to be honest. Then again, I don't do much data binding
etc.


Yes, it is exactly here I find difficulties - especially with Boolean for
which it is difficult to define a "null value" - there are only two valid
values as it is. With Int32 or DateTime one can of course define some value
as null (like the minimum or maximum allowable value for the respective
type).

Is the only way around this to wrap these types in your own class?


Not quite. You could wrap them in your own structs which have an extra
field specifying whether they're "null" or not - that would make them
keep value semantics.


I'm not 100% following you here. Do you mean define a "MyInt32" struct which
contains an Int32, and a bool field for "IsNull" - which I then need to
check when I use MyInt32 (and ensure I set it correctly as well when I set
the value of the Int32)?

For 1.1, there's a NullableTypes project on SourceForge:
http://nullabletypes.sourceforge.net/
Thanks, I'll look at that.

In C#/.NET 2.0, there's the concept of Nullable<T> built into the
language:
int? x = null;


Yes, I did see this but I am not using .NET 2.

(Why have Boolean actually, when it really is only a bool primitive and
not a class?)


What do you mean here? There's just as much cause to have Boolean as a
type as there is to have Int32. Note that "bool" is just a C# shorthand
for "System.Boolean".


OK, I wasn't aware of that.

Thanks,
Peter
Nov 25 '05 #3
"Jon Skeet [C# MVP]" <sk***@pobox.com> skrev i en meddelelse
news:11**********************@g49g2000cwa.googlegr oups.com...

For 1.1, there's a NullableTypes project on SourceForge:
http://nullabletypes.sourceforge.net/


Thanks, I'll look at that.


Looks good - I'll use it. Undoubtedly 100 times better and more
comprehensive than anything I could make myself!
Nov 25 '05 #4
Peter Kirk <pk@alpha-solutions.dk> wrote:
Is there anyone else who finds this annoying?


Not very often, to be honest. Then again, I don't do much data binding
etc.


Yes, it is exactly here I find difficulties - especially with Boolean for
which it is difficult to define a "null value" - there are only two valid
values as it is. With Int32 or DateTime one can of course define some value
as null (like the minimum or maximum allowable value for the respective
type).


Indeed.
Is the only way around this to wrap these types in your own class?


Not quite. You could wrap them in your own structs which have an extra
field specifying whether they're "null" or not - that would make them
keep value semantics.


I'm not 100% following you here. Do you mean define a "MyInt32" struct which
contains an Int32, and a bool field for "IsNull" - which I then need to
check when I use MyInt32 (and ensure I set it correctly as well when I set
the value of the Int32)?


Yes - although you could use conversion operators (explicit or
implicit) to help there.
For 1.1, there's a NullableTypes project on SourceForge:
http://nullabletypes.sourceforge.net/


Thanks, I'll look at that.


Sounds like that's your best answer, given your other post.
In C#/.NET 2.0, there's the concept of Nullable<T> built into the
language:
int? x = null;


Yes, I did see this but I am not using .NET 2.


Right. It wasn't clear from your post.
(Why have Boolean actually, when it really is only a bool primitive and
not a class?)


What do you mean here? There's just as much cause to have Boolean as a
type as there is to have Int32. Note that "bool" is just a C# shorthand
for "System.Boolean".


OK, I wasn't aware of that.


The same is true for all "built-in" types - all the "C#" types are
really just shorthands for CLR types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 25 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Metro Sauper | last post by:
Why is the default text representation for booleans in C# 'True' and 'False' whereas in xml it is 'true' and 'false'? It seems like it would make sense to have them both the same. Metro T....
3
by: Daves | last post by:
trying to get a boolean value from DataReader (that is, result from a SQL query); boolean MyBool = MyDataReader.GetBoolean(3); well it works using that number 3 but of course I would like to...
2
by: Raj | last post by:
Hi, When we are sorting the DataGrid Boolean column the grid is becoming redcross. I have my own PPMIPDataGridBoolColumn class inherited from System.Windows.Forms.DataGridBoolColumn. In this...
12
by: Joel Whitehouse | last post by:
Hello All, How can I cast a System.Int32 as a boolean in VB.NET? I know I used to used Cbool(myInt) in VB6, but I cxan't seem to find the .NET equivalent. -Joel
2
by: HL | last post by:
I am using VS2005 Beta2 In the VS.NET IDE, the Intellisense drop-down shows both bool and Boolean. What is the difference between the two? Similarly, it shows string and String ('s'...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
43
by: Shehab Kamal | last post by:
Which approach is better: 1) Use a method that returns true/false (the method will log the exception that lead to false using log4net for example) OR 2) Use a method that returns void and throws...
2
by: shapper | last post by:
Hello, When using C# should I use bool or Boolean, string or String, ... do define a variable? I am used to VB but for a few months I have been using C# instead ... Thanks, Miguel
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.