none
c# class design - how to throw exception when user sets unallowable value RRS feed

  • Question

  • Here is my class:

    public class HubBasicProfile
     {
         enum allowed_statuses {Active, Inactive, Completed};
     
        public int Id { get; set; }
         ... snip ...
     
        private String _Status;
         public String Status {
             get
             {
                 return this._Status;
             }
             set
             {
                 if (value == null || value.Equals(String.Empty))
                 {
                     this._Status = "Active";
                 }
                 else
                 {
                     // IF status value in allowed_statuses enum
                     this._Status = value;
                     // ELSE throw exception
                 }
             }
         }
         ... Snip constructors ...
     }

    When a user tries to set a Status value that is unacceptable, how do I throw ArgumentOutOfRangeException? 

    I tried this:

    if (Enum.IsDefined(allowed_statuses, value))
     {
         this._Status = value;
     }
     else
     {
         throw new ArgumentOutOfRangeException("'" + value + "' is not an acceptable value for Status.");
     }
     

    but then get these 3 errors:

    1. 'HubBasicProfile.allowed_statuses' is a 'type' but is used like a 'variable'
    2. The best overloaded method match for 'System.Enum.IsDefined(System.Type, object)' has some invalid arguments
    3. Argument 1: cannot convert from 'HubBasicProfile.allowed_statuses' to 'System.Type'


    So, I need help figuring this out.

    Thanks.

    Eric




    Thursday, October 25, 2012 3:02 PM

Answers