|
The point isn't that the compiler gives a more useful message (as has been pointed out), the point is it will give an error message even if the type in question in fact has a conversion-to-bool operator!
Whether or not you cannot figure out what the actual cause of the resulting message is, is an entirely different question. I'd suppose everyone intelligent enough to put literals on the left hand side of a comparison will recognise what it means when the compiler states he wants an lvalue! At least I am
|
|
|
|
|
I agree, but it seems this construct is no longer 'in vogue'. Being a sheep I'll just follow the herd.
It's time for a new signature.
|
|
|
|
|
Hmm, has it ever been 'en vogue'? Judging by my experience, I believe the reason why 'nobody' uses it is that few people know about it, not that it isn't 'en vogue' any more.
Also, it seems more natural to ask 'does x currently have a value of 2?' than 'does 2 happen to be the value currently stored in x?'. So even when I tell people about it, they might be reluctant to change their style accordingly.
|
|
|
|
|
Stefan63 wrote: has it ever been 'en vogue'?
Possibly not, but it was certainly common practice at my last company, which had a fairly large group of developers around the world. I've never actually tried to see if it's in any of the published books on C++ (or C).
It's time for a new signature.
|
|
|
|
|
There's a difference between
if (btnPause.Content = "Pause")
and
if (btnPause.Content == "Pause")
You aren't comparing with btnPause.Content, but assigning to it. The assignment operator returns the assigned object, so in effect your code is like this:
btnPause.Content = "Pause";
if (btnPause)
It's a classic typo-bug, and the safest way of avoiding it is to make a habit of putting the constant (if you have one) to the left:
if ("Pause" = btnPause.Content)
This would give a compiler error along the lines of "Cannot assign to constant", which is more helpful.
|
|
|
|
|
I already suggested this above; you may want to read Harold, Luc and Keith's comments in response. Also note that someone around here really hates this idea and is downvoting us for suggesting it. I've given you a 5 to compensate.
It's time for a new signature.
|
|
|
|
|
Yes, I saw that once I'd replied. It was the changed subject line that threw me off.
Sorry, I'm new in the neighbourhood.
|
|
|
|
|
Cool Cow Orjan wrote: Sorry, I'm new in the neighbourhood.
No problem, we all make mistakes from time to time. And welcome to CodeProject, judging by some of your posts I have seen I am sure your contributions will have a positive impact for many people.
It's time for a new signature.
|
|
|
|
|
Ok, first things first...If this is August 10th, I will gladly eat this post. Why are the messages all dated the 10th? Secondly, being "new in the neighbourhood" sounds like a good reason for everyone to pile on and really "get you" for not plowing through all of the messages!! LOL!
|
|
|
|
|
The messages are dated [date] [month] [year], so they're all posted in 2010, or '10 for short. And it's August 10th now...
As for piling on, I'd expect to be shouted at for factual errors, or bad advice (like saying "Use Emacs" but not so much for being a newbie. And should that happen, well, I've been online long enough to grow a reasonably thick skin.
|
|
|
|
|
Personally, I'd prefer to hide the Pause button and show the resume button rather than repurpose the one button and do string compares.
|
|
|
|
|
Personally when performing value comparisons with strings (and many other types) I find I am far less prone to syntax errors if I make myself use the "Equals" method. With strings in particular, it is a good practice to use the static method
String.Equals(string a, string b) as this eliminates the possibility of a null reference exception.
With this in mind, the boolean in your if statement would look like this:
if (String.Equals(btnPause.Content, "Pause"))
"We are men of action; lies do not become us."
|
|
|
|
|
You can't get a NRE using the == operator. The .Net runtime essentially does this:
if (Object.ReferenceEquals(a, null) && Object.ReferenceEquals(b, null)) return true;
else if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(b, null)) return false;
else return a.Equals(b);
Unless you override the == operator (in which case you should do this in the header).
The String.Equals is better because you would be more inclined to use the StringComparison enum[^] - but that is only the case for Strings.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb]
Jonathan C Dickinson (C# Software Engineer)
|
|
|
|
|
Jonathan,
I can see why my language may have been confusing. I was not meaning to imply that using the == operator would result in a NullReferenceException. My first statement was that the "Equals" method should be used to make the comparison, and I then went on to state that the static method should be preferred for strings (rather than the instance method) since using the instance method could result in a NullReferenceException while using the static method will not. Looking back at my post, however, I can see why one might think I was implying something different, which was not my intent. Regardless, thanks.
"We are men of action; lies do not become us."
|
|
|
|
|
you are trying to assign "Pause" to btnPause.content (=) you need the equality comparison operator (==)
I dont have a lot of context to what you are doing, but you might want to try refering to sender rather than btnPause in the code.
|
|
|
|
|
Ah, bitten by the old "==" typo. Don't feel bad it was my first coding mistake in C 25 years ago. I still occasionally make that error but it usually takes me a whole lot less time to find it.
|
|
|
|
|
error in line "if (btnPause.Content = "Pause") "
it should be like btnpauser.content == "Pause"
hopefully this should fix the error
|
|
|
|
|
Silly mistake, but hey CodeProject a bit of downer to place this question on the daily news email.
or maybe someone is having a bad day and wants to pass it on.....
|
|
|
|
|
hey all,
i've some textbox, which shows some words, the user should retype those words.
each time when the user types correct char, this specific char should be repainted to red color (at the specific word within the text box).
in richTextBox there is a property called .SelectionColor
is there any way to achieve this action by using a regular textbox ?
the reason i don't use richTextBox, due to the fact, it impossible to align text (at least i didn't find any property for that) in richTextBox...
thanks for help
|
|
|
|
|
No, the content of a TextBox (or most other Controls) is single font, single size, single style, single color.
igalep132 wrote: impossible to align text
?? not sure what you mean. Are you suggesting there is something a TextBox does and a RichTextBox doesn't? That would be news to me.
|
|
|
|
|
i didn't find any property for text alignment...
do you know any ?
|
|
|
|
|
In MSDN I see this[^], I have never used it though.
|
|
|
|
|
Hi,
How to make identity information in webbrowser control windows application.
eg: "*"http://google.com
* => logo of the particular url(website)
How to display the particular logo of the url in web-browser control. c#.net
Thanks
krishna
|
|
|
|
|
By default, the "logo" is nothing but the favicon.ico file located at the website's root. A meta tag can also be used to point to an alternative site icon.
/ravi
|
|
|
|
|
I want to create a windows application in C#.net that
will be used to make a call on given number.
THis application will use modem that is connected to my telephone.
Can we do this application?
As I have seen many api's to dial phone number from .net application
but don't know how to use them.
|
|
|
|