Click to See Complete Forum and Search --> : problems with comparing Object's properties


phr0663r
December 19th, 2006, 08:07 AM
ok, first forum post and a newbee .net programmer so forgive a little please!

the problem is that when comparing the text property of two label's proves to always fail when
made from Args->KeyChar.ToString()
i made a little test program to isolate the issue which is here:


#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Text;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public __gc class CGui : public Form {
public:
CGui(){
// initialise first label
this->myLabel01 = new Label();
this->myLabel01->Location = Drawing::Point(0,0);
this->myLabel01->Size = Drawing::Size(20,20);
this->myLabel01->Text = S"x";
this->Controls->Add(this->myLabel01);

// initialise second label
this->myLabel02 = new Label();
this->myLabel02->Location = Drawing::Point(50,0);
this->myLabel02->Size = Drawing::Size(20,20);
this->myLabel02->Text = S"x";
this->Controls->Add(this->myLabel02);

// add keypress event handler to set label text to key pressed
this->add_KeyPress(new KeyPressEventHandler(this,KeyPress));
// test that label's text match at begining of test
testString();
testString02();
// set label02's text value to not match label01's and re-test
this->myLabel02->Text = S"w";
testString();
testString02();
}
private:
Label * myLabel01;
Label * myLabel02;
void KeyPress(Object * Sender, KeyPressEventArgs * Args){
// set label's text value to the pressed key's char in string format
// this is where the error must occur
this->myLabel01->Text = Args->KeyChar.ToString();
this->myLabel02->Text = Args->KeyChar.ToString();
// run test again to evaluate change in label's text value
testString();
testString02();
}
void testString(){
if(this->myLabel01->Text == this->myLabel02->Text)
MessageBox::Show(S"both label's text values are the same value",S"String test 01");
else
MessageBox::Show(S"the label's text values are NOT the same value",S"String test 01");
}
void testString02(){
if(this->myLabel01->Text == this->myLabel01->Text)
MessageBox::Show(S"label01's text value is itself",S"String test 02");
else
MessageBox::Show(S"label01's text value is NOT itself!",S"String test 02");
}
};
int __stdcall WinMain(){
Application::Run(new CGui());
return 0;
}

can anyone tell me why once the keyPress func has been run the text values never match even when comparing label01->text with itself!
and any ideas to work around it?

darwen
December 20th, 2006, 03:42 AM
I've found this too with strings in Managed C++. They don't seem to work with the '==' operator.

Try using


if(String::Compare(this->myLabel01->Text, this->myLabel02->Text) == 0)
{
}


Darwen.

TheCPUWizard
December 20th, 2006, 07:02 AM
That's because (for default implementations) "==" is a reference comparator (do both things point to the same memory instance).ince strings are immutable, no ttwo of them ever point to the same thing.

You need to compare contents (as Darwen has shown one method of doing).

This is by design. [and there are other good reasons :) ]

phr0663r
December 28th, 2006, 07:43 AM
thank you for that! when you put it that way it makes sence and
me == fool!!!
but there you go live and learn!!!

Zaccheus
January 2nd, 2007, 02:31 PM
System::String has an overloaded == operator, but Managed C++ (VC2003) failed to use it for some reason.

Comparing strings using == does work in C++/CLI (the new syntax introduced with VC2005).