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

Ternary Conditional Operators and Method Calls

The following code will not compile...

public bool active = false;
public void Activate(){}
public void Deactivate(){}
public void object_Activate(object sender, EventArgs e){
(active)? Deactivate() : Activate();
}

the compiler gives the following error...

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

Now the two statements here are both Calls? In which case this should
compile according to the given message?

If I change the code to this....

public bool active = false;
public bool Activate(){
return true;
}
public bool Deactivate(){
return false;
}
public void object_Activate(object sender, EventArgs e){
active = (active)? Deactivate() : Activate();
}

It compiles as I am now doing an assignment.

Is this an intentional feature of the C# compiler? And if so what is the
reasoning behind it? It doesn't matter hugely as it is easy enough to work
around.

Cheers

Rob
Nov 16 '05 #1
2 4360
Rob Williams <Ro*********@discussions.microsoft.com> wrote:
The following code will not compile...

public bool active = false;
public void Activate(){}
public void Deactivate(){}
public void object_Activate(object sender, EventArgs e){
(active)? Deactivate() : Activate();
}
Indeed.
the compiler gives the following error...

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

Now the two statements here are both Calls? In which case this should
compile according to the given message?
No, because the overall statement is not a call. Each part of it is,
but it isn't in itself.

You can't write

Deactivate() && Activate();

either.
If I change the code to this....

public bool active = false;
public bool Activate(){
return true;
}
public bool Deactivate(){
return false;
}
public void object_Activate(object sender, EventArgs e){
active = (active)? Deactivate() : Activate();
}

It compiles as I am now doing an assignment.

Is this an intentional feature of the C# compiler? And if so what is
the reasoning behind it? It doesn't matter hugely as it is easy
enough to work around.


The idea of the ternary conditional operator isn't to allow code like
yours - it's to allow things like parameter passing and assignment.

A far more idiomatic (and IMO easier to read) way of writing your code
would be:

if (active)
{
Deactivate();
}
else
{
Activate();
}

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

I believe its intentional. But they could have added another error message
specific to this though.

IMHO ?: is not a complete replacement for if-else blocks. It is designed for
use with statements whose conditional section returns a value, which is
being used somehow, not as a method invocation source.

Your first example statement: (active)? Deactivate() : Activate();...
happens to invoke methods depending upon 'active''s value, which should
actually be represented by an if-else block.

Rakesh

"Rob Williams" wrote:
The following code will not compile...

public bool active = false;
public void Activate(){}
public void Deactivate(){}
public void object_Activate(object sender, EventArgs e){
(active)? Deactivate() : Activate();
}

the compiler gives the following error...

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

Now the two statements here are both Calls? In which case this should
compile according to the given message?

If I change the code to this....

public bool active = false;
public bool Activate(){
return true;
}
public bool Deactivate(){
return false;
}
public void object_Activate(object sender, EventArgs e){
active = (active)? Deactivate() : Activate();
}

It compiles as I am now doing an assignment.

Is this an intentional feature of the C# compiler? And if so what is the
reasoning behind it? It doesn't matter hugely as it is easy enough to work
around.

Cheers

Rob

Nov 16 '05 #3

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

Similar topics

3
by: Paul E Johnson | last post by:
Dear friends in C: I'm completely baffled by this problem I have with printf statements and conditional operators in C. I'm using gcc-3.3. I have a project where the rarely used Union type...
2
by: Paul E Johnson | last post by:
Dear friends in C: I'm completely baffled by this problem I have with printf statements and conditional operators in C. I'm using gcc-3.3. I have a project where the rarely used Union type...
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
10
by: John Smith | last post by:
After reading C# documentation the Conditional attribute seemed the way to go, but after inspecting the IL it seems those methods are still there and I imagine the CLR removes them. Using #if DEBUG...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
5
by: slashdotcommacolon | last post by:
Hello, I've been asked to port some old code to a new platform. We have a vendor-supplied compiler that is supposed to be ansi compliant, but it refuses to compile the following code: $ cat...
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
8
by: Bob Hoeppner | last post by:
In the following code //add to bool or double Dictionary this.m_unit.Add((unittype == "b")? unitnum:(double)unitnum); The bool dictionary uses an integer index, the double uses a double...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.