gna*_*ket 3 .net c# variables enums class
对于我的程序,我创建了一个新类,FinishedPiece
其中包含许多可用于我的主程序的公共变量.例如:
class FinishedPiece
{
private double _PieceLength;
public double PieceLength
{
get { return _PieceLength; }
set { _PieceLength = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,因为那时我可以声明一个新的FinishedPiece
并添加属性:
FinishedPiece piece = new FinishedPiece();
piece.PieceLength = 48.25;
Run Code Online (Sandbox Code Playgroud)
我的问题是,怎么做同样的enum
?如果我做
public enum Cut
{
Angle = 0,
Straight = 1,
AngleThenStraight = 2,
StraightThenAngle = 3
};
Run Code Online (Sandbox Code Playgroud)
然后我想改变这样的事情:piece.Cut = Cut.Angle;
但我只能通过声明一个新FinishedPiece.Cut
对象来改变它:
FinishedPiece.Cut cut = new FinishedPiece.Cut();
cut = FinishedPiece.Cut.Angle;
Run Code Online (Sandbox Code Playgroud)
如何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
};
Run Code Online (Sandbox Code Playgroud)
提前致谢!如果我的问题不清楚,请告诉我,我会尽力帮助.
Hab*_*bib 10
如何在变量中创建一个枚举,这样我才能做到.Cut = Cut.Angle?
只需Cut
在类中定义另一个类型属性,如:
public Cut Cut { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
FinishedPiece piece = new FinishedPiece();
piece.PieceLength = 48.25;
piece.Cut = Cut.Angle; //like this
Run Code Online (Sandbox Code Playgroud)
所以你的班级喜欢:
class FinishedPiece
{
private double _PieceLength;
public double PieceLength
{
get { return _PieceLength; }
set { _PieceLength = value; }
}
public Cut Cut { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果您只有简单和,请考虑使用自动实现的属性set
get