Saturday, March 31, 2012

Set Boolean Property to Nothing

I need to set a public property of type boolean to nothing. Is this possible or will it always revert to false?

Thanks

You use the FCL(framework class library) 2.0 System.Nullable, try the link below for code sample from Microsoft using the TreeView control. Hope this helps.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.treenode.showcheckbox.aspx


I should have mentioned that I'm using 1.1

Good news bad news I found a code with Nullable Boolean but it is readonly. Try the link below for details. Hope this helps.

http://www.codeproject.com/vb/net/ReflectionDataBinding.asp


As far as i know the boolean type is a primitive type and can't be set to nothing/null. If you do set it to nothing/null it's value will be false. If you need more than 2 values (true/false), ie 3 values (null/true/false) then use another type like a smallint (0/1/2) or even an enum with 3 values. Bottom line the boolean type is intended for 2 values only: True or False.

Boolean in ANSI SQL is three valued the third value is null and in FCL(framework class library) 2.0 it is in System.Nullable as three valued, what I posted and the Dotnetnuke used reflction to create a read only property through DBNULL. I am a data person so I know when it was created and released last year.

http://blogs.msdn.com/somasegar/archive/2005/08/11/450640.aspx


Nearl all SQL implementations (including Sql Server and Oralce) don't have a Boolean datatype. A true boolean can only have two values - by definition you cannot have a 'tristate' boolean.

SQL99 has a special BOOLEAN data type with a range that includes only two values: TRUE and FALSE. Oracle, DB2, and Microsoft SQL Server don't have a BOOLEAN data type. (Or, to be more precise, DB2 has it, but for internal use only, i.e., you cannot declare a column of type BOOLEAN.) But the BOOLEAN data type can be easily simulated, for example by using a user-defined data type of type VARCHAR that only allows FALSE and TRUE for its values.source


Microsoft spent 10 billion dollars on only math in SQL Server 2005 the only thing not there is Time Interval and it is coming in a service pack because it was there till beta 2. Try the link below for three valued BIT in 2005. The .NET version is not because it uses a place holder.

http://msdn2.microsoft.com/en-us/library/ms177603.aspx


Caddre:

Boolean in ANSI SQL is three valued the third value is null and in FCL(framework class library) 2.0 it is in System.Nullable as three valued, what I posted and the Dotnetnuke used reflction to create a read only property through DBNULL. I am a data person so I know when it was created and released last year.

http://blogs.msdn.com/somasegar/archive/2005/08/11/450640.aspx

I don't think he is talking data from the DB, i think he is referring to a variable within the class.

in C# (.net 2.0) both of the examples bellow give a compile error:

bool x =null;bool x = System.Nullable;

and in VB.Net (.net 2.0) the example bellow makes the variable be false

dim x as boolean = nothing

Caddre:

Try the link below for three valued BIT in 2005.

http://msdn2.microsoft.com/en-us/library/ms177603.aspx

But it's desribed as "An integer data type that can take a value of 1, 0, or NULL". That isn't a boolean. A true boolean can only have two states.

Caddre:

Try the link below for three valued BIT in 2005.

I'm sorry but that is why they called it bit and not a boolean, because by definition a boolean can have only 2 values: True or False if you add null to the equation then it's not a boolean anymore and it becomes something else, like a bit. A bit can be used torepresent a boolean. But if you have a bit in your DB that accepts null, then that field can not be represented as a boolean on the code (whatever that code might be: Java, C#, VB.net, c++, etc) unless you don't mind null being false.


Connect:


But it's desribed as "An integer data type that can take a value of 1, 0, or NULL". That isn't a boolean. A true boolean can only have two states.

Connect, I agree with you 100%.


jstawski:

Caddre:

Boolean in ANSI SQL is three valued the third value is null and in FCL(framework class library) 2.0 it is in System.Nullable as three valued, what I posted and the Dotnetnuke used reflction to create a read only property through DBNULL. I am a data person so I know when it was created and released last year.

http://blogs.msdn.com/somasegar/archive/2005/08/11/450640.aspx

I don't think he is talking data from the DB, i think he is referring to a variable within the class.

in C# (.net 2.0) both of the examples bellow give a compile error:

bool x =null;bool x = System.Nullable;

and in VB.Net (.net 2.0) the example bellow makes the variable be false

dim x as boolean = nothing

We are talking about the same thing but you are not using the System.Nullable<T> variable.

( The bool? type

The bool? nullable type can contain three different values: true, false and null. As such, the cannot be used in conditionals such as with if, for, or while.)

http://msdn2.microsoft.com/en-us/library/2cf62fcy.aspx

http://www.yoda.arachsys.com/csharp/csharp2/nullable.html


Caddre:

We are talking about the same thing but you are not using the System.Nullable<T> variable.

http://msdn2.microsoft.com/en-us/library/2cf62fcy.aspx

http://www.yoda.arachsys.com/csharp/csharp2/nullable.html

By making it a nullable type it's not a real boolean type anymore. It's not a primitive/value type like boolean is, it's now a typed object.

Source:

For an example of when you might use a nullable type, consider how an ordinary Boolean variable can have two values: true and false. There is no value that signifies "undefined". In many programming applications, most notably database interactions, variables can exist in an undefined state. For example, a field in a database may contain the values true or false, but it may also contain no value at all. Similarly, reference types can be set tonull to indicate that they are not initialized.

By what they wrote above, it tells me they incorporated this on the .net framework to accomadate for a null value from the db, but i think we all agree that a bit is not a truly boolean since it can have 3 values (null, 0, 1) and that is why they created a new type. If you ask me, they should have never called it a nullable boolean because a boolean can't be anything else than True or False, therefore it can't be null. Maybe that's why they didn't call it a boolean in SQL Server.

Bottom line it works for what the initial request was. Too bad he is not using 2.0, though.

Thanks to this I learned something new today and something i really disagree with.



(Bottom line it works for what the initial request was. Too bad he is not using 2.0, though.)

But the code in the link below provides a readonly Boolean property through System.Reflection PropertyInfo.

http://www.codeproject.com/vb/net/ReflectionDataBinding.asp

0 comments:

Post a Comment