简体   繁体   English

总是返回 null 的方法

[英]Method that always return null

I'm stuck with a piece of a code I'm translating from Java to C#.我被一段代码卡住了,我正在从 Java 翻译到 C#。

Basically, I have a Map(Dictionary) with keys composed by Pair and Values represented by a class made by me (Square);基本上,我有一个 Map(Dictionary),其键由 Pair 组成,而 Values 由我制作的 class (Square) 表示; in this class there's only one field, which is Optional (yes, I create the Optional class in C#).在这个 class 中只有一个字段,它是可选的(是的,我在 C# 中创建了可选的 class)。

At the beginning I fill this Dictionary with pairs to make a simil-grid and empty Optional, as you can see in the code below.一开始我用对填充这个字典来创建一个 simil-grid 和空的 Optional,你可以在下面的代码中看到。

class World
{
    private Dictionary<Pair<int, int>, Square> map = 
        new Dictionary<Pair<int, int>, Square>();

    public World(int width, int height)
    {
        this.size = new Pair<int, int>(width, height);
        for (int w = 0; w < this.size.GetX(); w++)
        {
            for (int h = 0; h < this.size.GetY(); h++)
                this.map.Add(new Pair<int, int>(w, h), 
                    new Square(Optional<Entity>.Empty()));
        }
    }
}

And this is the Square class这是方形 class

class Square
{
    private Optional<Entity> entity;

    public Square (Optional<Entity> entity)
    {
        this.entity = entity;
    }

    public Optional<Entity> GetEntity() 
    {
        return this.entity;
    }

    public void SetEntity(Optional<Entity> entity)
    {
        this.entity = entity;
    }
}

Here's the problem, this function below always returns null when I try to get an existent value from the Dictionary, it throws System.NullReferenceException: Object reference not set to an instance of an object. Here's the problem, this function below always returns null when I try to get an existent value from the Dictionary, it throws System.NullReferenceException: Object reference not set to an instance of an object. In this code I cut away all the controls, but I know that I try to get a value already inserted;在这段代码中,我删除了所有控件,但我知道我试图获取一个已经插入的值; also, I tried to run Dictionary.ContainsValue and it return false.另外,我尝试运行 Dictionary.ContainsValue 并返回 false。 But I do have inizialed the Dictionary.但我确实已经初始化了字典。

public Square? GetSquare(int x, int y)
{
    if (y < this.size.GetY() && y >= 0 && < x this.size.GetX() && x >= 0)
    {
        this.map.TryGetValue(new Pair<int, int>(x, y), out Square? square);
        return square;
    }

    throw new InvalidOperationException("no square in this position!");
}

I leave here the code of the Optional class too, but I'm almost 100% sure that it's not the problem我在这里也留下了可选 class 的代码,但我几乎 100% 确定这不是问题

public class Optional<T>
{
    private T value;
    public bool IsPresent { get; set; } = false;

    private Optional() { }

    public static Optional<T> Empty()
    {
        return new Optional<T>();
    }

    public static Optional<T> Of(T value)
    {
        Optional<T> obj = new Optional<T>();
        obj.Set(value);
        return obj;
    }

    private void Set(T value)
    {
        this.value = value;
        this.IsPresent = true;
    }

    public T Get()
    {
        return value;
    }
}

This is Pair class这是一对 class

    public class Pair<X, Y>
    {
        private X first;
        private Y second;

        public Pair(X first, Y second)
        {
            this.first = first;
            this.second = second;
        }
        public X GetX()
        {
            return this.first;
        }

        public Y GetY()
        {
            return this.second;
        }

        public override string ToString()
        {
            return "<" + first + "," + second + ">";
        }
    }

Ok, solved it.好的,解决了。 The problem was, as you said, the Pair class.正如您所说,问题是对 class。 I substitute it with the ValueTuple<> class and now everything works fine, still thanks.我用 ValueTuple<> class 代替它,现在一切正常,仍然感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM