1

I have a class like this:

public class Meta
{
    public string Height { get; set; }
}

I would like to add some things to the class but I'm not sure how to do it. What I would like is for the Height to only be able to be set to either "Tall" or "Short". Maybe more things in the future but for now it would be a choice just between those two. Also I would like it to default to "Short" in the constructor. I think I would need to use an Enums but I am not sure how to do this.

Could someone explain. I would very much appreciate.

Jalal Said
  • 15,906
  • 7
  • 45
  • 68
RichardA
  • 329
  • 1
  • 3
  • 8
  • Do you what is enum in C# is ? if not please take a look in this [article](http://msdn.microsoft.com/en-us/library/sbbt4032(v=VS.100).aspx) – guyl Jul 10 '11 at 11:27

3 Answers3

9

Yes, you could use an enum:

public enum Height
{
    Short = 0,
    Tall = 1;
}

public class Meta
{
    public Height Height { get; private set; }

    public Meta(Height height)
    {
        if (!Enum.IsDefined(typeof(Height), height))
        {
            throw new ArgumentOutOfRangeException("No such height");
        }
        this.Height = height;
    }
}

(If you want the property to be writable, you need to put the validation in the setter.)

You need the validation because enums are really just differently-typed integer values. For example, without the validation this would proceed fine:

new Meta((Height) 1000);

but it would obviously be meaningless to any callers.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Pardon my ignorance but why the check on the `enum` argument. Isn't it enough that it's strongly typed? – InBetween Jul 10 '11 at 11:57
  • @InBetween: No, it's not. See the bit at the end of the answer - we *don't* want to allow `(Height) 1000` as a property value, but nothing within the normal infrastructure would prevent it. – Jon Skeet Jul 10 '11 at 12:24
  • Oh wow, I actaully thought (Height)1000 would throw which IMHO should be the expected behavior as Height does not define the value. Kind of confused why that cast is allowed. – InBetween Jul 10 '11 at 12:36
4

You could define an enum with the possible values:

public enum HeightTypes
{
    Tall,
    Short
}

and then use this as the type of the Height property:

public class Meta
{
    public Meta()
    {
        // Set the Height property to Short by default in the constructor
        Height = HeightTypes.Short;
    }
    public HeightTypes Height { get; set; }
}

Now when you have an instance of the Meta class you could set its Height property only to Tall or Short:

var meta = new Meta();
meta.Height = HeightTypes.Tall;
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Minor note (mainly to the OP): enum values are not automatically validated; you can assign other values that are neither Tall nor Short – Marc Gravell Jul 10 '11 at 11:38
2

Define an Enum.

public Enum Heights
{
    Tall,
    Short
}

and then define your property to be of type enum

public Heights Height { get; set; }

see http://msdn.microsoft.com/en-us/library/system.enum.aspx for more info

tkerwood
  • 1,875
  • 16
  • 26