类const C#中的令牌无效

时间:2012-10-17 18:44:17

标签: c#

我知道此问题已在此处提出,但我无法找到我的代码有什么问题。

      class Slider
     {
        public int const DEFAULT_SIZE = 20; // Problem is here. Invalid token in class 

    private int rise { get; set; }
    private int run { get; set; }
    private int size { get; set; }

    int positionX = 0;
    int positionY = 0;

    private int leftBoundX { get; set;}
    private int leftBoundY { get; set; }

    private int rightBoundX { get; set; }
    private int rightBoundY { get; set; }

    // Constructor
    Slider()
    {
        size = DEFAULT_SIZE; 
    }

    private void Bound()
    {
        if (positionX > leftBoundX)
            positionX = rightBoundX;
        else if (positionY > leftBoundY)
            positionY = rightBoundY;
        else if (positionX > leftBoundX)
            positionX = rightBoundX;
        else if (positionX > leftBoundX)
            positionX = rightBoundX;
        }

我google了一些东西,他们告诉我要包含System.Collection我做了但仍然是同样的错误

1 个答案:

答案 0 :(得分:7)

const修饰符必须之前常量的类型。你想要:

public const int DEFAULT_SIZE = 20;

或遵循.NET命名约定:

public const int DefaultSize = 20;