473,379 Members | 2,132 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,379 software developers and data experts.

Cleanest syntax to logically AND multiple nullable boolean flags i

Given several nullable boolean flags;

bool? l_flg_01 = true;
bool? l_flg_02 = false;
bool? l_flg_03 = true;
bool? l_result_flg = null;

I would have liked one of these syntax formats to work;

// if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
// if ( l_flg_01 & l_flg_02 & l_flg_03 ) // Line B
// if ( l_flg_01 &&? l_flg_02 &&? l_flg_03 ) // Line C
// if ( l_flg_01 &? l_flg_02 &? l_flg_03 ) // Line D
// if ( l_flg_01 AND l_flg_02 AND l_flg_03 ) // Line E

But I have to settle for casting (which looks messy) as below;

if ( (bool) l_flg_01 && (bool) l_flg_02 && (bool) l_flg_03 )
{
l_result_flg = true;
}
else
{
l_result_flg = false;
}

I thought 'lifted operators' would have worked for Line A or B.

My desired result, logically, is a null in any flag produces a false result.
My desired result, aesthetically, is a clean code line with no casting
baggage.

Do I have to write a custom operator for C# 2.0 nullable booleans,
(to get clean looking code) or am I missing something?

Thanks : shawnk

PS. I'm not worked about the logic, I would like to know about the aesthetics.
Feb 16 '06 #1
8 4828
How are you declaring the flags (what's their type)?
"shawnk" <sh****@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
Given several nullable boolean flags;

bool? l_flg_01 = true;
bool? l_flg_02 = false;
bool? l_flg_03 = true;
bool? l_result_flg = null;

I would have liked one of these syntax formats to work;

// if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
// if ( l_flg_01 & l_flg_02 & l_flg_03 ) // Line B
// if ( l_flg_01 &&? l_flg_02 &&? l_flg_03 ) // Line C
// if ( l_flg_01 &? l_flg_02 &? l_flg_03 ) // Line D
// if ( l_flg_01 AND l_flg_02 AND l_flg_03 ) // Line E

But I have to settle for casting (which looks messy) as below;

if ( (bool) l_flg_01 && (bool) l_flg_02 && (bool) l_flg_03 )
{
l_result_flg = true;
}
else
{
l_result_flg = false;
}

I thought 'lifted operators' would have worked for Line A or B.

My desired result, logically, is a null in any flag produces a false
result.
My desired result, aesthetically, is a clean code line with no casting
baggage.

Do I have to write a custom operator for C# 2.0 nullable booleans,
(to get clean looking code) or am I missing something?

Thanks : shawnk

PS. I'm not worked about the logic, I would like to know about the
aesthetics.

Feb 16 '06 #2
Just a 'regular' nullable boolean as in;

bool? my_nullable_boolean_flg = null;

"Ravi Ambros Wallau" wrote:
How are you declaring the flags (what's their type)?
"shawnk" <sh****@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
Given several nullable boolean flags;

bool? l_flg_01 = true;
bool? l_flg_02 = false;
bool? l_flg_03 = true;
bool? l_result_flg = null;

I would have liked one of these syntax formats to work;

// if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
// if ( l_flg_01 & l_flg_02 & l_flg_03 ) // Line B
// if ( l_flg_01 &&? l_flg_02 &&? l_flg_03 ) // Line C
// if ( l_flg_01 &? l_flg_02 &? l_flg_03 ) // Line D
// if ( l_flg_01 AND l_flg_02 AND l_flg_03 ) // Line E

But I have to settle for casting (which looks messy) as below;

if ( (bool) l_flg_01 && (bool) l_flg_02 && (bool) l_flg_03 )
{
l_result_flg = true;
}
else
{
l_result_flg = false;
}

I thought 'lifted operators' would have worked for Line A or B.

My desired result, logically, is a null in any flag produces a false
result.
My desired result, aesthetically, is a clean code line with no casting
baggage.

Do I have to write a custom operator for C# 2.0 nullable booleans,
(to get clean looking code) or am I missing something?

Thanks : shawnk

PS. I'm not worked about the logic, I would like to know about the
aesthetics.


Feb 16 '06 #3
Since these are booleans, and you are defaulting a null to false (If I
am understanding this correctly) can you use a
System.Collections.BitArray instead?

John


shawnk wrote:
Just a 'regular' nullable boolean as in;

bool? my_nullable_boolean_flg = null;

"Ravi Ambros Wallau" wrote:
How are you declaring the flags (what's their type)?
"shawnk" <sh****@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
Given several nullable boolean flags;

bool? l_flg_01 = true;
bool? l_flg_02 = false;
bool? l_flg_03 = true;
bool? l_result_flg = null;

I would have liked one of these syntax formats to work;

// if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
// if ( l_flg_01 & l_flg_02 & l_flg_03 ) // Line B
// if ( l_flg_01 &&? l_flg_02 &&? l_flg_03 ) // Line C
// if ( l_flg_01 &? l_flg_02 &? l_flg_03 ) // Line D
// if ( l_flg_01 AND l_flg_02 AND l_flg_03 ) // Line E

But I have to settle for casting (which looks messy) as below;

if ( (bool) l_flg_01 && (bool) l_flg_02 && (bool) l_flg_03 )
{
l_result_flg = true;
}
else
{
l_result_flg = false;
}

I thought 'lifted operators' would have worked for Line A or B.

My desired result, logically, is a null in any flag produces a false
result.
My desired result, aesthetically, is a clean code line with no casting
baggage.

Do I have to write a custom operator for C# 2.0 nullable booleans,
(to get clean looking code) or am I missing something?

Thanks : shawnk

PS. I'm not worked about the logic, I would like to know about the
aesthetics.


Feb 16 '06 #4
Yes. However I wanted to get a feel for the use of nullable types in the
following general programming context.

You have a complex boolean state space whose cardinality is around a dozen
or so states. If the cardinality was around 32 to 64 I would move towards
bitmaps.

For cardinality of one order of magnitude (1 to 10) I would like to use
nullable boolean flags.

On the logic, defaulting Nulls to false is just and example for this post. I
can live with the default logical combinations from MS C# 2.0.

I'm just surprised that I can't string the nullable Bools together 'right
off the bat'.

Thats' really what I would like to know. Cant' we (us C# programmers) just
string the nullable bools together with & or && or something else?

"John Murray" wrote:
Since these are booleans, and you are defaulting a null to false (If I
am understanding this correctly) can you use a
System.Collections.BitArray instead?

John


shawnk wrote:
Just a 'regular' nullable boolean as in;

bool? my_nullable_boolean_flg = null;

"Ravi Ambros Wallau" wrote:
How are you declaring the flags (what's their type)?
"shawnk" <sh****@discussions.microsoft.com> wrote in message
news:39**********************************@microsof t.com...
Given several nullable boolean flags;

bool? l_flg_01 = true;
bool? l_flg_02 = false;
bool? l_flg_03 = true;
bool? l_result_flg = null;

I would have liked one of these syntax formats to work;

// if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
// if ( l_flg_01 & l_flg_02 & l_flg_03 ) // Line B
// if ( l_flg_01 &&? l_flg_02 &&? l_flg_03 ) // Line C
// if ( l_flg_01 &? l_flg_02 &? l_flg_03 ) // Line D
// if ( l_flg_01 AND l_flg_02 AND l_flg_03 ) // Line E

But I have to settle for casting (which looks messy) as below;

if ( (bool) l_flg_01 && (bool) l_flg_02 && (bool) l_flg_03 )
{
l_result_flg = true;
}
else
{
l_result_flg = false;
}

I thought 'lifted operators' would have worked for Line A or B.

My desired result, logically, is a null in any flag produces a false
result.
My desired result, aesthetically, is a clean code line with no casting
baggage.

Do I have to write a custom operator for C# 2.0 nullable booleans,
(to get clean looking code) or am I missing something?

Thanks : shawnk

PS. I'm not worked about the logic, I would like to know about the
aesthetics.

Feb 16 '06 #5
This information is from the docs, perhaps it will help:

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. For example, this code fails to compile with Compiler
Error CS0266:

bool? b = null;
if (b) // Error CS0266.
{
}

This is not allows because it is unclear what null means in the context
of a conditional. Nullable Booleans can be cast to a bool explicitly in
order to be used in a conditional, but if the object has a value if
null, InvalidOperationException will be thrown. It is therefore
important to check the HasValue property before casting to bool.

Feb 17 '06 #6
Excellent point and I am aware of exceptions thrown when NULL is accessed
(via the variables). I'm OK with that and can catch the exceptions (during
testing, etc).

The point is that C# 2.0 Nullable types was not designed 'clean' as a
general nullable 'flag' mechanism (My opinion of course).

Many programmers have 'rolled' their own nullable flags such as with Enums
(C++, C#). None are as general purpose as the current C# 2.0 implementation
(Kudos to the MS C# dev team), however they fell short on the utility of
nullable bool flags when you can't just say (Flg1 & Flg2 & Flg3) in an
expression. I've seen many good approaches from coders better than I and the
MS delivery falls short in its utility for nullable boolean logic (I still
like nullable types in C# but think it could have been done MUCH better). The
'MUCH' being reflective of the most important and utilitarian usage - a high
cardinality boolean space where you work in a small subset of that space in
any code block. This drops a lot of masking concerns with bitmapped
approaches.

So I"ll just put in a feature request to the C# compiler (VS Studio) to
change the logical combination syntax to one that (I think) is more
reflective of my own (limited and opinionated :-) experience with nullable
boolean logic.

Finally the 'lifted operators' should have covered the above behaviour (Flg1
& Flg2 & Flg3) as a compilable statement with runtime exceptions on null
values.

Since I am getting compile errors I figure that I'm missing something or
nullable types fell prey to the VS 2005 delivery schedule.

"Chris Dunaway" wrote:
This information is from the docs, perhaps it will help:

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. For example, this code fails to compile with Compiler
Error CS0266:

bool? b = null;
if (b) // Error CS0266.
{
}

This is not allows because it is unclear what null means in the context
of a conditional. Nullable Booleans can be cast to a bool explicitly in
order to be used in a conditional, but if the object has a value if
null, InvalidOperationException will be thrown. It is therefore
important to check the HasValue property before casting to bool.

Feb 17 '06 #7
shawnk wrote:

<snip>
Finally the 'lifted operators' should have covered the above behaviour (Flg1
& Flg2 & Flg3) as a compilable statement with runtime exceptions on null
values.

Since I am getting compile errors I figure that I'm missing something or
nullable types fell prey to the VS 2005 delivery schedule.


There is no lifted operator for && or || in the spec, but the &
operator compiles fine - it just works in a different way to what you
expect. (true & null) results in null, not false. (false & null results
in false though.) This is consistent with what I'd personally expect.

If you frequently need this, why not write a method like this:

static bool AllTrue (params bool?[] values)
{
foreach (bool? value in values)
{
if (!(value ?? false))
{
return false;
}
}
return true;
}

Jon

Feb 17 '06 #8
I tryed Jon's solution and it worked well. Thank you for an excellent solution.

I still will try to write a custom operator but the best solution is for me
to put in feature request for C# 3 so that we (programmers) can work with a
subset of complex boolean state space with a fairly clean expression.

This way we don't have to worry about bitmaps (the rest of state space) or
non-native (inherent in language spec) solutions.

Thanks Jon :-)

"Jon Skeet [C# MVP]" wrote:
shawnk wrote:

<snip>
Finally the 'lifted operators' should have covered the above behaviour (Flg1
& Flg2 & Flg3) as a compilable statement with runtime exceptions on null
values.

Since I am getting compile errors I figure that I'm missing something or
nullable types fell prey to the VS 2005 delivery schedule.


There is no lifted operator for && or || in the spec, but the &
operator compiles fine - it just works in a different way to what you
expect. (true & null) results in null, not false. (false & null results
in false though.) This is consistent with what I'd personally expect.

If you frequently need this, why not write a method like this:

static bool AllTrue (params bool?[] values)
{
foreach (bool? value in values)
{
if (!(value ?? false))
{
return false;
}
}
return true;
}

Jon

Feb 21 '06 #9

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

Similar topics

10
by: Ramprasad A Padmanabhan | last post by:
Hello all, On my linux box ( redhat 7.2 ), I have been using char as a boolean data type. In order to save on the number of bytes as compared to using int or short int. typedef char boolean;...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
15
by: scparker | last post by:
I have yet to find a satisfactory solution to this problem. It involves VB.NET 2.0 and datetime issues. I have a form that asks for a Date to be submitted in dd/mm/yyyy format. When this is...
20
by: W Karas | last post by:
Would the fear factor for concepts be slightly reduced if, instead of: concept C<typename T> { typename T::S; int T::mem(); int nonmem(); };
6
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have a generic function I am using to check constraints, an example of which is the following: Public Function ValidateMinValue(Of t As IComparable)(ByVal PropertyName As String, ByVal value...
2
Cathode Follower
by: Cathode Follower | last post by:
In VB6 we put date values obtained from databases into variants. We can then use statements like IsNull(.EndDate) to check whether a date is set or not. When you put code like this through the vb.net...
6
by: Tony Johansson | last post by:
Hello! I'm reading in a book called Visual C# 2005. In the chapter about Generics there ia a section about Nullable types. Here is the text that isn't complete true I think. It says: "You...
1
by: Peter Morris | last post by:
It's a C# shortcut for System.Nullable<Point>
6
by: nzkks | last post by:
Hi I am using these: ASP.Net 2.0 with VB.Net, Visual Studio 2005, SQL Server 2005 I suspect, there is something missing in BLL class. I created the ASP.Net form also and checked whether it is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.