0

我总是在以下行中收到此错误消息:

  Casttoenum(this.Polygontype);

无法通过嵌套类型“WindowsPhoneGame2.Containerclass.PolygonContainer”访问外部类型“WindowsPhoneGame2.Containerclass”的非静态成员

怎么了?如何从我的 PolygonContainer 中调用公共 void Casttoenum?

解决此问题的最佳方法是什么?

    public List<PolygonContainer> PolygonList = new List<PolygonContainer>();
    public struct PolygonContainer
    {
        public float PolygonpositionX;
        public float PolygonpositionY;
        public float Polygonrotation;
        public int Polygontype;

        public PolygonContainer(float polygonpositionx, float polygonpositiony, float polygonrotation, int polygontype)
            : this()
        {
            this.PolygonpositionX = polygonpositionx;
            this.PolygonpositionY = polygonpositiony;
            this.Polygonrotation = polygonrotation;
            this.Polygontype = polygontype;
            Casttoenum(this.Polygontype);
        }
    }

    public enum Polygontypes
    {
        PolyState1 = 1,
        PolyState2 = 2,
        PolyState3 = 3
    }

    private Polygontypes currentPolygontype;

    public void Casttoenum(int state)
    {
       currentPolygontype = (Polygontypes)state;                                     
       WhichPolygon(currentPolygontype);
    }

    public List<Vertices> Polyglist = new List<Vertices>();

    public void WhichPolygon(Polygontypes polyliststate)
    {
        switch (polyliststate)
        {
            case Polygontypes.PolyState1:
                Polyglist = list1;
                break;
            case Polygontypes.PolyState2:
                Polyglist = list2;
                break;
            case Polygontypes.PolyState3:
            Polyglist = list3;
                break;
        }
    }
4

1 回答 1

0

在方法声明中,而不是直接采用 int take Polygontypes (enum) ,因此可以避免强制转换。

更改 PolygonContainer() 如下:

public PolygonContainer(float polygonpositionx, float polygonpositiony, float polygonrotation, Polygontypes polygontype)

然后删除以下用于强制转换为枚举的语句:

this.Polygontype = polygontype; 
Casttoenum(this.Polygontype);

在调用 PolygonContainer() 函数时,必须将枚举值作为 int 的最后一个参数传递,这样就不能传递无效值。

于 2013-11-01T12:53:06.183 回答