Object array problem
-
Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????
CC26
-
Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????
CC26
Equals method will return true only if both the objects refer to same memory location. Otherwise false. Here obj1 and obj2 are referring to two different memory locations. Hence you are getting return value as false.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
Equals method will return true only if both the objects refer to same memory location. Otherwise false. Here obj1 and obj2 are referring to two different memory locations. Hence you are getting return value as false.
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
Thanks for the reply danish. What if instead of Equal I did if(obj1==obj2)?
CC26
-
Thanks for the reply danish. What if instead of Equal I did if(obj1==obj2)?
CC26
-
Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????
CC26
Try this:
if (obj1.Length != obj2.Length) { return false; } else { return true; for (int i=0; i < obj1.Length; i++) { if (obj1[i] != obj2[i]) { return false; break; } } }
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
-
Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????
CC26
CrazyCoder26 wrote:
But it is returning false (I know I am wrong).
Actually you comparing memory location thats why you it return false. consider the case :- When you create object, the memory is created in Heap and it's heap memory reference is stored in stack. So object as such nothing but variable containing address location, so when you compare two object, they bound to differ on memory address, so the answer
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????
CC26
-
Hi I have a problem as described below : <pre> object[] obj1={1,"hello"}; object[] obj2={1,"hello"}; if(obj1.Equals(obj2)) { return true; } else { return false; } </pre> According to me, it should return true. But it is returning false (I know I am wrong). But why?????
CC26
If you are wanting to compare the contents of the arrays, then consider the following code:
/// <summary> /// Compare 2 arrays for equality. /// </summary> /// <param name="data1">Array 1.</param> /// <param name="data2">Array 2.</param> /// <returns><c>true</c> if equal, <c>false</c> otherwise.</returns> public static bool CompareArrays<T>(T\[\] data1, T\[\] data2) { // If both are null, they're equal if ((data1 == null) && (data2 == null)) { return true; } // If either but not both are null, they're not equal if ((data1 == null) || (data2 == null)) { return false; } if (data1.Length != data2.Length) { return false; } for (int i = 0; i < data1.Length; i++) { if (!data1\[i\].Equals(data2\[i\])) { return false; } } return true; }
-
Try this:
if (obj1.Length != obj2.Length) { return false; } else { return true; for (int i=0; i < obj1.Length; i++) { if (obj1[i] != obj2[i]) { return false; break; } } }
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
Uh, that would return true for any case where obj1 and obj2 are the same length.
-
Try this:
if (obj1.Length != obj2.Length) { return false; } else { return true; for (int i=0; i < obj1.Length; i++) { if (obj1[i] != obj2[i]) { return false; break; } } }
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
-
A little bit wrong, maybe it should look like this:
if (obj1.Length != obj2.Length)
{
return false;
}
else
{
for (int i=0; i < obj1.Length; i++)
{
if (!object.Equals(obj1[i], obj2[i]))
{
return false;
}
}
return true;
}Yes,you are right,I just couldn't find a bit free time to modify my answer. Thanks for correction.
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com