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 2 4383
Rob Williams <Ro*********@di scussions.micro soft.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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
is called for! Incredible, but true.
union mixed
|
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
is called for! Incredible, but true.
union mixed
|
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 way
assert.h can be implemented without using it? Are these decisions
related in anyway?
|
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 means the code does not even
reach the IL when compiling in release mode.
Is there any benefit to using the Conditional Attribute? Am I right in
thinking there will small performance overhead using
over #if DEBUG.
|
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.
I can understand that complicated code would be much less readable using nested
operators, but aside from that is there another reason? -- something going on
under the hood perhaps which make it inadvisable to use?
| |
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 suggestions?
|
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 test.c
int main(int argc, char **argv)
{
static int a, b, c, d, e, f, g, h, i, j, k;
return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
|
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, but
the return type doesn't match:
Rectangle? m_textArea = null;
public Rectangle TextArea {
set { m_textArea = value; }
get { return m_textArea; }
|
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
index, which some may take issue with, but that's a different
discussion. The problem is that unitnum is always cast to a double, even
when unittype is "b". That seems like a bug to me.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |