0

object reference is required for the non-static field, method, or property

我的代码中出现上述错误,我不知道为什么(有很多类似的问题,但我读过的所有问题似乎都与我有关)。

class Program
{
    static void Main(string[] args)
    {

        VenueMappings venueMappings = new VenueMappings();
        var venue1 = new Venue
        {
            ExtEventNo = 1,
            UsiVenue = "building1",
        };

        VenueMappings.venues[0] = venue1;
    }
}

public struct Venue
{
    public int ExtEventNo;
    public String UsiVenue;
};


public class VenueMappings
{
    public Venue[] venues = new Venue[2];  
};
4

1 回答 1

5

这里的问题是您使用的是 class name VenueMappings,但您应该使用 variable venueMappings。您可以通过类名引用静态属性和方法,但您只能通过实例引用实例属性和方法。

代替

VenueMappings.venues[0] = venue1;

venueMappings.venues[0] = venue1;
于 2013-11-13T17:57:51.477 回答