>> I am using visual c++ 2003 i believe.
>>
[quoted text clipped - 25 lines]
>
> if (button1->Text == S"Start")
> ...
> else
[quoted text clipped - 7 lines]
>
> Regards
> >Demostheneswrote:
> >> I am using visual c++ 2003 i believe.
[quoted text clipped - 48 lines]
>
> - 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";
}
Demosthenes - 28 Mar 2008 14:43 GMT
> > >Demostheneswrote:
> > >> I am using visual c++ 2003 i believe.
[quoted text clipped - 63 lines]
>
> - 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;
}
Cholo Lennon - 28 Mar 2008 15:43 GMT
>>>> Demostheneswrote:
>>>>> I am using visual c++ 2003 i believe.
[quoted text clipped - 78 lines]
> 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
--
Cholo Lennon
Bs.As.
ARG