Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statements in buttons c#

Tags:

c#

c#-4.0

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?

like image 589
lmolino Avatar asked May 21 '13 10:05

lmolino


3 Answers

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.

like image 83
Daniel Daranas Avatar answered Sep 28 '22 06:09

Daniel Daranas


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.

like image 35
Habib Avatar answered Sep 28 '22 04:09

Habib


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

like image 31
vikas rajan Avatar answered Sep 28 '22 06:09

vikas rajan