C# bug

  • Thread starter Thread starter Pedro
  • Start date Start date
P

Pedro

I have this class:

public class Test
{
protected System.Collections.Specialized.BitVector32 _bv32;

public System.Collections.Specialized.BitVector32 BV32 {
get { return _bv32; }
set { _bv32 = value; }
}

public Test()
{
_bv32 = new System.Collections.Specialized.BitVector32(33);
_bv32[1] = false;
}
}

Why if i do:

Test test = new Test();
int a = test.BV32.Data;
test.BV32[1] = false; // DOESN'T COMPILE!!!

it generates a compiler error in the last line???
As you can see in the Test constructor i can do:
_bv32[1] = false

FYI, I'm using VS.NET 2002.

I'm going crazy, please help me.

Pedro.
 
Hi,

This is not a bug, what is happening is that you accessing the BitVector32
through a property, since BitVector32 is a struct a copy is returned. Had
your code compiled successfully, you would have modified a temporary copy of
the BitVector32 instance and had no effect on the actuall instance in the
Test object, and that would have been a bug.

Hope this helps
 
The problem is that BitVector32 is a value type (struct in C#). When you
return _bv32 from a property, you have a copy of it (not a reference, even
if Bitvector32 *seems* an array). Modifying a value type returned from a
property does not make sense, so the C# compiler complains about that.

Have a nice day
GV
 
Pedro said:
I have this class:

public class Test
{
protected System.Collections.Specialized.BitVector32 _bv32;

public System.Collections.Specialized.BitVector32 BV32 {
get { return _bv32; }
set { _bv32 = value; }
}

public Test()
{
_bv32 = new System.Collections.Specialized.BitVector32(33);
_bv32[1] = false;
}
}

Why if i do:

Test test = new Test();
int a = test.BV32.Data;
test.BV32[1] = false; // DOESN'T COMPILE!!!

it generates a compiler error in the last line???
As you can see in the Test constructor i can do:
_bv32[1] = false

FYI, I'm using VS.NET 2002.

I'm going crazy, please help me.

Other posts have explained why you get the error. But, what you can do
to avoid the problem is create you own indexer property:

public bool this [int index] {
get {
if ((index < 0) || (index > 31)) {
throw new IndexOutOfRangeException();
}
int bit = (1 << index);
return _bv32[bit];
}
set {
if ((index < 0) || (index > 31)) {
throw new IndexOutOfRangeException();
}
int bit = (1 << index);
_bv32[bit] = value;
}
}

Note that this indexer treats the BitVector32 as an array of bool, which
may not be what you want, but I think it's what you were going for.

The int indexer in the BitVector32 class uses the index as a bitmask,
and you're supposed to use the CreateMask() methods to set up indexers
that access individual bits - which I found extremely unintuitive. I
wonder why it was designed that way.
 
Thank you very much guys!

mikeb said:
Pedro said:
I have this class:

public class Test
{
protected System.Collections.Specialized.BitVector32 _bv32;

public System.Collections.Specialized.BitVector32 BV32 {
get { return _bv32; }
set { _bv32 = value; }
}

public Test()
{
_bv32 = new System.Collections.Specialized.BitVector32(33);
_bv32[1] = false;
}
}

Why if i do:

Test test = new Test();
int a = test.BV32.Data;
test.BV32[1] = false; // DOESN'T COMPILE!!!

it generates a compiler error in the last line???
As you can see in the Test constructor i can do:
_bv32[1] = false

FYI, I'm using VS.NET 2002.

I'm going crazy, please help me.

Other posts have explained why you get the error. But, what you can do
to avoid the problem is create you own indexer property:

public bool this [int index] {
get {
if ((index < 0) || (index > 31)) {
throw new IndexOutOfRangeException();
}
int bit = (1 << index);
return _bv32[bit];
}
set {
if ((index < 0) || (index > 31)) {
throw new IndexOutOfRangeException();
}
int bit = (1 << index);
_bv32[bit] = value;
}
}

Note that this indexer treats the BitVector32 as an array of bool, which
may not be what you want, but I think it's what you were going for.

The int indexer in the BitVector32 class uses the index as a bitmask,
and you're supposed to use the CreateMask() methods to set up indexers
that access individual bits - which I found extremely unintuitive. I
wonder why it was designed that way.
 
Back
Top