Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 6th, 2007, 1:38 AM   #1
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Need a little clarification

Look at this line of code:

Console.WriteLine("9 > 7 is " + (9 > 7));

All it does is print "9 > 7 is True. The parentheses are needed because without them the compiler says this to me:

Operator '>' cannot be applied to operands of type string and int.

According to Herb, he says the parentheses are needed because the + operator has a higher precedence than the > operator.

I'm trying to understand the compilers message. I understand what Herb is saying. By putting parentheses around the (9 > 7) you force the compiler to evaluate those right away. I guess I don't quite understand what the compiler's message is though.

When I omit the parentheses, what does the compiler "think" I'm trying to do?
357mag is offline   Reply With Quote
Old Aug 6th, 2007, 1:55 AM   #2
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
It thinks you are trying to write something to the console. Hence when you type Console.WriteLine. Your problem is that WriteLine only takes 1 parameter (as far as I know anyways). The first parameter anyways is a string. The error you're getting is telling you that you can't compare a string and an int using the '>' operator. You can put anything you want between the brackets for the parameter, but in the end, for it to work, it has to be a valid string. The reason it works with the code you provided is that, you had text and applied the + operator, meaning to add on to the string in this case, and you provided (9 > 7). It evaluates the equation in parentheses and connects it to the string.

To tell you the truth I'm a little surprised it works with the code you provided. I would have thought that when it evaluated (9 > 7), it would return true, but would throw you a similar error telling you you can't convert a bool to string.
Booooze is offline   Reply With Quote
Old Aug 6th, 2007, 2:34 AM   #3
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
So without the parentheses is the compiler kind of seeing this whole thing as a string:

9 > 7 is + 9

I see the + 9 part as concatenating an integer onto the end of a string, but maybe the compiler sees it as concatenating a string(the character 9)onto the string "9 > 7".
357mag is offline   Reply With Quote
Old Aug 6th, 2007, 8:24 AM   #4
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,038
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by 357mag
 Console.WriteLine("9 > 7 is " + (9 > 7));
All it does is print "9 > 7 is True. The parentheses are needed because without them the compiler says this to me:

Operator '>' cannot be applied to operands of type string and int.

According to Herb, he says the parentheses are needed because the + operator has a higher precedence than the > operator.
Herb is not exactly wrong here- that is part of the problem- but he misses out the rest of the reason. In C++ and C#, you can do what is called 'operator overloading'. This just means you write a custom method that is invoked for the operator you replace. For example, in C++, the bit-shift operators, << and >>, are overloaded to provide output and input when applied to certain objects. Likewise, + is overloaded for the string class in C# to provide concatenation. However, the > operator is not. Because of this, it tries to do a regular comparison, which it cannot do (the types don't match), hence the error.
Quote:
Originally Posted by Booooze
To tell you the truth I'm a little surprised it works with the code you provided. I would have thought that when it evaluated (9 > 7), it would return true, but would throw you a similar error telling you you can't convert a bool to string.
The + operator is overloaded for all primitive types for the string class. Internally, it probably just 'boxes' the value, then calls the ToString() method of new boxed object. For object types, it just alls the ToString() method directly (one more good reason to have your own implementation of this method in your classes).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Aug 6th, 2007, 1:33 PM   #5
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Aha, that explains it. Thanks.
Booooze is offline   Reply With Quote
Old Aug 6th, 2007, 2:49 PM   #6
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
So without the parentheses around the (9 > 7) part, the compiler attempts to compare the string "9 > 7 is" with the > 7 or the 9 > 7 part? And because you can't compare a string to an int the compiler says "error".
357mag is offline   Reply With Quote
Old Aug 6th, 2007, 3:17 PM   #7
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Order of operations
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Aug 6th, 2007, 8:30 PM   #8
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
"9 > 7 is " + 9 > 7

is the same as

("9 > 7 is " + 9) > 7

if that makes it more clear.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Aug 7th, 2007, 3:36 PM   #9
357mag
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 148
Rep Power: 4 357mag is on a distinguished road
Yes that helps. I kind of suspected that.
357mag is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Clarification Nested Lists flebber Python 3 Aug 31st, 2006 10:24 AM
Clarification, please. sparda C 7 Jul 13th, 2005 10:24 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC