3

For my program, I created a new class called FinishedPiece with a number of public variables available to my main program. For example:

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

This all works fine, because then I can declare a new FinishedPiece and add properties:

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

My question is, how do the same with an enum? If I do

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

then I'd like to change it something like this: piece.Cut = Cut.Angle; but I can only change it by declaring a new FinishedPiece.Cut object:

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

How do I make an enum available inside a variable so I can do piece.Cut = Cut.Angle? To me it would make sense to do something like this, but it doesn't appear to work.

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

}

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

Thanks in advance! Let me know if my question is unclear and I'll try to help as best as I can.

2
  • 3
    FinishedPiece doesn't contain a public variable. It contains a private variable, which backs a public property. Commented Oct 9, 2013 at 13:36
  • _Cut is not an int. The public and private must be the same type Commented Oct 9, 2013 at 13:40

3 Answers 3

10

How do I make an enum available inside a variable so I can do piece.Cut = Cut.Angle?

Just define another property of type Cut in your class like:

public Cut Cut { get; set; }

Then you can do:

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

So your class would like like:

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

    public Cut Cut { get; set; }
}

Consider using Auto-Implemented properties, if you have only simple set and get

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for beating me to the punch by minutes and not seconds, and for suggesting AIPs. :0
Thank you for your answer. And I took your advice about AIPs. Good to know! I always like to shorten my code any way that I can and that certainly does.
@gnarlybracket, you are welcome, just remember that Auto implemented properties use a private backing field internally. But it does shorten the code
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;

Comments

0

You can try this:

    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; }
        }
    }

Then in your calling code...

FinishedPiece piece = new FinishedPiece();
//if you dont want the default change it...
piece.CutObj = Cut.Straight;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.