vendredi 28 avril 2017

When I make conditions within properties, Do I have to make same conditions in Custom constructor again?

My full question is:
When I make conditions within properties, Do I have to make same conditions in Custom constructor again or I can somehow use Property Custom constructor?

If i have code like this:

class Program
{
    struct Student
    {
        private int _id;
        private string _name;
        private int _age;

        public int ID // Property
        {
            get
            {
                return _id;
            }
            set
            {
                if (value <= 0)
                {
                    Console.WriteLine("You cannot assign id less then 1");
                    Console.ReadLine();
                    Environment.Exit(0);
                }
                else
                {
                    _id = value;
                }
            }
        }

        public string NAME // Property
        {
            get
            {
                return _name;
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    _name = "No Name";
                }
                else
                {
                    _name = value;
                }
            }
        }

        public int AGE // Property
        {
            get
            {
                return _age;
            }
            set
            {
                if(value <= 0)
                {
                    Console.WriteLine("Your age cannot be less then 1 year.");
                    Console.ReadLine();
                    Environment.Exit(0);
                }
                else
                {
                    _age = value;
                }
            }
        }

        public Student(int initID, string initName, int initAge) // Defining custom constructor
        {
            if (initID <= 0)
            {
                Console.WriteLine("You cannot assign id less then 1");
                Console.ReadLine();
                Environment.Exit(0);
            }
            if (String.IsNullOrEmpty(initName))
            {
                _name = "No Name";
            }

            if (initAge <= 0)
            {
                Console.WriteLine("Your age cannot be less then 1 year.");
                Console.ReadLine();
                Environment.Exit(0);
            }
            _id = initID;
            _name = initName;
            _age = initAge;
        }

        public void Status() // struct member - method
        {
            Console.WriteLine("ID: {0}", _id);
            Console.WriteLine($"Name: {_name}");
            Console.WriteLine($"Age: {_age}\n");
        }


    }

    static void Main(string[] args)
    {
        Student s1 = new Student(1, "James", 10);
        s1.Status(); 
        Console.ReadLine();
    }
}

As you can see I set some conditions within property like ID cannot be 0 and less then 0, but when I want to use Custom constructor, I have to make this condition again. It is only way how to do this? or is some another way?

Are even custom constructors used with encapsulation and when you have properties?

Thank you for your answers. :)

Aucun commentaire:

Enregistrer un commentaire