{"id":5,"date":"2007-04-28T02:10:52","date_gmt":"2007-04-27T22:10:52","guid":{"rendered":"http:\/\/blog.eweibel.net\/?p=5"},"modified":"2007-04-28T15:08:29","modified_gmt":"2007-04-28T13:08:29","slug":"programming-several-conditions-in-c","status":"publish","type":"post","link":"https:\/\/blog.eweibel.net\/?p=5","title":{"rendered":"Programming several conditions in C#"},"content":{"rendered":"<p>When you have to programme a piece of code which have several conditions you could do it in the following way: <\/p>\n<p dir=\"ltr\" style=\"MARGIN-RIGHT: 0px\"><code>switch(a.name)<br \/><\/code><code>{<br \/><\/code><code>&nbsp;&nbsp;&nbsp; case \"smith\":<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CallHim(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code><code>break;<br \/><\/code><code>&nbsp;&nbsp;&nbsp; case \"doyle\":<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SendAMessageToHim();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br \/>&nbsp;&nbsp;&nbsp; default:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new WriteNotReachableErrorException();<br \/>}<\/code><\/p>\n<p dir=\"ltr\" style=\"MARGIN-RIGHT: 0px\"><code><\/code>If you couldn&#8217;t use constants as conditions, you will implement the logic in the following way: <\/p>\n<p dir=\"ltr\" style=\"MARGIN-RIGHT: 0px\"><code>if(a.name == smithObject.ToString())<br \/>{<br \/><\/code><code>&nbsp;&nbsp;&nbsp; CallHim();<br \/>}<br \/>else if(a.name == doyleObject.ToString())<br \/>{<br \/>&nbsp;&nbsp;&nbsp; SendAMessageToHim();<br \/>}<br \/>else<br \/>{<br \/>&nbsp;&nbsp;&nbsp; throw new WriteNotReachableErrorException();<br \/>}<\/code><\/p>\n<p dir=\"ltr\" style=\"MARGIN-RIGHT: 0px\"><code><\/code>But if you think now there is now other possibility to implement the logic, wait! I found recently following code which is doing exactly the same thing:<\/p>\n<p dir=\"ltr\" style=\"MARGIN-RIGHT: 0px\"><code>for(;;)<br \/>{<br \/>&nbsp;&nbsp;&nbsp; if(a.name == smithObject.ToString())<br \/><\/code><code>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CallHim();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br \/>&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; if(a.name == doyleObject.ToString())<br \/>&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SendAMessageToHim();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br \/>&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; throw new WriteNotReachableErrorException();<br \/>}<\/code><\/p>\n<p dir=\"ltr\" style=\"MARGIN-RIGHT: 0px\"><code><\/code>I&#8217;m not sure if the last version is comprehensible and maintainable. I will go even further, it&#8217;s bad code, because there are better constructs (else if) for doing the job, so we don&#8217;t need an approximation workaround for the switch-case statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you have to programme a piece of code which have several conditions you could do it in the following way: switch(a.name){&nbsp;&nbsp;&nbsp; case &#8220;smith&#8221;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CallHim(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;&nbsp; case &#8220;doyle&#8221;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SendAMessageToHim();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;&nbsp; default:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new WriteNotReachableErrorException();} If you couldn&#8217;t use constants as conditions, you will implement the logic in the following way: if(a.name == smithObject.ToString()){&nbsp;&nbsp;&nbsp; CallHim();}else if(a.name == doyleObject.ToString()){&nbsp;&nbsp;&nbsp; SendAMessageToHim();}else{&nbsp;&nbsp;&nbsp; throw new WriteNotReachableErrorException();} But if you think now there is now other possibility to implement the logic, wait! I found recently&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/blog.eweibel.net\/?p=5\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-5","post","type-post","status-publish","format-standard","hentry","category-good-practices"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/plOV9-5","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1248,"url":"https:\/\/blog.eweibel.net\/?p=1248","url_meta":{"origin":5,"position":0},"title":"VDD &#8211; the new programming manifesto?","author":"Patrick","date":"17. Jul 2012","format":false,"excerpt":"When I was at the NDC, I had also the possibility to visit with colleagues the city of Oslo. During a stop in front of a little shop, a colleague discovered a post card about Viking laws. When I read it, I was really surprised how well the laws fit\u2026","rel":"","context":"In &quot;Agile&quot;","block_context":{"text":"Agile","link":"https:\/\/blog.eweibel.net\/?cat=17"},"img":{"alt_text":"VikingLaws","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/VikingLaws_thumb1.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1132,"url":"https:\/\/blog.eweibel.net\/?p=1132","url_meta":{"origin":5,"position":1},"title":"DataSet and deleted rows","author":"Patrick","date":"31. Jan 2012","format":false,"excerpt":"Yes, I know, the DataSet isn't the leading edge technology, but as the company where I work currently, there are several companies who use DataSet as data access technology. Recently, I had to migrate the DataSet subclasses of the framework of my current employer to .NET 4.0 and add LINQ\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"Pair of cubic eggs","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Fotolia_23504153_S_thumb.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":487,"url":"https:\/\/blog.eweibel.net\/?p=487","url_meta":{"origin":5,"position":2},"title":"WordPress and SyntaxHighlighter","author":"Patrick","date":"8. Jan 2010","format":false,"excerpt":"I often post code in my blog posts, so the SyntaxHighlighter by is very useful. But there are several plug-ins for WordPress, the question is which one is the most comfortable and has the most configuration options: Google Syntax Highlighter for Wordpress Syntax Highlighter and Code Colorizer for Wordpress SyntaxHighlighter\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":61,"url":"https:\/\/blog.eweibel.net\/?p=61","url_meta":{"origin":5,"position":3},"title":"Fall-thru or not to fall through","author":"Patrick","date":"3. Mar 2009","format":false,"excerpt":"There is a little differences how C# and Java implement the switch-statement. I discovered it when I want to implement a fall through in C#. In Java it is very simple: just leave the break statement at the end of the case block away. In C# it doesn't work like\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":622,"url":"https:\/\/blog.eweibel.net\/?p=622","url_meta":{"origin":5,"position":4},"title":"Branching practices","author":"Patrick","date":"6. Apr 2010","format":false,"excerpt":"In an environment where several developers work at the same code base or several features have to be implemented, then branching will be a topic. I was asked to create a branching guide for my current employer which use TFS as source code repository. Motivation In our projects I found\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/image_thumb3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/image_thumb3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/image_thumb3.png?resize=525%2C300 1.5x"},"classes":[]},{"id":76,"url":"https:\/\/blog.eweibel.net\/?p=76","url_meta":{"origin":5,"position":5},"title":"Code generation for unit testing","author":"Patrick","date":"10. Mar 2009","format":false,"excerpt":"There exists a project at Microsoft which generate unit tests based of source code. The name of this interesting project is Pex.At the first time when I heard about this project I was skeptical. Where's the value of generated tests? Doesn't it break the \"test first\" approach? After I saw\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/5","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5"}],"version-history":[{"count":0,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/5\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}