compare string to button text

  • Thread starter Thread starter Demosthenes
  • Start date Start date
D

Demosthenes

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(System::Object * sender,
System::EventArgs * 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....
 
Demosthenes said:
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(System::Object * sender,
System::EventArgs * 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:

http://www.codeproject.com/KB/mcpp/managed_types.aspx
http://punkouter.wordpress.com/2007/03/22/managed-c-string-equality-reference-ob
jects-equality/


Regards
 
Cholo said:
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.
 
or even better,

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

This makes it clear it is not pointer comparison.





- 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";
}
 
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;
}
 
Demosthenes said:
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;
}


Well, Ben was right. His aproach is the correct way to compare strings. If you
use my aproach you're comparing objects. The question is: Why my aproach works
sometimes? It works because internally the form and S prefixed strings use the
same MSIL instruction to load strings from meatada, ldstr. The documentation for
ldstr says:

"...The Common Language Infrastructure (CLI) guarantees that the result of two
ldstr instructions referring to two metadata tokens that have the same sequence
of characters return precisely the same string object (a process known as
"string interning")..."

So, in the case of

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

We're comparing the same String object.
Why sometimes fails? Because the next time you change the text with:

button1->Text = "Start";

you're not going to use a string loaded from metadata with ldstr. Internally
"Start" is trated like an array of char (data isn't stored in the metada
section). When you assign the string, a new String object is created, so the
next comparisson will fail (because, you're going to compare two distinct
objects).

If you assign prefixed strings, the code will work too:

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


Regards
 
Back
Top