/ Published in: C#
This snippet accepts a string which is assumed to be the string version of one of the members and returns an enum object with the respective member selected.*
*Thanks to user: mcbutterbuns for improving upon my version of this snippet
Expand |
Embed | Plain Text
public enum PayTypeList { Unknown, Hourly, Salary, } // Throws an ArgumentException if the string is found not to be one of the members of the enum (in this case, if you pass in a string "Weekly" instead of "Hourly", it will throw an ArgumentException)
Comments
Subscribe to comments
You need to login to post a comment.

There is a simpler way than this snip:
(PayTypeList)Enum.Parse(typeof(PayTypeList), "Hourly");
Will through an ArgumentException (I believe) if the string is cannot be parsed into the type of enum requested.
cool! it works and throws the ArgumentException as you said. Thanks for the tip!