locked
Strange behaviour of TableCell blocks RRS feed

  • Question

  • Brief background
    I'm trying to write a method that splits a table cell but I'm trying to write it in a generic way such that the content Blocks of the created cells can come from any source, including the BlockCollection from the original cell.

    I was keeping the existing cell as the first in the set of new cells, which means I want to clear its contents before adding the new contents.
    If I do this and the contents is coming from the original cell (through the generic method) then those contents will be lost.

    So what I needed to do was check if the collection of Blocks I am adding is the original BlockCollection and leave it alone but it was getting cleared.

    This is where I tracked the bug to. A simple test illustrates:

    [TestMethod]
    public void TestBlockReference()
    {
        TableCell cell = new TableCell(new Paragraph(new Run("test")));
        TableRow row = new TableRow();
        row.Cells.Add(cell);

        Assert.IsTrue(IsCellsBlocks(cell, cell.Blocks));
    }

    static bool IsCellsBlocks(TableCell cell, BlockCollection bc)
    {
        return bc == cell.Blocks;
    }

    This fails!

    I got around this by not keeping the original cell but I still would like to know why that equality check fails. Is == overidden to always return false? If so why?
    Friday, July 18, 2008 2:05 PM

Answers

  • -> I got around this by not keeping the original cell but I still would like to know why that equality check fails. Is == overidden to always return false? If so why?

    It seems this is the desired behaviour, the following is the implementation of the Blocks property:

    /// <value>
    /// Collection of Blocks contained in this Section.
    /// </value>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public BlockCollection Blocks
    {
        get
        {
            return new BlockCollection(this, /*isOwnerParent*/true);
        }
    }

    So every time you access the Blocks property, and new instance of BlockCollection will be returned.

    Hope this helps
    • Proposed as answer by Wodahs Wednesday, July 23, 2008 4:44 PM
    • Marked as answer by Marco Zhou Thursday, July 24, 2008 11:01 AM
    Wednesday, July 23, 2008 6:28 AM
  • Interesting... wonder why they decided to do it that way.  (They probably had a good reason... just curious as to what it was.)

    Paragraph p = new Paragraph(new Run("test"));  
    TableCell cell = new TableCell(p);  
    TableCell cell2 = new TableCell(p); 

    cell.Blocks.FirstBlock == cell.Blocks.FirstBlock    // Returns True
    cell.Blocks.FirstBlock == cell2.Blocks.FirstBlock  // Returns False

    Does seem to work so the OP could do this:

    return bc.FirstBlock == cell.Blocks.FirstBlock;


    John Fenton
    • Marked as answer by Marco Zhou Thursday, July 24, 2008 11:02 AM
    Wednesday, July 23, 2008 5:19 PM

All replies

  • That seems like a bug, I would report it as such and see what the response is.

    https://connect.microsoft.com/
    John Fenton
    Friday, July 18, 2008 10:05 PM
  • -> I got around this by not keeping the original cell but I still would like to know why that equality check fails. Is == overidden to always return false? If so why?

    It seems this is the desired behaviour, the following is the implementation of the Blocks property:

    /// <value>
    /// Collection of Blocks contained in this Section.
    /// </value>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public BlockCollection Blocks
    {
        get
        {
            return new BlockCollection(this, /*isOwnerParent*/true);
        }
    }

    So every time you access the Blocks property, and new instance of BlockCollection will be returned.

    Hope this helps
    • Proposed as answer by Wodahs Wednesday, July 23, 2008 4:44 PM
    • Marked as answer by Marco Zhou Thursday, July 24, 2008 11:01 AM
    Wednesday, July 23, 2008 6:28 AM
  • Interesting... wonder why they decided to do it that way.  (They probably had a good reason... just curious as to what it was.)

    Paragraph p = new Paragraph(new Run("test"));  
    TableCell cell = new TableCell(p);  
    TableCell cell2 = new TableCell(p); 

    cell.Blocks.FirstBlock == cell.Blocks.FirstBlock    // Returns True
    cell.Blocks.FirstBlock == cell2.Blocks.FirstBlock  // Returns False

    Does seem to work so the OP could do this:

    return bc.FirstBlock == cell.Blocks.FirstBlock;


    John Fenton
    • Marked as answer by Marco Zhou Thursday, July 24, 2008 11:02 AM
    Wednesday, July 23, 2008 5:19 PM