473,322 Members | 1,401 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,322 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 4825
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.