<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sides of march &#187; C#</title>
	<atom:link href="http://www.sidesofmarch.com/index.php/archive/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sidesofmarch.com</link>
	<description>Thoughts on life, liberty, and information technology</description>
	<lastBuildDate>Mon, 16 Jan 2012 02:43:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Everything you ever wanted to know about an enum (well, almost everything)</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2009/07/08/everything-you-ever-wanted-to-know-about-an-enum-well-almost-everything/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2009/07/08/everything-you-ever-wanted-to-know-about-an-enum-well-almost-everything/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 15:59:51 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[enum]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=374</guid>
		<description><![CDATA[<p>The enum, what a lovely creature available to us in C#. They help make our code more readable and bug-resistant. They also have some tricks up their sleeves.</p>
<p>Take a sample enum declaration:</p>


public enum Level
{
	Unknown = 0,
	Low,
	Medium,
	High
}

<p>If you want a string representation of an enum, you simply use the ToString() method.</p>


Console.WriteLine(Level.Unknown.ToString());
/* output:
Unknown
*/

<p>If you want the numeric value of an enum, cast it to an int. (By default, an enum is an int, though <a href="http://www.geekpedia.com/KB6_How-do-I-set-the-type-of-an-enumeration.html">you can specify other numeric values</a> for them.)</p>


int i = (int)Level.Low;
Console.WriteLine(i);
/* output:
1
*/

<p>If you have a string and want to convert it to an enum, use the Enum.Parse() method. Pass the enum type, the string value to parse, and an optional boolean parameter to note if you want the parsing to be case-insensitive. (By default, parsing is case-sensitive.)</p>


Level lvl;

// case-insensitive parsing
lvl = (Level)Enum.Parse(typeof(Level), &#34;medium&#34;, true);
Console.WriteLine(i);
/* output:
Medium
*/

// default, case-sensitive parsing
lvl = (Level)Enum.Parse(typeof(Level), &#34;high&#34;);
/* throws an exception:
System.ArgumentException: Requested value &#039;high&#039; was not found.
*/

<p>If you have a numeric value and want to <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2009/07/08/everything-you-ever-wanted-to-know-about-an-enum-well-almost-everything/">Everything you ever wanted to know about an enum (well, almost everything)</a></span>]]></description>
			<content:encoded><![CDATA[<p>The <code>enum</code>, what a lovely creature available to us in C#. They help make our code more readable and bug-resistant. They also have some tricks up their sleeves.</p>
<p>Take a sample enum declaration:</p>
<pre class="brush: csharp; ">

public enum Level
{
	Unknown = 0,
	Low,
	Medium,
	High
}
</pre>
<p>If you want a string representation of an enum, you simply use the <code>ToString()</code> method.</p>
<pre class="brush: csharp; ">

Console.WriteLine(Level.Unknown.ToString());
/* output:
Unknown
*/
</pre>
<p>If you want the numeric value of an enum, cast it to an <code>int</code>. (By default, an <code>enum</code> is an <code>int</code>, though <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.geekpedia.com%2FKB6_How-do-I-set-the-type-of-an-enumeration.html&sref=rss">you can specify other numeric values</a> for them.)</p>
<pre class="brush: csharp; ">

int i = (int)Level.Low;
Console.WriteLine(i);
/* output:
1
*/
</pre>
<p>If you have a string and want to convert it to an <code>enum</code>, use the <code>Enum.Parse()</code> method. Pass the <code>enum</code> type, the string value to parse, and an optional <code>boolean</code> parameter to note if you want the parsing to be case-insensitive. (By default, parsing is case-sensitive.)</p>
<pre class="brush: csharp; ">

Level lvl;

// case-insensitive parsing
lvl = (Level)Enum.Parse(typeof(Level), &quot;medium&quot;, true);
Console.WriteLine(i);
/* output:
Medium
*/

// default, case-sensitive parsing
lvl = (Level)Enum.Parse(typeof(Level), &quot;high&quot;);
/* throws an exception:
System.ArgumentException: Requested value &#039;high&#039; was not found.
*/
</pre>
<p>If you have a numeric value and want to convert it to an <code>enum</code>, simply typecast the number to the <code>enum</code> type. Remember, an <code>enum</code> is just a number!</p>
<pre class="brush: csharp; ">

int val = 3;
Level lvl = (Level)val;
Console.WriteLine(lvl);
/* output:
High
*/
</pre>
<p>What about a set of enums that are not mutually exclusive (i.e. a sets of flags)? A &#8220;flags&#8221; enum can have multiple values, and is decorated with the <code>[Flags]</code> attribute, as shown below. Note that a these enums should have values defined in powers of two (1, 2, 4, 8, etc.). See below for a sporting example.</p>
<pre class="brush: csharp; ">

[Flags]
public enum Bases
{
	Empty = 0,
	First = 1,
	Second = 2,
	Third = 4,
	FirstAndSecond = Bases.First + Bases.Second,
	FirstAndThird = Bases.First + Bases.Third,
	SecondAndThird = Bases.Second + Bases.Third,
	Loaded = Bases.First + Bases.Second + Bases.Third
}
</pre>
<p>Many of the previously defined operations work on these enums. String output is as you would expect when using <code>ToString()</code>:</p>
<pre class="brush: csharp; ">

Console.WriteLine(Bases.Empty.ToString());
Console.WriteLine(Bases.FirstAndSecond.ToString());
/* output:
Empty
FirstAndSecond
*/
</pre>
<p>Getting the numeric value is the same, too:</p>
<pre class="brush: csharp; ">

int i = (int)Bases.First;
int j = (int)Bases.SecondAndThird;
Console.WriteLine(i);
Console.WriteLine(j);
/* output:
1
6
*/
</pre>
<p>Parsing &#8212; yes, the same.</p>
<pre class="brush: csharp; ">

Bases b1 = (Bases)Enum.Parse(typeof(Bases), &quot;second&quot;, true);
Bases b2 = (Bases)Enum.Parse(typeof(Bases), &quot;Loaded&quot;);
Console.WriteLine(b1);
Console.WriteLine(b2);
/* output:
Second
Loaded
*/
</pre>
<p>Numeric values? The same as well.</p>
<pre class="brush: csharp; ">

int val = 5;
Bases bases = (Bases)val;
Console.WriteLine(bases);
/* output:
FirstAndThird
*/
</pre>
<p>One thing you&#8217;d want to do with a flagg enum is to detect if a particular flag is set. It&#8217;s easy to do using the AND operator (<code>&#038;</code>).</p>
<pre class="brush: csharp; ">

bool isFirstOnSecondOrThird = (Bases.First &amp; Bases.SecondAndThird) == Bases.First;
bool isFirstOnLoaded = (Bases.First &amp; Bases.Loaded) == Bases.First;

/* output:
False
True
*/
</pre>
<p>There&#8217;s lots more that you can do with enums &#8212; check out some of the following links for more info:</p>
<ul>
<li><a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.enum.aspx&sref=rss">MSDN documentation on the Enum Class</a></li>
<li><a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.csharp-station.com%2FTutorials%2FLesson17.aspx&sref=rss">C# Station Enums tutorial</a></li>
<li><a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fen.csharp-online.net%2Fenum&sref=rss">enum at C# Online.Net</a></li>
<li><a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fdotnetperls.com%2Fenum-value&sref=rss">C# Enum Tips and Examples</a></li>
</ul>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=374&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2009/07/08/everything-you-ever-wanted-to-know-about-an-enum-well-almost-everything/' addthis:title='Everything you ever wanted to know about an enum (well, almost everything) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2009/07/08/everything-you-ever-wanted-to-know-about-an-enum-well-almost-everything/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# 3.0&#8242;s syntatic sugar</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2009/01/05/c-sharp-3-syntatic-sugar/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2009/01/05/c-sharp-3-syntatic-sugar/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 19:53:18 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=318</guid>
		<description><![CDATA[<p>In the old days, we’d write something like this:</p>


view.Rows.Add(&#34;Type&#34;);
view.Rows.Add(&#34;Count&#34;);
view.Rows.Add(&#34;Whatever&#34;);

<p>With C# 3.0 we can do this:</p>


new string[] { &#34;Type&#34;, &#34;Count&#34;, &#34;Whatever&#34; }
    .ForEach(str =&#62; view.Rows.Add(str));

<p>That is very cool <a href="http://weblogs.asp.net/scottgu/archive/2007/03/08/new-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx">syntatic sugar</a>. Sure, this is not new news, but when you think about C# in the context of the new features available in 3.0, it really starts to feel a lot more like coding with JavaScript (which is a good thing).</p>
<a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2009/01/05/c-sharp-3-syntatic-sugar/">C# 3.0&#8242;s syntatic sugar</a></span>]]></description>
			<content:encoded><![CDATA[<p>In the old days, we’d write something like this:</p>
<pre class="brush: c-sharp; ">

view.Rows.Add(&quot;Type&quot;);
view.Rows.Add(&quot;Count&quot;);
view.Rows.Add(&quot;Whatever&quot;);
</pre>
<p>With C# 3.0 we can do this:</p>
<pre class="brush: c-sharp; ">

new string[] { &quot;Type&quot;, &quot;Count&quot;, &quot;Whatever&quot; }
    .ForEach(str =&gt; view.Rows.Add(str));
</pre>
<p>That is very cool <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fweblogs.asp.net%2Fscottgu%2Farchive%2F2007%2F03%2F08%2Fnew-c-orcas-language-features-automatic-properties-object-initializers-and-collection-initializers.aspx&sref=rss">syntatic sugar</a>. Sure, this is not new news, but when you think about C# in the context of the new features available in 3.0, it really starts to feel a lot more like coding with JavaScript (which is a good thing).</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=318&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2009/01/05/c-sharp-3-syntatic-sugar/' addthis:title='C# 3.0&#8242;s syntatic sugar ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2009/01/05/c-sharp-3-syntatic-sugar/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Google AdSense ViewComponent for MonoRail</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 02:05:21 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MonoRail]]></category>
		<category><![CDATA[ViewComponent]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=253</guid>
		<description><![CDATA[<p>Like many, I use <a href="http://www.google.com/adsense/">Google AdSense</a> to host ads on my web sites. Including the ad code typically requires you to paste a block a block of JavaScript onto the page. I don&#8217;t mind injecting the JavaScript, but I wanted to come up with a better way.</p>
<p>The solution I came up with was a quick <a href="http://www.castleproject.org/monorail">MonoRail</a> <a href="http://www.castleproject.org/monorail/documentation/trunk/usersguide/viewcomponents.html">ViewComponent</a>, AdUnit. It has a simple purpose: to render a block of code for an ad unit of a given size.</p>
<p>The first task was to create an enum which represented all the possible ad unit sizes, based on <a href="https://www.google.com/adsense/static/en_US/AdFormats.html">what&#8217;s offered by Google</a>.</p>


/// &#38;lt;summary&#38;gt;
/// A typical ad unit size, using standards defined by Google.
/// &#38;lt;/summary&#38;gt;
public enum AdUnitSize
{
/// &#38;lt;summary&#38;gt;
/// 728x90
/// &#38;lt;/summary&#38;gt;
Leaderboard = 1,
/// &#38;lt;summary&#38;gt;
/// 468x60
/// &#38;lt;/summary&#38;gt;
Banner = 2,
/// &#38;lt;summary&#38;gt;
/// 234x60
/// &#38;lt;/summary&#38;gt;
HalfBanner = 3,
/// &#38;lt;summary&#38;gt;
/// 125x125
/// &#38;lt;/summary&#38;gt;
Button = 4,
/// &#38;lt;summary&#38;gt;
/// 120x600
/// &#38;lt;/summary&#38;gt;
Skyscraper = 5,
/// &#38;lt;summary&#38;gt;
/// 160x600
/// &#38;lt;/summary&#38;gt;
WideSkyscraper = 6,
/// &#38;lt;summary&#38;gt;
/// 180x150
/// &#38;lt;/summary&#38;gt;
SmallRectangle = 7,
/// &#38;lt;summary&#38;gt;
/// 120x240
/// &#38;lt;/summary&#38;gt;
VerticalBanner = 8,
/// &#38;lt;summary&#38;gt;
/// 200x200
/// &#38;lt;/summary&#38;gt;
SmallSquare = 9,
/// <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/">Google AdSense ViewComponent for MonoRail</a></span>]]></description>
			<content:encoded><![CDATA[<p>Like many, I use <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.google.com%2Fadsense%2F&sref=rss">Google AdSense</a> to host ads on my web sites. Including the ad code typically requires you to paste a block a block of JavaScript onto the page. I don&#8217;t mind injecting the JavaScript, but I wanted to come up with a better way.</p>
<p>The solution I came up with was a quick <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail&sref=rss">MonoRail</a> <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail%2Fdocumentation%2Ftrunk%2Fusersguide%2Fviewcomponents.html&sref=rss">ViewComponent</a>, AdUnit. It has a simple purpose: to render a block of code for an ad unit of a given size.</p>
<p>The first task was to create an enum which represented all the possible ad unit sizes, based on <a href="http://redirectingat.com?id=17923X751173&xs=1&url=https%3A%2F%2Fwww.google.com%2Fadsense%2Fstatic%2Fen_US%2FAdFormats.html&sref=rss">what&#8217;s offered by Google</a>.<span id="more-253"></span></p>
<pre class="brush: c-sharp; ">

/// &amp;lt;summary&amp;gt;
/// A typical ad unit size, using standards defined by Google.
/// &amp;lt;/summary&amp;gt;
public enum AdUnitSize
{
/// &amp;lt;summary&amp;gt;
/// 728x90
/// &amp;lt;/summary&amp;gt;
Leaderboard = 1,
/// &amp;lt;summary&amp;gt;
/// 468x60
/// &amp;lt;/summary&amp;gt;
Banner = 2,
/// &amp;lt;summary&amp;gt;
/// 234x60
/// &amp;lt;/summary&amp;gt;
HalfBanner = 3,
/// &amp;lt;summary&amp;gt;
/// 125x125
/// &amp;lt;/summary&amp;gt;
Button = 4,
/// &amp;lt;summary&amp;gt;
/// 120x600
/// &amp;lt;/summary&amp;gt;
Skyscraper = 5,
/// &amp;lt;summary&amp;gt;
/// 160x600
/// &amp;lt;/summary&amp;gt;
WideSkyscraper = 6,
/// &amp;lt;summary&amp;gt;
/// 180x150
/// &amp;lt;/summary&amp;gt;
SmallRectangle = 7,
/// &amp;lt;summary&amp;gt;
/// 120x240
/// &amp;lt;/summary&amp;gt;
VerticalBanner = 8,
/// &amp;lt;summary&amp;gt;
/// 200x200
/// &amp;lt;/summary&amp;gt;
SmallSquare = 9,
/// &amp;lt;summary&amp;gt;
/// 250x250
/// &amp;lt;/summary&amp;gt;
Square = 10,
/// &amp;lt;summary&amp;gt;
/// 300x250
/// &amp;lt;/summary&amp;gt;
MediumRectangle = 11,
/// &amp;lt;summary&amp;gt;
/// 336x280
/// &amp;lt;/summary&amp;gt;
LargeRectangle = 12
}
</pre>
<p>With that, I now can write the <code>AdUnitComponent</code>.</p>
<pre class="brush: c-sharp; ">

/// &amp;lt;summary&amp;gt;
/// Displays an ad unit of a given size.
/// &amp;lt;/summary&amp;gt;
[ViewComponentDetails(&quot;AdUnit&quot;)]
public class AdUnitComponent : ViewComponent
{
private string _adUnitCode;
private bool _renderView;

/// &amp;lt;summary&amp;gt;
/// The HTML ID of the form element to set focus to.
/// &amp;lt;/summary&amp;gt;
[ViewComponentParam(Required = true)]
public virtual AdUnitSize Size { get; set; }

/// &amp;lt;summary&amp;gt;
/// Initializes the view component.
/// &amp;lt;/summary&amp;gt;
public override void Initialize()
{
_adUnitCode = WebConfigurationManager.AppSettings[&quot;AdUnit_&quot; + this.Size.ToString()];
_renderView = !String.IsNullOrEmpty(_adUnitCode);
}

/// &amp;lt;summary&amp;gt;
/// Renders the view component, injecting HTML into the web page.
/// &amp;lt;/summary&amp;gt;
public override void Render()
{
if (_renderView)
{
RenderText(_adUnitCode);
}
CancelView();
}
}
</pre>
<p>The most important code takes place in the <code>Initialize</code> method. It&#8217;s in that method where we retrieve the ad code from the web.config file. Simply add an entry to your <code>&lt;appSettings&gt;</code> section with the name <code>AdUnit_<em>AdUnitSize</em></code>. An example follows.</p>
<pre class="brush: xml; ">

&amp;lt;appSettings&amp;gt;
&amp;lt;!-- ad units --&amp;gt;
&amp;lt;add key=&quot;AdUnit_MediumRectangle&quot; value=&quot;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;&amp;lt;!--
google_ad_client = &quot;pub-4509135091166921&quot;;
google_ad_slot = &quot;2912665903&quot;;
google_ad_width = 300;
google_ad_height = 250;
//--&amp;gt;
&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&amp;gt;
&amp;lt;/script&amp;gt;
&quot; /&amp;gt;
&amp;lt;add key=&quot;AdUnit_WideSkyscraper&quot; value=&quot;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;&amp;lt;!--
google_ad_client = &quot;pub-4509135091166921&quot;;
google_ad_slot = &quot;3455950222&quot;;
google_ad_width = 160;
google_ad_height = 600;
//--&amp;gt;
&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&amp;gt;
&amp;lt;/script&amp;gt;
&quot; /&amp;gt;
&amp;lt;/appSettings&amp;gt;
</pre>
<p>You don&#8217;t have to use Google AdSense code &#8212; you can actually use this ViewComponent to inject any HTML into a page.</p>
<p>Last thing &#8212; how do you call the <code>AdUnitComponent</code> from your view? If using NVelocity&#8230;</p>
<pre class="brush: html; ">

#component(AdUnit with &quot;Size=WideSkyscraper&quot;)
</pre>
<p>That would render the text specified in the value field of the <code>AdUnit_WideSkyscraper</code> key in your configuration file&#8217;s appSettings.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=253&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/' addthis:title='Google AdSense ViewComponent for MonoRail ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2008/06/05/google-adsense-viewcomponent-for-monorail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

