5

I'm a brand new n00bie in visual c# and I ran into a weird obstacle that is driving me CRAZY!! Here is the code in question (yes, an 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 also tried to use textBox1.Text=="Goodbye Cruel World"; as the evaluation parameter for the if statement getting no errors in the compiler (by the way I am using Visual Studio 2012 Ultimate)

The program runs fine. I initalised the textbox text property as "Hello World!" using the Design GUI of VS. The problem I am facing is that the code works only the first time the user clicks the button. Any time after the button does NOTHING.

I debugged the code and I made sure that the textbox text property is appropriately changed the first time the user clicks the button. When the user clicks the button a second time (or any time after that for that matter) once the code gets to the if statement it skips it, as if the evaluation of the expression within is FALSE. In fact, keeping up with the debug tool, the button keeps executing only the code within the else block, even though I know for a fact that the TextBox.Text property that I am working with has been appropriately changed before.

What am I missing here??? Why doesn't the button just switches the textbox text value between the two strings I hardcoded within?

lmolino
  • 73
  • 6
  • 5
    spelling mistake. '"Goodye Cruel World"' you missed `b` in it. – Yahya May 21 '13 at 10:04
  • You are missing a sharp eye here. – Daniel Hilgarth May 21 '13 at 10:07
  • This is why you should always use a single `private const string` field for things like this instead of typing the string over and over. :) – Matthew Watson May 21 '13 at 10:07
  • Put the Goodbye Cruel World into a variable (string), instead of copy paste. At least it will make your code more consistent – Fendy May 21 '13 at 10:07
  • Sorry guys...found the error myself...i guess i just needed to waste some of your time to find out that i'm loosing my eyesight!!! there is a type in the else block in case you are wondering..=) – lmolino May 21 '13 at 10:09
  • 1
    thank you everybody=D wow that was fast....i will be sure to ask other things in case i need it....thanks everybody again! – lmolino May 21 '13 at 10:10
  • 3
    @DanielHilgarth You might even say he's missing a *good eye*. – Hjalmar Z May 21 '13 at 10:10
  • So...can anyone explain to me what is the difference between – lmolino May 21 '13 at 10:12
  • what is the difference between textBox1.Text== and textBox1.Text.Equals ? – lmolino May 21 '13 at 10:13
  • Welcome to StackOverflow. Please have a read through http://stackoverflow.com/about to see how the site works. – John H May 21 '13 at 10:14
  • @Imolino This should be asked as a different question, _except_ that it has been already asked before. See for example: http://stackoverflow.com/questions/3678792/are-string-equals-and-operator-really-same and http://stackoverflow.com/questions/9529422/difference-between-operator-and-equals-method-in-c – Daniel Daranas May 21 '13 at 10:15

3 Answers3

8

You are using three strings, not two. "Goodye Cruel World" is not equal to "Goodbye Cruel World". Hence, you cannot expect any kind of "string swapping" behaviour whatsoever from this source code.

Lesson to learn: Do not use the same string at different points of your code. Instead, create a constant string variable which has that value, and then use it every time you need it. For example code see Habib's answer.

Community
  • 1
  • 1
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
7

That is a case of defining string constant in your code:

public partial class Form1 : Form
{
    private const string GOODBYE = "Goodbye Cruel World";
    private const string HELLO = "Hello World!";
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text.Equals(GOODBYE ))
        {
            textBox1.Text = HELLO;

        }
        else { textBox1.Text = (GOODBYE ); }
    }
}

If you are using same string in multiple places then its better if you define it as a const and use that everywhere in your code, this will help you in reducing errors like the one you have now (Goodye is Goodbye) and it is also easier to change/maintain.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 2
    +1. An interesting addition would be that he needs to use HELLO also to initialize his button, instead of typing again "Hello World!" in the GUI designer tool. – Daniel Daranas May 21 '13 at 10:09
2

Check the spelling of Goodye in the else clause. The condition will always be false.