Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 4th, 2003, 01:54 PM
cjo cjo is offline
Authorized User
 
Join Date: Oct 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default conditional (ternary) operator using C#

Friends,

I'm attempting to use the following method in a class. "altitude" is a member of the class with ushort data type.

public void Descend()
{
     altitude >= 5000 ? altitude -= 5000 : altitude = 0;
}

However, I'm getting the following error message specifically referencing the line of code above: Compiler Error Message: CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

Apparently I can't use a conditional operator as a statement, but when I remove the semicolon, I get a different error message telling me that a semicolon is expected.

Is my syntax incorrect or am I using this conditional operator incorrectly?

Thanks in advance for any help you can provide.
 
Old November 4th, 2003, 03:01 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Well, it looks like it's having an issue with "-=". That isn't an assignment, call, increment, decrement, and new object expression.

Peter
----------------------------------------
Work smarter, not harder.
 
Old November 4th, 2003, 03:06 PM
cjo cjo is offline
Authorized User
 
Join Date: Oct 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nevermind. I think I got it:

public void Descend()
{
    altitude = altitude >= 500 ? (ushort)(altitude - 5000) : (ushort)0;
}

This works. If I had you thinking about it ... thanks any.
 
Old January 12th, 2005, 11:55 AM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

would this work? (i tested in java--never used c#--and it works fine, but there might be some nuances.

Code:
// if altitude is more than 5000, decrement it by 5000
// if it is less than 5000, decrement it by its own value
// hence setting it to zero

altitude -= (altitude >= 5000) ? 5000 : altitude;
slightly more terse...oh well.

----
also, i realize this thread is over a year old, but alas...bless google for bringing me to it.






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use like operator thillaiarasu SQL Server 2000 4 April 23rd, 2010 09:47 AM
=== operator in c# surendraparashar C# 2005 8 November 8th, 2007 05:14 AM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
? operator in PHP Tachyon Beginning PHP 2 May 24th, 2004 02:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.