加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

公共变量.NET中的C#枚举

发布时间:2020-12-16 00:09:38 所属栏目:百科 来源:网络整理
导读:对于我的程序,我创建了一个名为FinishedPiece的新类,其中包含许多可用于我的主程序的公共变量.例如: class FinishedPiece{ private double _PieceLength; public double PieceLength { get { return _PieceLength; } set { _PieceLength = value; } }} 这一
对于我的程序,我创建了一个名为FinishedPiece的新类,其中包含许多可用于我的主程序的公共变量.例如:

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

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

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

我的问题是,如何使用枚举?如果我做

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;

如何在变量中创建一个枚举,这样我才能做到.Cut = Cut.Angle?对我来说,做这样的事情是有道理的,但它似乎不起作用.

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

}

private enum _Cut
{
    Angle = 0,StraightThenAngle = 3
};

提前致谢!如果我的问题不清楚,请告诉我,我会尽力帮助.

解决方法

How do I make an enum available inside a variable so I can do
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; }
}

如果您只有简单的设置和获取,请考虑使用Auto-Implemented properties

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读