Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to pass custom options to function
    text
    copied!<p>I am writing a wrapper to a REST API. So, whenever I am retrieving items from the server, I just need to play around with the parameters (in the uri). The way I am doing it now works fine but I just feel there must be another elegance way to do this maybe with <code>enum</code> or something.</p> <p>I don't like that I need to 'know' what are the options are as the <code>Dictionary</code> type is <code>string</code>. I tried with <code>Dictionary&lt;EnumType, string&gt;</code>, but I have more than one type of <code>enum</code>. Plus, I am not sure how to map the <code>EnumType</code> (key) to the appropriate value.</p> <p>Basically, I am trying to avoid as much as possible the use of magic keyword.</p> <p>Here is my partial code:</p> <pre><code>public string GetUnreadItems() { var options = new Dictionary&lt;string, string&gt;(); options.Add("ItemType", "Unread"); options.Add("SortBy", "Latest"); // GetItemsBasedOn(options); } public string GetAllItems() { var options = new Dictionary&lt;string, string&gt;(); options.Add("ItemType", "All"); // GetItemsBasedOn(options); } public string GetItemsBasedOn(Dictionary&lt;string, string&gt; options) { // Do request here based on options passed // and return the result to caller function } </code></pre> <p>EDIT: This is what I am trying to implement <a href="http://getpocket.com/developer/docs/v3/retrieve" rel="nofollow">http://getpocket.com/developer/docs/v3/retrieve</a></p> <p>I would like to implement the options <code>sort</code>, <code>detailType</code>, <code>contentType</code>, <code>Favorite</code>, and <code>State</code>. And each of the options have their own options but only one can be selected at a time.</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