如何从PolygonContainer中调用公共空白?

时间:2013-11-01 12:39:03

标签: c# xna

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

  Casttoenum(this.Polygontype);
  

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

有什么问题?如何从我的PolygonContainer中调用公共虚空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;
        }
    }

1 个答案:

答案 0 :(得分:0)

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

更改PolygonContainer(),如下所示:

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

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

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

在调用PolygonContainer()函数时,必须将枚举值作为int的最后一个参数传递,以便无法传递无效值。