Forcing Available Property Selections

Posted on 2009-12-18
Last Modified: 2013-12-17
I am not beginning work with custom controls and am fairly new to C#. I am fine setting and getting all of my current properties... what I am wondering is is it possible to pre-assign some variables associated with a property? Meaning if I have a Human Class and one of the properties is HAIRCOLOR... how do I limit the options to a few colors. For instance

Human human = new Human();
human.haircolor = Color.Black //Is Valid
human.haircolor = Color.Brown //Is Valid

No other colors are valid...
Question by:Pretzel_Jesus
    LVL 30

    Expert Comment

    by:Reza Rad
    You can set your validation in Set Method of your property. for example:
    private Color _haircolor;
    public Color HairColor
    if(value==Color.Black or value=Color.Brown)
    throw new exception("value is not valid");
    return _haircolor;
    LVL 85

    Accepted Solution

    You could also use an Enum:
        public partial class Form1 : Form
            public Form1()
            private void button1_Click(object sender, EventArgs e)
                Human human = new Human();
                human.HairColor = Human.HairColors.Black;
        public class Human
            private HairColors _HairColor = HairColors.Black;
            public enum HairColors
                Black = 0,
                Brown = 1
            public HairColors HairColor
                get { return _HairColor; }
                set { _HairColor = value; }               

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now