If comparison is not working properly

Simple example:
A windows form with a textbox for password, button for event, and label for go or no go

1
2
3
4
5
6
7
8
9
10
11
12
13
	private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
			 {
				 System::String * txtPassword = textBox1->Text;
				 System::String * setPassword = "12345678";
				 if (txtPassword == setPassword)
				 {
					 label1->Text = "Correct";
				 }
				 else
				 {
					 label1->Text = "Try again";
				 }
			 }


Well I thought == is the correct comparison operator to use. But when running the form it always returns "Try again" even if its correct. I'm using VC++ .Net 2003. What's wrong?
maybe this code helps you

 
(strcmp (txtPassword ,setPassword) == 0)


http://www.cplusplus.com/reference/cstring/strcmp/

i am not sure this is answer of your question but == operator for strings is not useful.
Is strcmp the only option? I've seen other postings saying == is suitable but I couldn't get it to work in my case.

What is the easiest way to convert textBox1 text string to array of char? I need to do this because the strcmp arguments only accept char.
1
2
3
char* convertStringToCharArray(string str){
  return (char*)str.c_str();
}


txtPassword and setPassword are pointers to System::String objects.

You have to dereference them to compare what they're pointing to:

if (*txtPassword == *setPassword)

Your code is comparing the two pointers, which are always going to be different.

Cheers,
Jim
Topic archived. No new replies allowed.