Menu

The MailPriority class

Weber Wang
2004-01-21
2004-01-21
  • Weber Wang

    Weber Wang - 2004-01-21

    Why use priority as string? I think it's not security or must have more code to check it in other class(such as MailMessage), you can use enum for simple, and it is more safe and easy to use, or can use a more complex way called typesafed enum(descripted in Effective Java), the code just like this:
        public sealed class MailPriority
        {
            private MailPriority(string title, int value)
            {
                this.title = title;
                this.value = value;
            }

            public string Title
            {
                get
                {
                    return title;
                }
            }

            public int Value
            {
                get
                {
                    return value;
                }
            }

            public override string ToString()
            {
                return title;
            }

            private readonly string title;
            private readonly int value;

            public static readonly MailPriority Highest = new MailPriority("Highest", 1);
            public static readonly MailPriority High = new MailPriority("High", 2);
            public static readonly MailPriority Normal = new MailPriority("Normal", 3);
            public static readonly MailPriority Low = new MailPriority("Low", 4);
            public static readonly MailPriority Lowest = new MailPriority("Lowest", 5);
        }
    This is a sealed class and the instance of this class is readonly object, so needn't thinking of multithread problem and needn't clone object, it's more safe, and easy to use in MailMessage class.

     
    • Weber Wang

      Weber Wang - 2004-01-21

      And you can provide a method to get all priorities, like this:
              public static MailPriority[] GetValues()
              {
                  MailPriority[] ps = new MailPriority[]
                      {
                          Highest,
                          High,
                          Normal,
                          Low,
                          Lowest,
                      };
                  return ps;
              }
      There's no problem of performance, because all fields are singleton, all the cost is the new MailPriority[], and if you attach importance to performance, you can cache it because it will not changed and the Array is readonly object too.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.