c# 4.0 - if statements in buttons c# -


i'm brand new n00bie in visual c# , ran weird obstacle driving me crazy!! here code in question (yes, hello world program):

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace windowsformsapplication1 {     public partial class form1 : form     {         public form1()         {             initializecomponent();         }          private void button1_click(object sender, eventargs e)         {               if (textbox1.text.equals("goodbye cruel world"))             {                 textbox1.text = ("hello world!");              }             else { textbox1.text = ("goodye cruel world"); }           }        } } 

i tried use textbox1.text=="goodbye cruel world"; evaluation parameter if statement getting no errors in compiler (by way using visual studio 2012 ultimate)

the program runs fine. initalised textbox text property "hello world!" using design gui of vs. problem facing code works first time user clicks button. time after button nothing.

i debugged code , made sure textbox text property appropriately changed first time user clicks button. when user clicks button second time (or time after matter) once code gets if statement skips it, if evaluation of expression within false. in fact, keeping debug tool, button keeps executing code within else block, though know fact textbox.text property working has been appropriately changed before.

what missing here??? why doesn't button switches textbox text value between 2 strings hardcoded within?

you using 3 strings, not two. "goodye cruel world" not equal "goodbye cruel world". hence, cannot expect kind of "string swapping" behaviour whatsoever source code.

lesson learn: not use same string @ different points of code. instead, create constant string variable has value, , use every time need it. example code see habib's answer.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

java - Using an Integer ArrayList in Android -