compare string to button text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Demosthenes

    compare string to button text

    I am using visual c++ 2003 i believe.

    I created a new win32 application, and put 1 button on the form and
    changed it's text to "Start".


    and this code does not work:


    private: System::Void button1_Click(S ystem::Object * sender,
    System::EventAr gs * e)
    {
    if (button1->Text == "Start")
    {
    button1->Text = "Stop";
    }
    else
    {
    button1->Text == "Start";
    }
    }


    the project compiles ok, but when the code executes, the if statement
    always goes to the else clause. The text on the button is "Start"
    though. I think I am not understanding some fundamental concepts
    with
    pointers/objects....


  • Cholo Lennon

    #2
    Re: compare string to button text

    Demosthenes wrote:
    I am using visual c++ 2003 i believe.
    >
    I created a new win32 application, and put 1 button on the form and
    changed it's text to "Start".
    >
    >
    and this code does not work:
    >
    >
    private: System::Void button1_Click(S ystem::Object * sender,
    System::EventAr gs * e)
    {
    if (button1->Text == "Start")
    {
    button1->Text = "Stop";
    }
    else
    {
    button1->Text == "Start";
    }
    }
    >
    >
    the project compiles ok, but when the code executes, the if statement
    always goes to the else clause. The text on the button is "Start"
    though. I think I am not understanding some fundamental concepts
    with
    pointers/objects....
    When comparing add S prefix to your literals:

    if (button1->Text == S"Start")
    ...
    else
    ...

    Take a look to:


    Here’s a bug that recently crept into my project. A teammate made a simple comparison between strings like this: String* a = “abc”; String* b = “abc”; if ( a == b ) { …

    jects-equality/


    Regards


    --
    Cholo Lennon
    Bs.As.
    ARG



    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: compare string to button text

      Cholo Lennon wrote:
      Demosthenes wrote:
      >I am using visual c++ 2003 i believe.
      >>
      >I created a new win32 application, and put 1 button on the form and
      >changed it's text to "Start".
      >>
      >>
      >and this code does not work:
      >>
      >>
      >private: System::Void button1_Click(S ystem::Object * sender,
      >System::EventA rgs * e)
      > {
      > if (button1->Text == "Start")
      > {
      > button1->Text = "Stop";
      > }
      > else
      > {
      > button1->Text == "Start";
      > }
      > }
      >>
      >>
      >the project compiles ok, but when the code executes, the if statement
      >always goes to the else clause. The text on the button is "Start"
      >though. I think I am not understanding some fundamental concepts
      >with
      >pointers/objects....
      >
      When comparing add S prefix to your literals:
      >
      if (button1->Text == S"Start")
      or even better,

      if (button1->Text->Equals(S"Start "))

      This makes it clear it is not pointer comparison.
      ...
      else
      ...
      >
      Take a look to:
      >

      Here’s a bug that recently crept into my project. A teammate made a simple comparison between strings like this: String* a = “abc”; String* b = “abc”; if ( a == b ) { …

      jects-equality/
      >
      >
      Regards

      Comment

      • Demosthenes

        #4
        Re: compare string to button text

        On Mar 27, 5:14 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
        Cholo Lennon wrote:
        Demostheneswrot e:
        I am using visual c++ 2003 i believe.
        >
        I created a new win32 application, and put 1 button on the form and
        changed it's text to "Start".
        >
        and this code does not work:
        >
        private: System::Void button1_Click(S ystem::Object *  sender,
        System::EventAr gs *  e)
            {
                   if (button1->Text == "Start")
                {
                  button1->Text = "Stop";
                    }
              else
                {
                  button1->Text == "Start";
                 }
            }
        >
        the project compiles ok, but when the code executes, the if statement
        always goes to the else clause.  The text on the button is "Start"
        though.  I think I am not understanding some fundamental concepts
        with
        pointers/objects....
        >
        When comparing add S prefix to your literals:
        >
        if (button1->Text == S"Start")
        >
        or even better,
        >
        if (button1->Text->Equals(S"Start "))
        >
        This makes it clear it is not pointer comparison.
        >
        >
        >
               ...
        else
               ...
        >
        Take a look to:
        >>
        Regards- Hide quoted text -
        >
        - Show quoted text -- Hide quoted text -
        >
        - Show quoted text -
        thanks guys, now i run into a strange thing here, the first time i
        press the button, the buttons text = start, and gets set to stop.
        Then from stop to start, then it never works again. here is my code

        if (button1->Text == S"Start")
        {
        button1->Text = "Stop";
        }
        else
        {
        button1->Text = "Start";
        }



        Comment

        • Demosthenes

          #5
          Re: compare string to button text

          On Mar 28, 9:37 am, Demosthenes <sadbastur...@y ahoo.comwrote:
          On Mar 27, 5:14 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
          >
          >
          >
          >
          >
          Cholo Lennon wrote:
          >Demostheneswro te:
          >I am using visual c++ 2003 i believe.
          >
          >I created a new win32 application, and put 1 button on the form and
          >changed it's text to "Start".
          >
          >and this code does not work:
          >
          >private: System::Void button1_Click(S ystem::Object *  sender,
          >System::EventA rgs *  e)
          >    {
          >           if (button1->Text == "Start")
          >        {
          >          button1->Text = "Stop";
          >            }
          >      else
          >        {
          >          button1->Text == "Start";
          >         }
          >    }
          >
          >the project compiles ok, but when the code executes, the if statement
          >always goes to the else clause.  The text on the button is "Start"
          >though.  I think I am not understanding some fundamental concepts
          >with
          >pointers/objects....
          >
          When comparing add S prefix to your literals:
          >
          if (button1->Text == S"Start")
          >
          or even better,
          >
          if (button1->Text->Equals(S"Start "))
          >
          This makes it clear it is not pointer comparison.
          >
                 ...
          else
                 ...
          >
          Take a look to:
          >>
          Regards- Hide quoted text -
          >
          - Show quoted text -- Hide quoted text -
          >
          - Show quoted text -
          >
          thanks guys, now i run into a strange thing here, the first time i
          press the button, the buttons text = start, and gets set to stop.
          Then from stop to start, then it never works again.  here is my code
          >
               if (button1->Text == S"Start")
                  {
                    button1->Text = "Stop";
                  }
                else
                  {
                    button1->Text = "Start";
                  }- Hide quoted text -
          >
          - Show quoted text -
          but this seems to work fine:

          if (button1->Text->Equals(S"Start "))
          {
          button1->Text = "Stop";
          timer1->Enabled = true;
          timer1->Start;
          }
          else
          {
          button1->Text = "Start";
          timer1->Stop;
          timer1->Enabled = false;
          }

          Comment

          Working...