trinary statement

T

tshad

I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or just
leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?

Thanks,

Tom
 
J

Joe Cool

I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true.  If so set the checkbox, otherwise set to false or just
leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?

chkReviewed.Checked = ((drv["Reviewed"] != DBNull.Value && (bool)drv
["Reviewed"]) ? true : false);
 
M

Martin Honnen

tshad said:
I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or just
leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?

What you have is an expression, not a statement.
Why don't you simply use an if/else statement:

if (drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"])
{
chkReviewed.Checked = true
}
else
{
chkReviewed.Checked = false
}

On the other hand that could be shortened to

chkReviewed.Checked = drv["Reviewed"] != DBNull.Value &&
(bool)drv["Reviewed"];
 
T

tshad

That was it.

Thanks,

Tom

I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or just
leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?

chkReviewed.Checked = ((drv["Reviewed"] != DBNull.Value && (bool)drv
["Reviewed"]) ? true : false);
 
T

tshad

Martin Honnen said:
tshad said:
I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or
just leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?

What you have is an expression, not a statement.
Why don't you simply use an if/else statement:

I could have, but I like using the trinary statement.
if (drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"])
{
chkReviewed.Checked = true
}
else
{
chkReviewed.Checked = false
}

On the other hand that could be shortened to

chkReviewed.Checked = drv["Reviewed"] != DBNull.Value &&
(bool)drv["Reviewed"];

That works.

Thanks,

Tom
 
B

Ben Voigt [C++ MVP]

tshad said:
I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or just
leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?

Not really better than the other answers, but I think this expresses the
intent better:

chkReviewed.Checked = (drv["Reviewed"] == DBNull.Value)? false:
(bool)drv["Reviewed"];
 
T

tshad

Ben Voigt said:
tshad said:
I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or
just leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?

Not really better than the other answers, but I think this expresses the
intent better:

chkReviewed.Checked = (drv["Reviewed"] == DBNull.Value)? false:
(bool)drv["Reviewed"];
I agree.

They all work but this does show what I am trying to do clearly. If null:
false, else whatever the boolean is.

Thanks,

Tom
 
G

Göran Andersson

Joe said:
I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or just
leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?

chkReviewed.Checked = ((drv["Reviewed"] != DBNull.Value && (bool)drv
["Reviewed"]) ? true : false);

Which of course is the same as:

chkReviewed.Checked = drv["Reviewed"] != DBNull.Value &&
(bool)drv["Reviewed"];
 
J

Joe Cool

Joe said:
I am trying to do a trinary statement to set a checkbox.
I need to check to see if a value in the DataView (which is a boolean)is
not null and true.  If so set the checkbox, otherwise set to false or just
leave as is (which would be unchecked).
I tried:
(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;
But get an error:
Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement
How do I phrase this to get it to work?
chkReviewed.Checked = ((drv["Reviewed"] != DBNull.Value && (bool)drv
["Reviewed"]) ? true : false);

Which of course is the same as:

chkReviewed.Checked = drv["Reviewed"] != DBNull.Value &&
(bool)drv["Reviewed"];

Agreed. All I was doing was correcting his syntax, not trying to
optimize it.
 
D

David Anton

Ok - I'll be a nitpicker and point out that the most-often used terms for
this are:
"ternary" operator, or "conditional" operator, or "ternary conditional"
operator.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
P

Peter Duniho

Ok - I'll be a nitpicker and point out that the most-often used terms for
this are:
"ternary" operator, or "conditional" operator, or "ternary conditional"
operator.

Sure, but...

From http://dictionary.reference.com/browse/trinary

tri⋅na⋅ry  [trahy-nuh-ree]

–adjective
consisting of three parts, or proceeding by three; ternary.

Is it really that bad for someone to simply use a synonym instead of the
more common term?

Pete
 
R

Rick Lones

tshad said:
Martin Honnen said:
tshad said:
I am trying to do a trinary statement to set a checkbox.

I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or
just leave as is (which would be unchecked).

I tried:

(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;

But get an error:

Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

How do I phrase this to get it to work?
What you have is an expression, not a statement.
Why don't you simply use an if/else statement:

I could have, but I like using the trinary statement.

Ah, well in that case you may prefer:

chkReviewed.Checked = drv["Reviewed"] == DBNull.Value ? false :
(bool)drv["Reviewed"] ? true : false;

And it's certainly possible to obfuscate it further if one's heart were really
in it.
On the other hand that could be shortened to

chkReviewed.Checked = drv["Reviewed"] != DBNull.Value &&
(bool)drv["Reviewed"];

Personally, I like this one from Martin and Goran. Readable and straight to the
point.

-rick-
 
D

David Anton

No - of course not.
It's just useful to use the common terms if you know them. For example, if
someone asked a question about 'categories', it might be confusing until you
knew that they were talking about 'classes' or 'types'.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB


Peter Duniho said:
Ok - I'll be a nitpicker and point out that the most-often used terms for
this are:
"ternary" operator, or "conditional" operator, or "ternary conditional"
operator.

Sure, but...

From http://dictionary.reference.com/browse/trinary

tri⋅na⋅ry  [trahy-nuh-ree]

–adjective
consisting of three parts, or proceeding by three; ternary.

Is it really that bad for someone to simply use a synonym instead of the
more common term?

Pete
 
G

Göran Andersson

Joe said:
Joe said:
I am trying to do a trinary statement to set a checkbox.
I need to check to see if a value in the DataView (which is a boolean) is
not null and true. If so set the checkbox, otherwise set to false or just
leave as is (which would be unchecked).
I tried:
(drv["Reviewed"] != DBNull.Value && (bool)drv["Reviewed"]) ?
chkReviewed.Checked = true: chkReviewed.Checked = false;
But get an error:
Error 27 Only assignment, call, increment, decrement, and new object
expressions can be used as a statement
How do I phrase this to get it to work?
chkReviewed.Checked = ((drv["Reviewed"] != DBNull.Value && (bool)drv
["Reviewed"]) ? true : false);
Which of course is the same as:

chkReviewed.Checked = drv["Reviewed"] != DBNull.Value &&
(bool)drv["Reviewed"];

Agreed. All I was doing was correcting his syntax, not trying to
optimize it.

Well, removing the "bool?true:false" anti-pattern is more of a
correction... :)
 
B

Ben Voigt [C++ MVP]

David is correct that it is an operator, not a statement, and this
difference is directly relevant to the trouble the OP was having.

C++ allows you to use any expression, including the result of the ternary
(or trinary) operator, as a statement. C# does not.

Peter Duniho said:
Ok - I'll be a nitpicker and point out that the most-often used terms for
this are:
"ternary" operator, or "conditional" operator, or "ternary conditional"
operator.

Sure, but...

From http://dictionary.reference.com/browse/trinary

tri⋅na⋅ry  [trahy-nuh-ree]

–adjective
consisting of three parts, or proceeding by three; ternary.

Is it really that bad for someone to simply use a synonym instead of the
more common term?

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top