0

I have a list, something like this "List< Block >". What I want is to get a block which is same as one in the list, not the object in the list. And do dome modify with the block and get another one.

But the problem is, after get the block three times (which the list's length is 3) the fourth block is already been modified. I tried all the ways I know, even use "new", it just get the object in the list, not a same one.

So how to solve this?

Here is some of my code:

//This is the list which length is 3
private List<BlockType> blocks;

//At the beginning it was like this but not work
//private List<Block> blocks;

//In a function to get a block type randomly
int blockNum = rand.Next(0, 3); //rand is a Random type
this.cBlock = new Block(blocks[blockNum]); //cBlock is object which I use to do something about the block

//The class Block goes to
class Block
{
    private List<Rectangle> _block;
    public List<Rectangle> block
    {
        get { return _block; }
    }

    private int _blockNum;
    public int blockNum
    {
        get { return _blockNum; }
    }

    public Block()
    {

    }

    public Block(int blockNum, List<Rectangle> block)
    {
        this._block = block;
        this._blockNum = blockNum;
    }

    public Block(BlockType block)
    {
        this._block = block.block;
        this._blockNum = block.blockNum;
    }
}

//And the BlockType is what I tried but does not work
class BlockType
{
    private List<Rectangle> _block;
    public List<Rectangle> block
    {
        get { return _block; }
    }

    private int _blockNum;
    public int blockNum
    {
        get { return _blockNum; }
    }

    public BlockType()
    {

    }

    public BlockType(int blockNum, List<Rectangle> block)
    {
        this._block = block;
        this._blockNum = blockNum;
    }
}
Super Guo
  • 133
  • 1
  • 1
  • 8

1 Answers1

0

EDIT:

OP was mistaking a new instance of Block as the one being pulled out of List<Block> because List<Rectangle> was not cloned in the copy constructor but simply assigned, hence resulting in two unique instances of Block referencing the same List<Rectangle>.

The correct copy constructor in this case would be:

public Block(Block originalBlock)
{
    this._block = new List<Rectangle>(originalBlock.block);
    this._blockNum = originalBlock.blockNum;
}

Which requires a similar copy constructor in the Rectangle class.


This should work for you:

public class Block
{
    private List<Rectangle> _block;
    public List<Rectangle> block
    {
        get { return _block; }
    }

    private int _blockNum;
    public int blockNum
    {
        get { return _blockNum; }
    }

    // Copy constructor
    public Block(Block originalBlock)
    {
        // IMPORTANT: This does not create a new List<Rectangle>! See EDIT
        this._block = originalBlock.block;

        this._blockNum = originalBlock.blockNum;
    }
}

Usage:

Block originalBlock = new Block();

// Returns a new instance of Block with similar member data.
Block copiedBlock = new Block(originalBlock);

Read up on How to: Write a Copy Constructor (C# Programming Guide) | Microsoft Docs

Swift
  • 3,250
  • 1
  • 19
  • 35
  • Emmm, I already tried, but it does not work. So I create a new type BlockType to see if it can do what I want. But, I don't know, it just not work. – Super Guo May 05 '18 at 12:49
  • This should work by definition as I've tried it myself and is shown in the docs. It might just be a logic error somewhere in your code. Maybe you want to show more related code? – Swift May 05 '18 at 13:09
  • Oops, I find the reason.... The reason is not the object is not been copied. The reason is the block, a field in the Block, is also a List type and this list does not copied. It reference to the same memory. But still thanks for your answer. It make me try again and find why. Thanks a lot. – Super Guo May 05 '18 at 13:56
  • Haha, that completely went over my head too. Updated the answer to reflect the new findings. – Swift May 05 '18 at 14:32