There is such a condition:

if (queryObj["DeviceID"].ToString().Trim() == @"USB\VID_0403&PID_6001\A900ER2T") { // TODO } 

In queryObj["DeviceID"] is exactly the same text as on the right when comparing. Even brought it to MessageBox for clarity:

MessageBox with DeviceID

When this condition does not work - what's the catch?

Compared to the length of the lines:

 1) USB\VID_0403&PID_6001\A900ER2T##30 // где 30 длина строки 2) string str2 = @"USB\VID_0403&PID_6001\A900ER2T"; // 30 

Now the check is:

 string PID = "0403"; string VID = "6001"; MessageBox.Show(str_PID + " | " + str_VID); if (str_PID == PID && str_VID == VID){ Сюда не заходит при этом str_PID = 0403 str_VID = 6001 } 
  • Miracles do not happen. Compare for interest the length of the lines to begin with. - VladD
  • Well, well: string s1 = queryObj["DeviceID"].ToString().Trim(); string s2 = @"USB\VID_0403&PID_6001\A900ER2T"; for (int i = 0; i < 30; i++) if (s1[i] != s2[i]) MessageBox.Show("Position " + i); string s1 = queryObj["DeviceID"].ToString().Trim(); string s2 = @"USB\VID_0403&PID_6001\A900ER2T"; for (int i = 0; i < 30; i++) if (s1[i] != s2[i]) MessageBox.Show("Position " + i); ? - VladD
  • But you said that the length of both lines is 30. Something is wrong here. - VladD
  • one
    And what have the faq? - Qwertiy
  • Simplified code - updated question - Piubur

1 answer 1

Try using the CompareTo method. The fact is that == compares two references to objects and determines whether they refer to the same instance. The CompareTo method CompareTo designed to test two objects for equivalence

 string str = @"USB\VID_0403&PID_6001\A900ER2T"; if(str.CompareTo(@"USB\VID_0403&PID_6001\A900ER2T") == 0) { //TODO } 

At me it returns TRUE .

  • 7
    for Object.String operator== compares values, not references. - ixSci