Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a few ways you could go about improving your current design. I don't agree that the solution I am about to present to you is the ideal solution, but given that you have already identified that you would like to use an <code>enum</code> I think you will be content with this solution. </p> <p>What you can do is define an <code>enum</code> marked with the <code>[Flags]</code> attribute. Assign to each flag in the enum a value that is a power of two. If you want to combine options to create a single option use <a href="http://msdn.microsoft.com/en-gb/library/kxszd0kx%28v=vs.71%29.aspx" rel="nofollow">bitwise or</a> just like I did with the flag named "All" in the proceeding sample: </p> <pre><code>[Flags] public enum GetItemOptions { Read = 0x1, Unread = 0x2, All = 0x1 | 0x2, SortByOldest = 0x4, SortByLatest = 0x8 } </code></pre> <p>From your code sample, the first call will now look like this: </p> <pre><code>GetItemsBasedOn(GetItemOptions.Unread | GetItemOptions.SortByLatest); </code></pre> <p>And the second will look like this: </p> <pre><code>GetItemsBasedOn(GetItemOptions.All); </code></pre> <p>In order to enable this design you will need to adjust your <code>GetItemsBasedOn</code> method signature so that it specifies an argument of the <code>GetItemOptions</code> enum type. Below is an example of how you can handle different settings. </p> <pre><code>public static void GetItemsBasedOn(GetItemOptions getItemOption) { if (getItemOption.HasFlag(GetItemOptions.SortByOldest) &amp;&amp; getItemOption.HasFlag(GetItemOptions.SortByLatest)) throw new ArgumentException("I can't sort by both..."); if (getItemOption.HasFlag(GetItemOptions.Read)) { Console.WriteLine("READ"); } if (getItemOption.HasFlag(GetItemOptions.Unread)) { Console.WriteLine("UNREAD"); } if (getItemOption.HasFlag(GetItemOptions.SortByOldest)) { Console.WriteLine("SORT BY OLDEST"); } else if (getItemOption.HasFlag(GetItemOptions.SortByLatest)) { Console.WriteLine("SORT BY NLATEST"); } } </code></pre> <p>I don't think you know much about bit-wise operations, and for that reason I simplified the code sample as much as possible by utilizing the <code>Enum.HasFlag</code> method which simply checks if the given <code>GetItemOptions</code> enum has a flag specified. </p> <p>You may have bared witness to this pattern before when using <a href="http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions.aspx" rel="nofollow"><code>RegexOptions Enumeration</code></a> or <a href="http://msdn.microsoft.com/en-gb/library/system.windows.forms.controlstyles.aspx" rel="nofollow"><code>ControlStyles Enumeration</code></a></p> <h2>Update</h2> <p>I would suggest that you create an <code>enum</code> for each parameter and define a class like this:</p> <pre><code>public class PocketDataRequest { public State? State { get; set; } public Favourite? Favourite { get; set; } public ContentType? ContentType { get; set; } public Sort? Sort { get; set; } public DetailType? DetailType { get; set; } public Dictionary&lt;string, string&gt; ToPostData() { return GetType().GetProperties() .Where(p =&gt; p.GetValue(this, null) != null) .ToDictionary(p =&gt; p.Name, p =&gt; p.GetValue(this, null).ToString()); } } </code></pre> <p>This would leverage the following syntax: </p> <pre><code>PocketDataRequest pocketDataRequest = new PocketDataRequest(); pocketDataRequest.State = State.Unread; pocketDataRequest.Sort = Sort.Newest; GetItemsBasedOn(pocketDataRequest.ToPostData()); </code></pre> <p>In my implementations <code>ToPostData</code> method, I use LINQ and Reflection, that is just because I am lazy. You need to manually evaluate each <code>enum</code> value, especially if you want to change the <code>enum</code> names to something more appropriate. Also, my code will fail if you try and pass the parameter titled <code>favorite</code>. This is because <code>favorite</code> takes either the number "0" or "1". This is not a big problem because what you can do it define the <code>enum</code> like this: </p> <pre><code>public enum Favourite { UnfavouritedItems = 0, FavouritedItems = 1 } </code></pre> <p>and then simply cast the value <code>(Int32)</code> and add that value to the <code>Dictionary&lt;string, string&gt;</code> or <code>NameValueCollection</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload