3

对于我的程序,我创建了一个名为的新类FinishedPiece,其中包含许多可用于我的主程序的公共变量。例如:

class FinishedPiece
{
    private double _PieceLength;
    public double PieceLength
    {
         get { return _PieceLength; }
         set { _PieceLength = value; }
    }
}

这一切都很好,因为我可以声明一个新的FinishedPiece并添加属性:

FinishedPiece piece = new FinishedPiece();
piece.PieceLength = 48.25;

我的问题是,如何与 a 一样enum?如果我做

public enum Cut
{
    Angle = 0,
    Straight = 1,
    AngleThenStraight = 2,
    StraightThenAngle = 3
};

然后我想像这样改变它:piece.Cut = Cut.Angle;但我只能通过声明一个新FinishedPiece.Cut对象来改变它:

FinishedPiece.Cut cut = new FinishedPiece.Cut();
cut = FinishedPiece.Cut.Angle;

如何enum在变量内部提供一个可用的,以便我可以做到piece.Cut = Cut.Angle?对我来说,做这样的事情是有意义的,但它似乎不起作用。

public int Cut
{
    get { return _Cut; }
    set { _Cut = value; }

}

private enum _Cut
{
    Angle = 0,
    Straight = 1,
    AngleThenStraight = 2,
    StraightThenAngle = 3
};

提前致谢!如果我的问题不清楚,请告诉我,我会尽我所能提供帮助。

4

3 回答 3

10

如何在变量中提供枚举,以便我可以执行piece.Cut = Cut.Angle?

Cut只需在您的类中定义另一个类型的属性,例如:

public Cut Cut { get; set; }

然后你可以这样做:

FinishedPiece piece = new FinishedPiece();
piece.PieceLength = 48.25;
piece.Cut = Cut.Angle; //like this

所以你的班级会喜欢:

class FinishedPiece
{
    private double _PieceLength;
    public double PieceLength
    {
        get { return _PieceLength; }
        set { _PieceLength = value; }
    }

    public Cut Cut { get; set; }
}

考虑使用自动实现的属性,如果你只有简单setget

于 2013-10-09T13:37:07.893 回答
2

Like this:

class FinishedPiece
{
    private double _PieceLength;
    public double PieceLength
    {
        get { return _PieceLength; }
        set { _PieceLength = value; }
    }
    private Cut _Cut;
    public Cut Cut
    {
        get { return _Cut; }
        set { _Cut = value; }
    }
}
public enum Cut
{
    Angle = 0,
    Straight = 1,
    AngleThenStraight = 2,
    StraightThenAngle = 3
};

Then you can do:

var piece = new FinishedPiece();
piece.Cut = Cut.AngleThenStraight;
于 2013-10-09T13:39:53.030 回答
0

你可以试试这个:

    private enum Cut
    {
        //if you dont want to use any of these values as defaults 
        //just add another value and in your class private member
        //assign it like for example a value called None
        Angle = 0,
        Straight = 1,
        AngleThenStraight = 2,
        StraightThenAngle = 3
    };

    public class FinishedPiece
    {
        //give it a default,if like stated in the enum you dont want
        //any of those values create a None and place it here as default.
        private Cut cutObj = Cut.Angle;

        public Cut CutObj
        {
            get { return cutObj; }
            set { cutObj = value; }
        }
    }

然后在你的调用代码中......

FinishedPiece piece = new FinishedPiece();
//if you dont want the default change it...
piece.CutObj = Cut.Straight;
于 2013-10-09T13:44:41.490 回答