-2

I am trying to make a contact class for my contact managers application and I need to have email have to contain 1 and only 1 @ symbol and make sure the phone number is in the format 123-456-7890 but I am not sure how to do this.

Any guidance would be appreciated

class Contact
{
    class Contact
    {

        //private member variables
        private String _firstName;
        private String _lastName;
        private Type _contactTypes;
        private String _phoneNumber;
        private String _emailAddress;




        //Public constructor that takes five arguments
        public Contact()
        {
            //Call the appropriate setter (e.g. FirstName) to set the member variable value
            /* GetFirstName = firstName;
             GetLastName = lastName;
             ContactTypes = contactTypes;
             GetPhoneNumber = phoneNumber;
             GetEmailAddress = emailAddress;*/

        }


        /*********************************************************************
         * Public accessors used to get and set private member variable values
         *********************************************************************/
        //Public  ContactTypes accessor
        public Type ContactTypes
        {
            get
            {
                //Return member variable value
                return _contactTypes;
            }
            set
            {
                //Validate value and throw exception if necessary
                if (value == null)
                    throw new Exception("ContactType must have a value");
                else
                    //Otherwise set member variable value*/
                    _contactTypes = value;
            }
        }
        enum ContactTypesEnum { Family, Friend, Professional }
        //Public FirstName accessor: Pascal casing
        public String GetFirstName
        {
            get
            {
                //Return member variable value
                return _firstName;
            }
            set
            {
                //Validate value and throw exception if necessary
                if (value == "")
                    throw new Exception("First name must have a value");
                else
                    //Otherwise set member variable value
                    _firstName = value;
            }
        }

        //Public LastName accessor: Pascal casing
        public String GetLastName
        {
            get
            {
                //Return member variable value
                return _lastName;
            }
            set
            {
                //Validate value and throw exception if necessary
                if (value == "")
                    throw new Exception("Last name must have a value");
                else
                    //Otherwise set member variable value
                    _lastName = value;
            }
        }



        //Public PhoneNumber accessor
        public String GetPhoneNumber
        {
            get
            {
                //Return member variable value
                return _phoneNumber;
            }
            set
            {
                bool isValid = Regex.IsMatch(value, @"/d{3}-/d{3}-/d{4}"); 
                //Validate value and throw exception if necessary
                if (value == "")
                    throw new Exception("PhoneNumber must have a value");
                else
                    //Otherwise set member variable value
                    _phoneNumber = value;
            }
        }



        //Public Email accessor
        public String GetEmailAddress
        {
            get
            {
                //Return member variable value
                return _emailAddress;
            }
            set
            {
                //Validate value and throw exception if necessary
                if (value == "")
                    throw new Exception("EmailAddress must have a value");
                else
                    //Otherwise set member variable value
                    _emailAddress = value;
            }
        }

    }
}
arron187
  • 1
  • 2
  • You really shouldn't be throwing exceptions when you get bad values. Exceptions should be for exceptional situations like running out of hard drive space or connections dropping out. Invalid input should be handle using data validation techniques - either pre-validate before assignment or implement `IDataErrorInfo` on your object. – Enigmativity Jul 29 '15 at 01:24

1 Answers1

0

I agree with the comment from @Enigmativity - it's not a good idea to throw exceptions in your accessor set functions. That being said, you're already halfway there with your isValid regular expression test in GetPhoneNumber.

set
{
    bool isValid = Regex.IsMatch(value, @"/d{3}-/d{3}-/d{4}"); 
    if (isValid) // Set member variable value
        _phoneNumber = value;
}

You can either do a similar regular expression test for your email or use System.Net.Mail.MailAddress class. See this SO response.

Community
  • 1
  • 1
Thane Plummer
  • 7,966
  • 3
  • 26
  • 30