?
Solved

Forcing Available Property Selections

Posted on 2009-12-18
2
Medium Priority
?
256 Views
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...
0
Comment
Question by:Pretzel_Jesus
2 Comments
 
LVL 30

Expert Comment

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

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 26085936
You could also use an Enum:
http://msdn.microsoft.com/en-us/library/sbbt4032(VS.80).aspx
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        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

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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