对象引用问题

时间:2011-06-07 07:37:31

标签: c# class reference copy

我的问题将会非常一致。

我有一个通用类Cell<T>。我有一个客户端类,它使用2维数组Cell<T>

进行操作
public sealed class Environment
{
    private Cell<YUV>[,] primaryLayer;        

    public Colorizator(Cell<YUV>[,] layer)
    {
        // initialization...
    }
// ...
}

Environment类中,我调用了以下方法:

for(var ix = 1; ix < this.Width - 1; ix++)
{
    for(var iy = 1; iy < this.Height - 1; iy++)
    {
        this.primaryLayer[ix, iy].Window[0] = this.primaryLayer[ix - 1, iy + 1];
        this.primaryLayer[ix, iy].Window[1] = this.primaryLayer[ix, iy + 1];
        this.primaryLayer[ix, iy].Window[2] = this.primaryLayer[ix + 1, iy + 1];
        this.primaryLayer[ix, iy].Window[3] = this.primaryLayer[ix + 1, iy];
        this.primaryLayer[ix, iy].Window[4] = this.primaryLayer[ix + 1, iy - 1];
        this.primaryLayer[ix, iy].Window[5] = this.primaryLayer[ix, iy - 1];
        this.primaryLayer[ix, iy].Window[6] = this.primaryLayer[ix - 1, iy - 1];
        this.primaryLayer[ix, iy].Window[7] = this.primaryLayer[ix - 1, iy];
    }
}

它用邻居填充摩尔附近的每个单元格。

然后我在其他方法中使用类似于洪水填充的算法:

foreach(var cell in some_list)
{
    Parent = cell;
    Parent.Conquer(Parent);
    Parent.ViabilityRatio = this.ViabilityTresholds.Max;

    lr[Parent.X, Parent.Y] = Parent;

    while(true)
    {
        if(null == (Child = Parent.Window.FirstOrDefault(x => !(x as Cell<YUV>).IsMarked) as Cell<YUV>))
        {
            break;
        }
        else
        {
            Parent.Mark(Child);               

            var wt = default(Double);
            foreach(Cell<YUV> ch in Child.Window)
            {
                // Operation fails   
                // NullReferenceException occurs: there's no cells in `Child` neighbourhood
                wt += ch.ViabilityRatio;
            }

            Parent = Child;
        }
    }
} 

当我尝试迭代Child.Window时,我发现没有邻域元素。首先,我认为ParentChild,尤其是Child,不会保存对我指定的对象的引用。我的意思是cell变量in the foreach loop没有邻居。但是父母没有。

解决方案1.使用父级单元格

我在Copy类中实现了Cell<T>方法:

public Cell<T> Copy()
{
    return (Cell<T>)this.MemberwiseClone();
} 

从那时起Parent保存了非空Window属性。

foreach(var cell in some_list)
{
    Parent = cell.Copy();

if(null == (Child = Parent.Window.FirstOrDefault(x => !(x as Cell<YUV>).IsConquered) as Cell<YUV>).Copy())审判不幸失败了。

Child没有非Window属性。

请帮助!! 谢谢!

修改

我试着做下一个:

var ActiveWindow = (from ch in Parent.Window select (Cell<YUV>)ch).ToList(); ActiveWindow集合正在保持所有邻居的初始化。此外,我所期待的所有邻居都有自己的邻居被初始化。

但是当我调试这个时: Child = ActiveWindow[0] ... Child不存储邻居..

编辑2

Child单元格不为空。我在else子句中遇到异常。

这是我使用Child单元格获得的内容:enter image description here

这就是我使用Parent单元格所得到的:

enter image description here

1 个答案:

答案 0 :(得分:0)

我希望我没有得到任何错误,这些陈述使我的眼睛受伤:P

Parent.Window不包含任何未被征服的Window,然后

Parent.Window.FirstOrDefault(x => !(x as Cell<YUV>).IsConquered) as Cell<YUV>

由于没有项目符合该模式,因此没有Firstdefault(Cell<YUV>)会返回null

重新考虑这句话:

if (null == (Child = Parent.Window.FirstOrDefault(x => !(x as Cell<YUV>).IsConquered) as Cell<YUV>).Copy())

会屈服:

if (null == (Child = null).Copy())

或者:

if (null == null.Copy())

由于Copy()方法没有对象引用,因此会抛出异常。

相关问题