
{"id":365,"date":"2009-12-11T16:02:01","date_gmt":"2009-12-11T21:02:01","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=365"},"modified":"2009-12-11T16:02:01","modified_gmt":"2009-12-11T21:02:01","slug":"parsing-net-enums","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=365","title":{"rendered":"Parsing .NET Enums"},"content":{"rendered":"<p>I needed the following functionality:<br \/>\n&#8211; class MarketSession has property MarketType (options, futures, &#8230;)<br \/>\n&#8211; this property is an enum, but in the underlying protocol it is actually a number<br \/>\n&#8211; in my Spring.NET configuration I wanted to specify a string which would be either a symbolic name or a number; one would use symbolic names for defined market types and numbers for the market types that did not make it yet into the enum<\/p>\n<p>I was pleasantly surprised to find out, that the built-in <code>Enum.Parse()<\/code> provides exactly what I need. You can give it either a symbolic name, or a numeric value, and it will work fine, even if the value is out of range.<\/p>\n<div style=\"border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt\">\n<div style=\"background-color: #ffffff; overflow: auto; padding: 2px 5px;\"><span style=\"color:#0000ff\">enum<\/span> <span style=\"color:#2b91af\">MarketType<\/span><br \/>\n{<br \/>\n&#160;&#160;&#160;&#160;Futures = 10,<br \/>\n&#160;&#160;&#160;&#160;Options = 20<br \/>\n}<\/p>\n<p><span style=\"color:#0000ff\">public<\/span> <span style=\"color:#0000ff\">class<\/span> <span style=\"color:#2b91af\">MyClass<\/span><br \/>\n{<br \/>\n&#160;&#160;&#160;&#160;MarketType _marketType;<\/p>\n<p>&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">public<\/span> <span style=\"color:#0000ff\">string<\/span> MarketType<br \/>\n&#160;&#160;&#160;&#160;{<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">get<\/span> { <span style=\"color:#0000ff\">return<\/span> _marketType.ToString(); }<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">set<\/span> { _marketType = Parse&lt;MarketType&gt;(<span style=\"color:#0000ff\">value<\/span>); }<br \/>\n&#160;&#160;&#160;&#160;}<\/p>\n<p>&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">private<\/span> <span style=\"color:#0000ff\">static<\/span> T Parse&lt;T&gt;(<span style=\"color:#0000ff\">string<\/span> s)<br \/>\n&#160;&#160;&#160;&#160;{<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span style=\"color:#0000ff\">return<\/span> (T)Enum.Parse(<span style=\"color:#0000ff\">typeof<\/span>(T), s);<br \/>\n&#160;&#160;&#160;&#160;}<br \/>\n}<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I needed the following functionality: &#8211; class MarketSession has property MarketType (options, futures, &#8230;) &#8211; this property is an enum, but in the underlying protocol it is actually a number <a href=\"https:\/\/ikriv.com\/blog\/?p=365\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[8],"tags":[],"class_list":["entry","author-ikriv","post-365","post","type-post","status-publish","format-standard","category-cs"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=365"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}