Hashtable and "class containing arrays" as value

  • Thread starter Thread starter bill.gates.com
  • Start date Start date
B

bill.gates.com

Dear Coders:

I have a hashtable,

Hashtable hash=new Hashtable();

and have a class, Pixels

class Pixels
{
public int[,] buf=new int[5,5];
..
..
..

}

when I set this
Pixels p=new Pixels;
p.buf[1,1]=0;
p.buf[2,2]=1;

hash[5]=p;

Pixels p2=hash[5];

!!! now all items of buf[,] are set to 1
 
Hello,

I have tried this out and i am getting right result.

The code is


class Class1
{

[STAThread]
static void Main(string[] args)
{
Hashtable hash=new Hashtable();
Pixels p=new Pixels( );
p.buf[1,1]=0;
p.buf[2,2]=1;

hash[5]=p;

Pixels p2=hash[5] as Pixels;

Console.Write( p2.buf[1,1] + " " + p2.buf[2,2]);

}
}

class Pixels
{
public int[,] buf=new int[5,5];
}
 
I have a hashtable,

Hashtable hash=new Hashtable();

and have a class, Pixels

class Pixels
{
public int[,] buf=new int[5,5];
.
.
.

}

when I set this
Pixels p=new Pixels;
p.buf[1,1]=0;
p.buf[2,2]=1;

hash[5]=p;

Pixels p2=hash[5];

!!! now all items of buf[,] are set to 1

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Back
Top