I don't get exceptions when I enter invalid inputs

I have a problem with my code. When I enter an invalid input it should give me an exception but it doesn't, instead it returns me the value as if its correct. Here is my class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;

namespace Lecture_1_Homework
{
    public class Person
    {
        private string name;
        private int age;
        private string email;

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (!ValidateName(name))
                {
                    throw new ArgumentException("You need to enter your name.");
                }
                this.name = value;
            }
        }
        public int Age
        {
            get
            {
                return this.age;
            }
            set
            {
                if (!ValidateAge(age))
                {
                    throw new ArgumentException("Your age must be between 1 and 100");
                }
                this.age = value;
            }
        }

        public string Email
        {
            get
            {
                return this.email;
            }
            set
            {
                if (!ValidateEmail(email))
                {
                    throw new ArgumentException("Invalid email input");
                }
                this.email = value;
            }
        }



        private bool ValidateName(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return false;
            }
            return true;
        }
        private bool ValidateAge(int age)
        {
            if (age > 100 || age < 1)
            {
                return false;
            }
            return true;
        }
        private bool ValidateEmail(string email)
        {
            try
            {
                MailAddress mail = new MailAddress(email);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
        public Person(string name, int age, string email)
        {
            this.name = name;
            this.age = age;
            this.email = email;
        }
        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }
        public void ToString()
        {
            Console.Write("Name:{0}\nAge:{1}\nEmail:{2}\n",this.name,this.age,string.IsNullOrWhiteSpace(this.email) ? "no email given" : this.email);
        }
    }
}

Here is my main

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lecture_1_Homework
{
    class Persons_main
    {
        static void Main()
        {
            Person Peter = new Person("Peter", 18, "test123@yahoo.com");
            Person Jake = new Person("Jake", 19);
            Peter.ToString();
            Jake.ToString();
        }
    }
}

If I enter an number greater than 100 or lower than 1 it should give me an exception but it doesn't.

if I don't enter a name it should give me an exception but it doesn't.

And if I enter a wrong format for my email it should give me and exception but it doesn't.

For example if I enter for my email "test123mail.com" this should give me an exception but it doesn't.

And if I enter a " " for my name it should give me an exception but it doesn't.

I can't find my mistake and I would appreciate some help.

Answers


Two things...

First of all, you're validating the variable before it's been set:

ValidateName(name)

I think you want to validate the incoming value for the property instead:

ValidateName(value)

Second, you're setting the backing variables directly without validating them:

this.name = name;

The validation is in the property setter, so use that instead:

this.Name = name;

you need to validate the value being passed in, not the property.

ValidateName(value)

instead of

ValidateName(name)

And when assigning the value to the property, use

this.Name = value; // instead of this.name

Need Your Help

Laravel Integration with Webix Datatable Editing

php mysql database laravel-5.1 webix

I have a site that's running Laravel 5 and I'd like to create a Webix datatable within it. My understanding of Webix datatables is that these tables can be set to load data from Laravel database by...

How to send an IP packet with given hex payload in Python?

python sockets igmp

I have sniffed an IGMP packet and now I would like to send it with the help of python. Is there any way to just send packet like