Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2008

Tip: Looking for answers? Try searching our database.

compare string to button text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Demosthenes - 27 Mar 2008 19:25 GMT
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....
Cholo Lennon - 27 Mar 2008 21:10 GMT
> I am using visual c++ 2003 i believe.
>
[quoted text clipped - 21 lines]
> 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 Lennon
Bs.As.
ARG
Ben Voigt [C++ MVP] - 27 Mar 2008 22:14 GMT
>> I am using visual c++ 2003 i believe.
>>
[quoted text clipped - 25 lines]
>
> if (button1->Text == S"Start")

or even better,

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

This makes it clear it is not pointer comparison.

>        ...
> else
[quoted text clipped - 7 lines]
>
> Regards
Demosthenes - 28 Mar 2008 14:37 GMT
> >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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.