BitArray Handling by a newbie

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

giving the 2 methods:

public bool SimpleBitArrayTest1(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
return array1.Equals(array2);
}

public bool SimpleBitArrayTest2(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
bool result = false;
for(int i = 0; i < array1.Length; i++) {
if (array1 != array2)
return result;
}
return true;
}

Can anyone explain to me why:
SimpleBitArrayTest1 returns false and
SimpleBitArrayTest2 returns true

Is this a bug or do i miss something?
 
Strange, not what I'd have expected anyway...

The reason must be that the BitArray "==" / Equals operator is not
overloaded, so this still asks the question, is the same object being
referenced by each variable, rather than, does each object that is reference
have the same contents.
 
MSDN says it is overloaded actually: "Overloaded. Determines whether two
Object instances are equal."

So it's overloaded to say that the object is the same...
It's testing this:

public bool SimpleBitArrayTest3(int length, int order)
{
BitArray array1 = new BitArray(length);
BitArray array2 = array1;
return array1.Equals(array2);
}


Array

Dan Bass said:
Strange, not what I'd have expected anyway...

The reason must be that the BitArray "==" / Equals operator is not
overloaded, so this still asks the question, is the same object being
referenced by each variable, rather than, does each object that is
reference have the same contents.

Heribert Coumans said:
giving the 2 methods:

public bool SimpleBitArrayTest1(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
return array1.Equals(array2);
}

public bool SimpleBitArrayTest2(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
bool result = false;
for(int i = 0; i < array1.Length; i++) {
if (array1 != array2)
return result;
}
return true;
}

Can anyone explain to me why:
SimpleBitArrayTest1 returns false and
SimpleBitArrayTest2 returns true

Is this a bug or do i miss something?

 
There is no meaning (in my opinion ) that Equals responds only true if 2
object instances are equal. See the following simpletest. It returns
true......

public bool AnOtherSimpleTest1(string aString) {
StringBuilder strB1 = new StringBuilder();
StringBuilder strB2 = new StringBuilder();
strB1.Append(aString);
strB2.Append(aString);
return strB1.Equals(strB2);
}

Dan Bass said:
MSDN says it is overloaded actually: "Overloaded. Determines whether two
Object instances are equal."

So it's overloaded to say that the object is the same...
It's testing this:

public bool SimpleBitArrayTest3(int length, int order)
{
BitArray array1 = new BitArray(length);
BitArray array2 = array1;
return array1.Equals(array2);
}


Array

Dan Bass said:
Strange, not what I'd have expected anyway...

The reason must be that the BitArray "==" / Equals operator is not
overloaded, so this still asks the question, is the same object being
referenced by each variable, rather than, does each object that is
reference have the same contents.

Heribert Coumans said:
giving the 2 methods:

public bool SimpleBitArrayTest1(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
return array1.Equals(array2);
}

public bool SimpleBitArrayTest2(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
bool result = false;
for(int i = 0; i < array1.Length; i++) {
if (array1 != array2)
return result;
}
return true;
}

Can anyone explain to me why:
SimpleBitArrayTest1 returns false and
SimpleBitArrayTest2 returns true

Is this a bug or do i miss something?


 
I agree.

Heribert Coumans said:
There is no meaning (in my opinion ) that Equals responds only true if 2
object instances are equal. See the following simpletest. It returns
true......

public bool AnOtherSimpleTest1(string aString) {
StringBuilder strB1 = new StringBuilder();
StringBuilder strB2 = new StringBuilder();
strB1.Append(aString);
strB2.Append(aString);
return strB1.Equals(strB2);
}

Dan Bass said:
MSDN says it is overloaded actually: "Overloaded. Determines whether two
Object instances are equal."

So it's overloaded to say that the object is the same...
It's testing this:

public bool SimpleBitArrayTest3(int length, int order)
{
BitArray array1 = new BitArray(length);
BitArray array2 = array1;
return array1.Equals(array2);
}


Array

Dan Bass said:
Strange, not what I'd have expected anyway...

The reason must be that the BitArray "==" / Equals operator is not
overloaded, so this still asks the question, is the same object being
referenced by each variable, rather than, does each object that is
reference have the same contents.

message giving the 2 methods:

public bool SimpleBitArrayTest1(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
return array1.Equals(array2);
}

public bool SimpleBitArrayTest2(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
bool result = false;
for(int i = 0; i < array1.Length; i++) {
if (array1 != array2)
return result;
}
return true;
}

Can anyone explain to me why:
SimpleBitArrayTest1 returns false and
SimpleBitArrayTest2 returns true

Is this a bug or do i miss something?

 
What you are observing here is due to the fact that the BitArray.Equals is
inherited from Object.
This means that there is no modification from what the Object class does;
which is a straight reference comparison.
In order for the comparison to work on the values contained rather than the
references, the Equals method should be *overriden*. And it is not in the
BitArray class.
The method is overloaded; meaning that there exists two versions of it that
differ by the number/type of parameters.
But the Equals method of StringBuilder is overriden to compare the contents
hence the fact that the method gives the expected results (true) for
StringBuilder.


HTH,
Cois
 
Thanks for your response. Your answer is correct. The behaviour in BitArray
is not logical..

Francois Bonin said:
What you are observing here is due to the fact that the BitArray.Equals is
inherited from Object.
This means that there is no modification from what the Object class does;
which is a straight reference comparison.
In order for the comparison to work on the values contained rather than the
references, the Equals method should be *overriden*. And it is not in the
BitArray class.
The method is overloaded; meaning that there exists two versions of it that
differ by the number/type of parameters.
But the Equals method of StringBuilder is overriden to compare the contents
hence the fact that the method gives the expected results (true) for
StringBuilder.


HTH,
Cois
Heribert Coumans said:
giving the 2 methods:

public bool SimpleBitArrayTest1(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
return array1.Equals(array2);
}

public bool SimpleBitArrayTest2(int length, int order) {
BitArray array1 = new BitArray(length);
BitArray array2 = new BitArray(length);
array1[order] = true;
array2[order] = true;
bool result = false;
for(int i = 0; i < array1.Length; i++) {
if (array1 != array2)
return result;
}
return true;
}

Can anyone explain to me why:
SimpleBitArrayTest1 returns false and
SimpleBitArrayTest2 returns true

Is this a bug or do i miss something?

 
Back
Top