<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-300439418265524101</id><updated>2011-11-27T16:41:32.376-08:00</updated><category term='Reference types'/><category term='XNA 4.0'/><category term='Visual Studio'/><category term='ASP.NET - Not so basic'/><category term='Enums'/><category term='C# - Not so basic'/><category term='Non programming'/><category term='Real Time Strategy with XNA'/><category term='Extension Methods'/><category term='Expandable objects'/><category term='Converters'/><category term='C# - Basic'/><category term='Blog thoughts'/><category term='Databinding expressions'/><title type='text'>Hugonne - Another .net Coding Blog</title><subtitle type='html'>Tips and thoughts about .net programming.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-3471178330934317475</id><published>2011-01-07T18:14:00.001-08:00</published><updated>2011-01-07T18:20:28.853-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='XNA 4.0'/><category scheme='http://www.blogger.com/atom/ns#' term='C# - Not so basic'/><category scheme='http://www.blogger.com/atom/ns#' term='Real Time Strategy with XNA'/><title type='text'>Creating a simple 2D RTS game with XNA 4.0 – Chapter 0</title><content type='html'>&lt;p&gt;Hi all. &lt;/p&gt;&lt;p&gt;In my attempt to learn some XNA 4.0, I decided to TRY to build a simple RTS (Real Time Strategy) game. The idea is to create a basic engine and continue adding more features to it within time. I´m gonna be sharing my experiences and questions here, so any help, feedback or general suggestions would be greatly appreciated.&lt;/p&gt;&lt;p&gt;I currently don’t even have a name for the game. I guess I’ll come up with one when I have at least my basic structure up and running.&lt;/p&gt;&lt;p&gt;My basic plan is to start by resolving some core issues of the game, like unit creation, selection and movement, and continue moving up the ladder by adding more and more features. Let’s just see how we do. Hope you enjoy the ride.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-3471178330934317475?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/3471178330934317475/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2011/01/creating-simple-2d-rts-game-with-xna-40.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/3471178330934317475'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/3471178330934317475'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2011/01/creating-simple-2d-rts-game-with-xna-40.html' title='Creating a simple 2D RTS game with XNA 4.0 – Chapter 0'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-7532800119918183586</id><published>2010-05-27T10:08:00.000-07:00</published><updated>2011-01-07T20:11:29.269-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio'/><title type='text'>Style your Visual Studio</title><content type='html'>&lt;p&gt;I found this great site, &lt;a href="http://studiostyles.info/"&gt;http://studiostyles.info/&lt;/a&gt;, which provides different color styles uploaded by the community for Visual Studio 2005, 2008 and 2010. Very nice stuff...  &lt;/p&gt;&lt;br /&gt;
&lt;div style="text-align: center; clear: both" class="separator"&gt;&lt;a style="margin-left: 1em; margin-right: 1em" href="http://2.bp.blogspot.com/_GhBv2tIkK2o/S_6m0j0r3xI/AAAAAAAABeY/w_DPBcF5r4M/s1600/StudioStyles.jpg" imageanchor="1"&gt;&lt;img style="display: inline; float: left" border="0" align="left" src="http://2.bp.blogspot.com/_GhBv2tIkK2o/S_6m0j0r3xI/AAAAAAAABeY/w_DPBcF5r4M/s640/StudioStyles.jpg" width="412" height="322" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-7532800119918183586?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/7532800119918183586/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2010/05/style-your-visual-studio.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7532800119918183586'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7532800119918183586'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2010/05/style-your-visual-studio.html' title='Style your Visual Studio'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_GhBv2tIkK2o/S_6m0j0r3xI/AAAAAAAABeY/w_DPBcF5r4M/s72-c/StudioStyles.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-8684212771363210895</id><published>2010-01-13T07:49:00.001-08:00</published><updated>2011-01-07T20:11:08.662-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Extension Methods'/><category scheme='http://www.blogger.com/atom/ns#' term='C# - Not so basic'/><category scheme='http://www.blogger.com/atom/ns#' term='Enums'/><title type='text'>Enums and string values: Improved version</title><content type='html'>&lt;p&gt;Last week I wrote an entry about &lt;a href="http://hugonne.blogspot.com/2010/01/enums-and-string-values.html" target="_blank"&gt;converting strings into enums by using their associated StringValueAttribute&lt;/a&gt;. Well, my friend &lt;a href="http://www.blogger.com/profile/11390055986785442299" target="_blank"&gt;Vicente Peña&lt;/a&gt; suggested another approach of the &lt;strong&gt;GetEnumStringValue&lt;/strong&gt; method, which will save us the need to make the explicit cast. Here's the code:  &lt;/p&gt;&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;1: &lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;2: &lt;span style="color: #808080"&gt;/// Get an enum from a string value attribute&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;3: &lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;4: &lt;span style="color: #808080"&gt;/// &amp;lt;see cref=&amp;quot;http://hugonne.blogspot.com/2010/01/enums-and-string-values.html&amp;quot;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;5: &lt;span style="color: #808080"&gt;/// &amp;lt;remarks&amp;gt;Created by Vicente Peña&amp;lt;/remarks&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;6: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T GetEnumStringValue&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; ignoreCase)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;7: {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;8:     &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; result = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;9:     Type type = &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;10:     &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; enumStringValue = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;11:     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(!type.IsEnum)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;12:         &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentException(&amp;quot;&lt;span style="color: #8b0000"&gt;enumType should be a valid enum&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;13: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;14:     &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;(FieldInfo fieldInfo &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; type.GetFields())
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;15:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;16:         var attribs =
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;17:             fieldInfo.GetCustomAttributes(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(StringValueAttribute), &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;) 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;18:             &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; StringValueAttribute[];
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;19:         &lt;span style="color: #008000"&gt;//Get the StringValueAttribute for each enum member&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;20:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(attribs != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;21:             &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(attribs.Length &amp;gt; 0)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;22:                 enumStringValue = attribs[0].StringValue;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;23: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;24:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Compare(enumStringValue, &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;, ignoreCase) == 0)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;25:             result = Enum.Parse(type, fieldInfo.Name);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;26:     }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;27: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;28:     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(result != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;29:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;30:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (T) result;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;31:     }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;32:     &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentException(&amp;quot;&lt;span style="color: #8b0000"&gt;String not found&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;33: }&lt;/pre&gt;&lt;/pre&gt;&lt;p&gt;To call it, we just have to do this:&lt;/p&gt;&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;1: UserState enu = &amp;quot;&lt;span style="color: #8b0000"&gt;A&lt;/span&gt;&amp;quot;.GetEnumStringValue&amp;lt;UserState&amp;gt;(&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-8684212771363210895?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/8684212771363210895/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2010/01/enums-and-string-values-improved.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/8684212771363210895'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/8684212771363210895'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2010/01/enums-and-string-values-improved.html' title='Enums and string values: Improved version'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-5153433606682260610</id><published>2010-01-12T15:45:00.001-08:00</published><updated>2010-01-12T15:47:13.088-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# - Not so basic'/><title type='text'>Moving the focus to the next element with the Enter key</title><content type='html'>&lt;p&gt;In Windows applications where users have to input big amounts of data, some people find it better to be able to move to the next control in the Form with the Enter key (as well as the Tab key). To accomplish that, you could always handle the KeyDown event on each control, and move the focus to the next control in the list. While this is effective, it'll get boring if your form has a lot of controls. &lt;/p&gt;  &lt;p&gt;The other way around is to handle the Form's KeyDown event directly, and move to the next UIElement in the list. Something like this (the example is done with a WPF Window object, not a WinForm, but the idea is the same):&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Window_KeyDown(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, KeyEventArgs e)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #008000"&gt;//Enable this code if you don't want to use the Tab key anymore&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:     &lt;span style="color: #008000"&gt;/*if (e.Key == Key.Tab)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:         e.Handled = true;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:         return;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8:     }*/&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10:     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (e.Key == Key.Enter)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12:         var tRequest = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TraversalRequest(FocusNavigationDirection.Next);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13:         var keyboardFocus = Keyboard.FocusedElement &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; UIElement;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 14: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 15:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (keyboardFocus != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 16:         {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 17:             keyboardFocus.MoveFocus(tRequest);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 18:         }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 19: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 20:         e.Handled = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 21:     }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 22: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;As &lt;a href="http://social.msdn.microsoft.com/Profile/en-US/?user=Ben%20Ronco%20-%20MSFT" target="_blank"&gt;Ben Ronco – MSFT&lt;/a&gt; recommended it in the forumk &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/007a0905-a05b-4009-930c-e206804b6a39" target="_blank"&gt;thread&lt;/a&gt; I started asking this: &amp;quot;be careful when using a key like Enter to control navigation if the elements in your application include controls that also consume Enter keystrokes&amp;quot;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-5153433606682260610?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/5153433606682260610/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2010/01/moving-focus-to-next-element-with-enter.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/5153433606682260610'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/5153433606682260610'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2010/01/moving-focus-to-next-element-with-enter.html' title='Moving the focus to the next element with the Enter key'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-7910650270640390787</id><published>2010-01-06T12:54:00.001-08:00</published><updated>2010-01-13T06:59:57.770-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Extension Methods'/><category scheme='http://www.blogger.com/atom/ns#' term='C# - Not so basic'/><category scheme='http://www.blogger.com/atom/ns#' term='Enums'/><title type='text'>Enums and String values</title><content type='html'>&lt;p&gt;Recently, I had the need to set string values for enums, instead of using int values. Browsing the web, I came to Stefan Sedich’s blog, which had &lt;a href="http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx" target="_blank"&gt;a very interesting article&lt;/a&gt; on creating string value attributes for enums. The article explained how to assign a custom string attribute to each enum value, and then how to get that string value from the enum by using reflection. &lt;/p&gt;  &lt;p&gt;The article, as I said, was great, but it was missing one thing: working the other way around. I mean, it explained how to &lt;em&gt;get the string value from the enum&lt;/em&gt;, but not how to &lt;em&gt;get the enum value from the string&lt;/em&gt;. I needed both &amp;quot;ends&amp;quot; of the feature, With the help of a CodeProject &lt;a href="http://www.codeproject.com/KB/cs/stringenum.aspx?display=Print" target="_blank"&gt;article&lt;/a&gt; I found, I got to implement it succesfully. &lt;/p&gt;  &lt;p&gt;So, to tell the whole story, here's how it basically works:&lt;/p&gt;  &lt;p&gt;First, create a custom Attribute (a class that inherits the &lt;strong&gt;Attribute&lt;/strong&gt; class)&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; StringValueAttribute : Attribute
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:     &lt;span style="color: #808080"&gt;/// Holds the stringvalue for a value in an enum.&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:     &lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; StringValue { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8:     &lt;span style="color: #808080"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9:     &lt;span style="color: #808080"&gt;/// Constructor used to init a StringValue Attribute&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10:     &lt;span style="color: #808080"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11:     &lt;span style="color: #808080"&gt;/// &amp;lt;param name=&amp;quot;value&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; StringValueAttribute(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 14:         &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.StringValue = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 15:     }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 16: }&lt;/pre&gt;&lt;/pre&gt;
Second, add the attribute to each one of your enum values: 

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;enum&lt;/span&gt; UserState
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     [StringValueAttribute(&amp;quot;&lt;span style="color: #8b0000"&gt;A&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:     Active,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:     [StringValueAttribute(&amp;quot;&lt;span style="color: #8b0000"&gt;I&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:     Inactive,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:     [StringValueAttribute(&amp;quot;&lt;span style="color: #8b0000"&gt;S&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8:     StandBy
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9: } &lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Now, to get the associated string for a given enum value, you create an &lt;a href="http://hugonne.blogspot.com/2010/01/adding-methods-to-existing-types-with.html" target="_blank"&gt;Extension Method&lt;/a&gt; for the Enum type:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; GetStringValue(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; Enum &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #008000"&gt;// Get the type&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:     Type type = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;.GetType();
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:     &lt;span style="color: #008000"&gt;// Get fieldinfo for this type&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:     FieldInfo fieldInfo = type.GetField(&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;.ToString());
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9:     &lt;span style="color: #008000"&gt;// Get the stringvalue attributes&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10:     var attribs = 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11:         fieldInfo.GetCustomAttributes(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(StringValueAttribute), &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;) 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12:         &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; StringValueAttribute[];
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 14:     &lt;span style="color: #008000"&gt;// Return the first if there was a match.&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 15:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; attribs.Length &amp;gt; 0 ? attribs[0].StringValue : &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 16: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;To get the string value for the enum, we just needed to call the extension method:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; state = UserState.Inactive.GetStringValue();&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;As I already said. This was all posted in Stefan Sedich’s &lt;a href="http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx" target="_blank"&gt;article&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Now to go the other way around, here's what we need to do:&lt;/p&gt;

&lt;p&gt;Just as in the first extension method was for the enum type and returned a string, our new one will have to be for the string type and return an enum. Actually, because the method will be generic to any enum type, we'll return an object, which will have to be casted by the caller to the appropriate type. This is the code:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; GetEnumStringValue(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;, 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2:     Type enumType, &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; ignoreCase)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:     &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; result = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:     &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; enumStringValue = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!enumType.IsEnum)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:         &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentException
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8:             (&amp;quot;&lt;span style="color: #8b0000"&gt;enumType should be a valid enum&lt;/span&gt;&amp;quot;);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10:     &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (FieldInfo fieldInfo &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; enumType.GetFields())
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12:         var attribs = 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13:             fieldInfo.GetCustomAttributes(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(StringValueAttribute), &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;) 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 14:             &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; StringValueAttribute[];
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 15:         &lt;span style="color: #008000"&gt;//Get the StringValueAttribute for each enum member&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 16:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (attribs.Length &amp;gt; 0)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 17:             enumStringValue = attribs[0].StringValue;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 18: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 19:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Compare(enumStringValue, &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;, ignoreCase) == 0)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 20:             result = Enum.Parse(enumType, fieldInfo.Name);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 21:     }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 22: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 23:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; result;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 24: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;To call it, we'll go with something like this:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: UserState enu = (UserState)&amp;quot;&lt;span style="color: #8b0000"&gt;A&lt;/span&gt;&amp;quot;.GetEnumStringValue(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(UserState), &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;/pre&gt;
There you go, hope you find it useful. 

  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-7910650270640390787?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/7910650270640390787/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2010/01/enums-and-string-values.html#comment-form' title='3 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7910650270640390787'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7910650270640390787'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2010/01/enums-and-string-values.html' title='Enums and String values'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-461315563875231923</id><published>2010-01-06T12:00:00.001-08:00</published><updated>2010-01-06T12:18:16.836-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Extension Methods'/><category scheme='http://www.blogger.com/atom/ns#' term='C# - Not so basic'/><title type='text'>Adding methods to existing types with Extension Methods</title><content type='html'>&lt;p&gt;Ever felt like one of the types you work with could have used a couple of extra methods? Maybe you’d want to add to the &lt;strong&gt;string&lt;/strong&gt; type a method called ToHyperLink, which will add HTML tags to your string to make it a valid HyperLink. In other words, if your string is &lt;/p&gt;  &lt;p&gt;&lt;font color="#804000"&gt;http://msdn.microsoft.com&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;and you call the method, you'll get something like &lt;/p&gt;  &lt;p&gt;&lt;font color="#804000"&gt;&amp;lt;a href='http://msdn.microsoft.com'&amp;gt;http://msdn.microsoft.com&amp;lt;/a&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;We basically have two approaches here: one, create a class that inherits &lt;strong&gt;string&lt;/strong&gt;, and implement the method there. This will mean that if you wanted to use the method on an object, it'll have to be declared as your new type, not &lt;strong&gt;string&lt;/strong&gt;. The second approach is to use &lt;strong&gt;Extension Methods&lt;/strong&gt;.&amp;#160; With this technique, you get the possibility to &amp;quot;add&amp;quot; methods to existing types without having to create derived classes, thus &lt;em&gt;extending&lt;/em&gt; their functionality. &lt;/p&gt;  &lt;p&gt;Extension method have to be implemented inside a &lt;strong&gt;static class&lt;/strong&gt;, and of course have to be static themselves. They could return any type you want, and have to have at least one parameter, of the type you want to extend, preceded by the word &lt;strong&gt;this&lt;/strong&gt;. Check out this example:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Extensions
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; ToHyperLink(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; url)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:     {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:         &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; hl = &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;&amp;lt;a href='{0}'&amp;gt;{0}&amp;lt;/a&amp;gt;&lt;/span&gt;&amp;quot;, url);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; hl;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:     }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;In the above code, we created an extension method for the &lt;strong&gt;string &lt;/strong&gt;type called ToHyperLink, which returns another string. It's that simple. As said before, the catch here is just to add the keyword &lt;strong&gt;this&lt;/strong&gt;, and to &lt;strong&gt;make both the method and its class static&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now, if we wanted to use it, we jus need a using reference to whatever the classes' namespace is, and call it just like we call any of the other string methods:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #008000"&gt;//Here call the extension method an a variable&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; a = &amp;quot;&lt;span style="color: #8b0000"&gt;http://msdn.microsoft.com&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: a = a.ToHyperLink();
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: &lt;span style="color: #008000"&gt;//Here we call the extension method on a plain string&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; b = &amp;quot;&lt;span style="color: #8b0000"&gt;http://hugonne.blogspot.com&lt;/span&gt;&amp;quot;.ToHyperLink();&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Notice line 5? You can call the extension method directly on a string value rather than a variable. This is something we couldn't have achieved with inheritance.&lt;/p&gt;

&lt;p&gt;Extension methods can also take additional parameters. Let's say we want to create an overload for the method, which takes the text for the HyperLink. We'd add a method to our class like this:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; ToHyperLink(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; url, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; linkText)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; hl = &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;&amp;lt;a href='{0}'&amp;gt;{1}&amp;lt;/a&amp;gt;&lt;/span&gt;&amp;quot;, url, linkText);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; hl;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Calling the method is just as simple:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; c = &amp;quot;&lt;span style="color: #8b0000"&gt;http://msdn.microsoft.com&lt;/span&gt;&amp;quot;;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: c = c.ToHyperLink(&amp;quot;&lt;span style="color: #8b0000"&gt;Go to MSDN&lt;/span&gt;&amp;quot;);&lt;/pre&gt;&lt;/pre&gt;
So, extension methods are are quick way to expand the functionality of our existing types. Nevertheless, it's always good to say you should try and use them only for simple operations. If you require some sort of more complex operations, it'll be always better to use inheritance, and make your own specialized types. 

&lt;p&gt;One final thing: extension methods only work in the .NET Framework 3.5.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-461315563875231923?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/461315563875231923/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2010/01/adding-methods-to-existing-types-with.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/461315563875231923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/461315563875231923'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2010/01/adding-methods-to-existing-types-with.html' title='Adding methods to existing types with Extension Methods'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-4278752429228307597</id><published>2009-10-16T14:46:00.001-07:00</published><updated>2009-10-16T14:46:35.424-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Reference types'/><category scheme='http://www.blogger.com/atom/ns#' term='C# - Basic'/><title type='text'>Reference types</title><content type='html'>&lt;p&gt;Quick reminder: Any object you have to instantiate (e.g. create with the &lt;strong&gt;new&lt;/strong&gt; keyword) is what’s called a &lt;strong&gt;Reference Type&lt;/strong&gt;. It means that, when used as a method parameter, it will always be treated as a reference parameter (&lt;strong&gt;ref &lt;/strong&gt;in C#, &lt;strong&gt;ByRef&lt;/strong&gt; in VB).&lt;/p&gt;  &lt;p&gt;In other words, this is redundant:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ModifyData(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; DataSet data)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #008000"&gt;//....&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Because DataSet is a reference type, the &lt;strong&gt;ref&lt;/strong&gt; keyword in the above examples serves no use.&amp;#160; &lt;/p&gt;

&lt;p&gt;Opposite to reference types are &lt;strong&gt;Value Types&lt;/strong&gt;, like an int or a string. They have to specify the ref keyword in the parameter if they want to be treated as such.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.c-sharpcorner.com/UploadFile/jaishmathews/WorkingofReferenceTypeandValueType09102006022502AM/WorkingofReferenceTypeandValueType.aspx" target="_blank"&gt;Here&lt;/a&gt;’s a good article on reference and value types.&lt;/p&gt;

&lt;p&gt;See ya.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-4278752429228307597?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/4278752429228307597/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/10/reference-types.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/4278752429228307597'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/4278752429228307597'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/10/reference-types.html' title='Reference types'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-1794401373688042113</id><published>2009-10-14T09:46:00.001-07:00</published><updated>2009-10-14T09:47:07.669-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Enums'/><category scheme='http://www.blogger.com/atom/ns#' term='C# - Basic'/><title type='text'>Setting an enum value from a string</title><content type='html'>&lt;p&gt;In a piece of code I was reviewing I saw that they wanted to set the value for an enum from a string, and what they did was to create a large method with a big switch statement, setting the correct enum value for each case. Something like:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;enum&lt;/span&gt; Operators
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:    GreaterThan,
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:    Equals,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:    LessThan
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: Operators GetOperatorFromString(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; s)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9: {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10:    &lt;span style="color: #0000ff"&gt;switch&lt;/span&gt;(s.ToUpper())
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11:    {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12:       &lt;span style="color: #0000ff"&gt;case&lt;/span&gt; &amp;quot;&lt;span style="color: #8b0000"&gt;GREATERTHAN&lt;/span&gt;&amp;quot;:
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13:          &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Operators.GreaterThan;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 14:       &lt;span style="color: #0000ff"&gt;case&lt;/span&gt; &amp;quot;&lt;span style="color: #8b0000"&gt;EQUALS&lt;/span&gt;&amp;quot;:
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 15:          &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Operators.Equals;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 16:       &lt;span style="color: #0000ff"&gt;case&lt;/span&gt; &amp;quot;&lt;span style="color: #8b0000"&gt;LESSTHAN&lt;/span&gt;&amp;quot;:
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 17:          &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Operators.LessThan;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 18:    }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 19: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Now, this could be kind of simple if your enum doesn’t have many values, but if we’re talking about very large lists, then we have a problem…&lt;/p&gt;

&lt;p&gt;Well, the solution for this is actually very simple, by using &lt;strong&gt;System.Enum.Parse&lt;/strong&gt;, which has the following signature: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;font face="Courier New"&gt;object Enum.Parse(System.Type enumType, string value, bool ignoreCase);&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, you can write the following:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;enum&lt;/span&gt; Operators
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:    GreaterThan,
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:    Equals,
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:    LessThan
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10: Operators o = (Operators)Enum.Parse(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(Operators), &amp;quot;&lt;span style="color: #8b0000"&gt;greaterthan&lt;/span&gt;&amp;quot;, &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;);&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Notice that, since we have the &lt;strong&gt;ingnoreCase&lt;/strong&gt; parameter set to true,&amp;#160; we can use the string in any kind of case. If the string value doesn’t match any of the enum values, an exception of type &lt;a href="http://msdn.microsoft.com/es-es/library/system.argumentexception(VS.80).aspx" target="_blank"&gt;ArgumentException&lt;/a&gt; will be raised.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-1794401373688042113?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/1794401373688042113/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/10/setting-enum-value-from-string.html#comment-form' title='1 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/1794401373688042113'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/1794401373688042113'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/10/setting-enum-value-from-string.html' title='Setting an enum value from a string'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-7982469636978922852</id><published>2009-10-06T06:57:00.001-07:00</published><updated>2009-10-06T06:57:58.633-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Non programming'/><title type='text'>Quick Windows tips</title><content type='html'>&lt;p&gt;This has nothing to do with programming, and probably many people already know it, but what the hell. It might be a nice thing to teach your end users. &lt;/p&gt;  &lt;p&gt;First, to get a screenshot of just the active window, instead of your whole screen, just press the &lt;strong&gt;Alt&lt;/strong&gt; and &lt;strong&gt;PrintScreen&lt;/strong&gt; keys together. That way, you’ll copy the screenshot to your clipboard, but you’ll not get your task bar, and you wouldn’t have to go to Paint to chop your image. &lt;/p&gt;  &lt;p&gt;Second, to get the text of a message box shown by an app, just make sure that box is active, and press Ctrl + C. Windows will automatically copy the title, message and buttons text to the clipboard. You’ll get something like this:&lt;/p&gt;  &lt;p&gt;---------------------------    &lt;br /&gt;Exit     &lt;br /&gt;---------------------------     &lt;br /&gt;Are you sure you want to quit?     &lt;br /&gt;---------------------------     &lt;br /&gt;Yes&amp;#160;&amp;#160; No&amp;#160;&amp;#160; &lt;br /&gt;---------------------------&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-7982469636978922852?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/7982469636978922852/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/10/quick-windows-tips.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7982469636978922852'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7982469636978922852'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/10/quick-windows-tips.html' title='Quick Windows tips'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-1654966792213410206</id><published>2009-09-18T09:18:00.001-07:00</published><updated>2009-09-18T09:26:55.866-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET - Not so basic'/><category scheme='http://www.blogger.com/atom/ns#' term='Databinding expressions'/><title type='text'>Difference between Eval and Bind databinding expressions</title><content type='html'>&lt;p&gt;Hi. I just faced a problem where I was using an ASP.NET GridView control which was bound to a SqlDataSource. One of the bound columns was “User” and, of course, brought the user name. What I wanted to do was to add a HyperLink to that text, so that instead of just saying “Mark”, with would show up as a link to the UserDetalis page, passing “Mark” as a QueryString variable. Something like this:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt; &lt;span style="color: #ff0000"&gt;href&lt;/span&gt;='&lt;span style="color: #ff0000"&gt;UserDetails&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;aspx&lt;/span&gt;?&lt;span style="color: #ff0000"&gt;user&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;Mark&lt;/span&gt;&amp;gt;&lt;span style="color: #ff0000"&gt;Mark&lt;/span&gt;&amp;lt;/&lt;span style="color: #ff0000"&gt;a&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;So, because I had a databound GridView, I just had to make the “User” column a template column and replace the standard label (&lt;strong&gt;Label1&lt;/strong&gt; in this code)&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #c71585"&gt;asp&lt;/span&gt;:&lt;span style="color: #800000"&gt;GridView&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ID&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;myGridView&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;runat&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2:     &lt;span style="color: #ff0000"&gt;DataSourceID&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;myDataSource&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Columns&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:         &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #c71585"&gt;asp&lt;/span&gt;:&lt;span style="color: #800000"&gt;TemplateField&lt;/span&gt; &lt;span style="color: #ff0000"&gt;HeaderText&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;User&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;SortExpression&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:             &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemTemplate&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:                 &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #c71585"&gt;asp&lt;/span&gt;:&lt;span style="color: #800000"&gt;Label&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ID&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;Label1&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;runat&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Text&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;'&amp;lt;%# Bind(&amp;quot;User&amp;quot;) %&amp;gt;'&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:             &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ItemTemplate&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8:         &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #c71585"&gt;asp&lt;/span&gt;:&lt;span style="color: #800000"&gt;TemplateField&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9:     &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Columns&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10: &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #c71585"&gt;asp&lt;/span&gt;:&lt;span style="color: #800000"&gt;GridView&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;by a HyperLink (&lt;strong&gt;HyperLink1&lt;/strong&gt; in this code), and set its NavigateUrl property to the new value, which held the whole hyperlink HTML text. So the first thing that came to my mind was to do something like this:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #c71585"&gt;asp&lt;/span&gt;:&lt;span style="color: #800000"&gt;HyperLink&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ID&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;HyperLink1&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;runat&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Text&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;'&amp;lt;%# Bind(&amp;quot;User&amp;quot;) %&amp;gt;'&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2:     &lt;span style="color: #ff0000"&gt;NavigateUrl&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;'&amp;lt;%# String.Format(&amp;quot;UserDetails.aspx?user={0}&amp;quot;, Bind(&amp;quot;User&amp;quot;)) %&amp;gt;'&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;But it turned out I got a runtime error that said: &lt;strong&gt;The name 'Bind' does not exist in the current context&lt;/strong&gt;. If I used Eval instead of Bind:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #c71585"&gt;asp&lt;/span&gt;:&lt;span style="color: #800000"&gt;HyperLink&lt;/span&gt; &lt;span style="color: #ff0000"&gt;ID&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;HyperLink1&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;runat&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Text&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;'&amp;lt;%# Eval(&amp;quot;User&amp;quot;) %&amp;gt;'&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2:     &lt;span style="color: #ff0000"&gt;NavigateUrl&lt;/span&gt;=&lt;span style="color: #0000ff"&gt;'&amp;lt;%# String.Format(&amp;quot;UserDetails.aspx?user={0}&amp;quot;, Eval(&amp;quot;User&amp;quot;)) %&amp;gt;'&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;It worked like a charm. &lt;/p&gt;

&lt;p&gt;OK, I got my problem solved, but now I needed to know the difference between both, and specifically why it worked with one and not the other. Well, here it is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Eval&lt;/strong&gt; is one way, for read-only purposes, as &lt;strong&gt;Bind&lt;/strong&gt; is two way, for read-write operations. &lt;strong&gt;Eval&lt;/strong&gt; is actually a static protected method defined on the TemplateControl class, from which the Page class is derived, and it returns a string. Because it’s a real method, you could combine it with other statements, like the string.Format we just used. &lt;strong&gt;Bind&lt;/strong&gt;, on the other hand, is not an actual method, it’s a new ASP.NET 2.0 databinding keyword, so of course, it can’t be used with other method calls or stuff. Bottom line is, &lt;em&gt;if you need to only read the text, and perhaps change it a little, use &lt;strong&gt;Eval&lt;/strong&gt;. If you need to write back to the db or something like that, use &lt;strong&gt;Bind&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Hope you got it clear. This &lt;a href="http://weblogs.asp.net/leftslipper/archive/2007/06/29/how-asp-net-databinding-deals-with-eval-and-bind-statements.aspx" target="_blank"&gt;article&lt;/a&gt; has some more deep definitions, which you might find useful as well.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-1654966792213410206?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/1654966792213410206/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/09/difference-between-eval-and-bind.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/1654966792213410206'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/1654966792213410206'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/09/difference-between-eval-and-bind.html' title='Difference between Eval and Bind databinding expressions'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-3183063053641505558</id><published>2009-09-15T09:01:00.001-07:00</published><updated>2009-09-15T09:01:04.052-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Expandable objects'/><category scheme='http://www.blogger.com/atom/ns#' term='Converters'/><category scheme='http://www.blogger.com/atom/ns#' term='C# - Not so basic'/><title type='text'>Creating design-time compatible properties and events, part 2</title><content type='html'>&lt;p&gt;Hi all. In &lt;a href="http://hugonne.blogspot.com/2009/08/creating-design-time-compatible.html" target="_blank"&gt;part 1&lt;/a&gt; of this article, we learned how to make our simple properties show up in the properties window at design-time, so that they looked just like the built in ones that .NET controls have. Here, in part 2, we’ll discuss how to make your events show up, as well as some more advanced stuff, like adding custom lists.&lt;/p&gt;  &lt;p&gt;First, events, opposite to Properties, don’t show by default in the properties Windows (that is, they’re not Browsable by default), so in order to show them all we need to do is to set that property to true:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 350px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: [Browsable(&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;event&lt;/span&gt; EventHandler MyEvent;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;It’s all it takes.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://lh3.ggpht.com/_GhBv2tIkK2o/Sq-sh0kM6oI/AAAAAAAABYc/mvOppJTRVZI/Properties5.jpg" /&gt; &lt;/p&gt;

&lt;p&gt;Now, the fun part. Let’s add some custom lists. Suppose we want to add a property to our NiceTextBox called &lt;strong&gt;SomeList&lt;/strong&gt;, which can hold any of these four values: One, Two, Three and Four. There are several ways to achieve this, but I’m gonna take the simple way and use a string. So, first, we’ll have to create a “converter” class. Converter classes are used to convert specific types to and from other different types. Since we’re using a string, we’ll need to create a class that inherits &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.stringconverter.aspx" target="_blank"&gt;System.ComponentModel.StringConverter&lt;/a&gt;. We’'ll use this to convert my list to and from a string.&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; SomeListConverter : StringConverter
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #008000"&gt;//Values list. You could bring this from a database or so&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:     &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] values = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt;[] { &amp;quot;&lt;span style="color: #8b0000"&gt;One&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color: #8b0000"&gt;Two&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color: #8b0000"&gt;Three&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color: #8b0000"&gt;Four&lt;/span&gt;&amp;quot; };
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; GetStandardValuesSupported(ITypeDescriptorContext context)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8:         &lt;span style="color: #008000"&gt;//When true, only a value from the list can be selected&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10:     }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 14:         &lt;span style="color: #008000"&gt;//Return the list as a collection of standard values.&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 15:         &lt;span style="color: #008000"&gt;//This is the method the designer will use to pupulate the list&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 16:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StandardValuesCollection(values);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 17:     }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 18: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 19:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; GetStandardValuesExclusive(ITypeDescriptorContext context)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 20:     {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 21:         &lt;span style="color: #008000"&gt;//When false, the list will be editable, meaning that the user could type &lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 22:         &lt;span style="color: #008000"&gt;//a value as well. When true, the list will be read-only&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 23:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 24:     }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 25: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Now, let’s create the property. We’ll have to use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx" target="_blank"&gt;TypeConverter&lt;/a&gt; attribute to tell the property what class it should use to convert our List (of type StandardValuesCollection) to and from a string:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: [TypeConverter(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(SomeListConverter))]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: [Category(&amp;quot;&lt;span style="color: #8b0000"&gt;Custom properties&lt;/span&gt;&amp;quot;), DefaultValue(&amp;quot;&lt;span style="color: #8b0000"&gt;&lt;/span&gt;&amp;quot;), Description(&amp;quot;&lt;span style="color: #8b0000"&gt;Some custom list used&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SomeList { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;The result will be pretty neat:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://lh6.ggpht.com/_GhBv2tIkK2o/Sq-xkTgJW4I/AAAAAAAABY4/HCEV10M9HBU/Properties6.jpg" /&gt;&lt;/p&gt;

&lt;p&gt;OK, so that’s it for simple lists. But what if one of our custom properties needs to be set through a more complex class? Let’s take a look at the following example, we’ll create a property called Version, which will have all four parts of a regular assembly version: Major, Minor, Build and Private. We’ll do it so that the user can set the version value directly (something like 2.1.0.17), or he can set the value for each part separately. That kind of behavior, like the one we see in common properties as &lt;strong&gt;Location&lt;/strong&gt; and &lt;strong&gt;Size&lt;/strong&gt;, is achieved through something called expandable objects, specifically by using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.expandableobjectconverter.aspx" target="_blank"&gt;ExpandableObjectConverter&lt;/a&gt; class.&lt;/p&gt;

&lt;p&gt;First, we’ll create an ApplicationVersion class:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 350px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ApplicationVersion
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;short&lt;/span&gt; Major { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;short&lt;/span&gt; Minor { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;short&lt;/span&gt; Build { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;short&lt;/span&gt; Private { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;This class we’ll represent the type of our Version property (which we’ll construct later). Now, we’ll have to create the converter. Like said before, because this property will be an expandable object, we’ll have to inherit the ExpandableObjectConverter class:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ApplicationVersionConverter : ExpandableObjectConverter
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; CanConvertTo(ITypeDescriptorContext context, 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:         Type destinationType)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:         &lt;span style="color: #008000"&gt;//This will indicate if the Converter can convert the object TO the &lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7:         &lt;span style="color: #008000"&gt;//specified type. In this case, ApplicationVersion to string&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; destinationType == &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(ApplicationVersion) || 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9:             &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.CanConvertTo(context, destinationType);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10:     }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; CanConvertFrom(ITypeDescriptorContext context, 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13:         Type sourceType)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 14:     {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 15:         &lt;span style="color: #008000"&gt;//This will indicate if the Converter can convert the object FROM the &lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 16:         &lt;span style="color: #008000"&gt;//specified type. In this case, string to ApplicationVersion&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 17:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; sourceType == &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(String) || 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 18:             &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.CanConvertFrom(context, sourceType);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 19:     }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 20: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 21:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; ConvertFrom(ITypeDescriptorContext context, 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 22:         System.Globalization.CultureInfo culture, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 23:     {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 24:         &lt;span style="color: #008000"&gt;//This will implement the logic to convert from string to object. &lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 25:         /It will be used when the user directly enters the version &lt;span style="color: #0000ff"&gt;value&lt;/span&gt; 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 26:         &lt;span style="color: #008000"&gt;//(2.0.4.17) instead of setting each part individually&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 27:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;value&lt;/span&gt; &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 28:             &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 29:             {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 30:                 &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; s = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;.ToString();
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 31:                 &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] versionParts = s.Split('.');
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 32:                 &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (versionParts.Length &amp;gt; 0)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 33:                 {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 34:                     var version = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ApplicationVersion();
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 35:                     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (versionParts[0] != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 36:                         version.Major = Convert.ToInt16(versionParts[0]);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 37:                     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (versionParts[1] != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 38:                         version.Minor = Convert.ToInt16(versionParts[1]);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 39:                     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (versionParts[2] != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 40:                         version.Build = Convert.ToInt16(versionParts[2]);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 41:                     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (versionParts[3] != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 42:                         version.Private = Convert.ToInt16(versionParts[3]);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 43:                     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; version;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 44:                 }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 45:             }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 46:             &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt; (Exception)
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 47:             {
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 48:                 &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentException(
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 49:                     &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 50:                         &amp;quot;&lt;span style="color: #8b0000"&gt;The value {0} isn't a valid Version number&lt;/span&gt;&amp;quot;, &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;));
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 51:             }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 52:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.ConvertFrom(context, culture, &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;);
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 53:     }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 54: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 55:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; ConvertTo(ITypeDescriptorContext context, 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 56:         System.Globalization.CultureInfo culture, 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 57:         &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;, Type destinationType)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 58:     {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 59:         &lt;span style="color: #008000"&gt;//This will implement the logic to convert from object to string. &lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 60:         &lt;span style="color: #008000"&gt;//It serves the exact opposite purpose of the above method&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 61:         &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (destinationType == &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;) &amp;amp;&amp;amp; &lt;span style="color: #0000ff"&gt;value&lt;/span&gt; &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; ApplicationVersion)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 62:         {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 63:             var version = (ApplicationVersion)&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 64:             &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0}.{1}.{2}.{3}&lt;/span&gt;&amp;quot;, version.Major, 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 65:                 version.Minor, version.Build, version.Private);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 66:         }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 67:         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.ConvertTo(context, culture, &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;, destinationType);
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 68:     }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 69: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Finally, we’ll just create our Version property:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: [TypeConverter(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(ApplicationVersionConverter))]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: [Category(&amp;quot;&lt;span style="color: #8b0000"&gt;Custom properties&lt;/span&gt;&amp;quot;), DefaultValue(&amp;quot;&lt;span style="color: #8b0000"&gt;0.0.0.0&lt;/span&gt;&amp;quot;), Description(&amp;quot;&lt;span style="color: #8b0000"&gt;App version&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ApplicationVersion Version { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Here’s what we’ll get:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://lh3.ggpht.com/_GhBv2tIkK2o/Sq-4vF3P6-I/AAAAAAAABZU/hnee9Xw1CR8/Properties7.jpg" /&gt; &lt;/p&gt;

&lt;p&gt;So, it’s possible to create properties and events that are fully design-time compatible. It might take a little work, but it’s definitely worth it, specially for people that create custom controls and plan to share or sell them. Hope you enjoyed the articles.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-3183063053641505558?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/3183063053641505558/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/09/creating-design-time-compatible.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/3183063053641505558'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/3183063053641505558'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/09/creating-design-time-compatible.html' title='Creating design-time compatible properties and events, part 2'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/_GhBv2tIkK2o/Sq-sh0kM6oI/AAAAAAAABYc/mvOppJTRVZI/s72-c/Properties5.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-7195047993897804511</id><published>2009-08-12T07:37:00.001-07:00</published><updated>2009-08-12T07:37:46.920-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# - Not so basic'/><title type='text'>Creating design-time compatible properties and events, part 1</title><content type='html'>&lt;p&gt;We all create custom or user controls and classes all day and, of course, custom properties and events. Well, whenever you create you own control, it’s properties are automatically show in the property window at design time. But our properties don’t look the same as the built in ones for objects, right? Those have got categories, descriptions, lists with custom values, and so on. And for custom events, they don’t even show up! &lt;/p&gt;  &lt;p&gt;In this two part article, I’ll be showing you how to make your properties and events more “developer friendly” (nice term, right?). In part 1, I’ll be talking about simple properties, and in part 2 we’ll get to events and custom lists.&lt;/p&gt;  &lt;p&gt;This is what any built in property for, let’s say, a TextBox looks like:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img src="http://lh5.ggpht.com/_GhBv2tIkK2o/SoLHhS9lPwI/AAAAAAAABXA/nux6n9E9Tu4/Properties1.jpg" /&gt; &lt;/p&gt;  &lt;p&gt;We got:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;A category &lt;/li&gt;    &lt;li&gt;A custom list, which provides from an enumeration &lt;/li&gt;    &lt;li&gt;A description of what the property does &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;We can get that for our properties and events as well, so first, to continue with the example, let’s create or custom TextBox, which will basically be a class that inherits from the TextBox class, and add a couple of properties:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; partial &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; NiceTextBox : TextBox
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SomeString { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: 
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; SomeInt { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Nice. Compile and add a NiceTextBox control to the form. Our two very clever properties will show in the Properties Window, but they’ll be under the “Misc” category (meaning they don’t have a category at all), and they will have no comment:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;img src="http://lh6.ggpht.com/_GhBv2tIkK2o/SoLG3bWaYeI/AAAAAAAABWg/Ra4AimYIPIY/Properties2.jpg" /&gt;&amp;#160; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;In order to make our properties “look” that way, we need to use a concept called &lt;strong&gt;Attributes&lt;/strong&gt;. &lt;a href="http://msdn.microsoft.com/en-us/library/z0w1kczw(VS.80).aspx" target="_blank"&gt;Attributes&lt;/a&gt; provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). In our case, these attributes will “kinda” represent the metadata used by the designer in the properties window. &lt;/p&gt;

&lt;p&gt;So, to create a category for our couple of properties, will use the “Category” attribute, like this:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: [Category(&amp;quot;&lt;span style="color: #8b0000"&gt;Custom properties&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SomeString { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: [Category(&amp;quot;&lt;span style="color: #8b0000"&gt;Custom properties&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; SomeInt { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Here’s what we’ll get:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://lh5.ggpht.com/_GhBv2tIkK2o/SoLMRp50wAI/AAAAAAAABXc/0erTt6A11bw/Properties3.jpg" /&gt; &lt;/p&gt;

&lt;p&gt;Of course, the category name, which is the one in double quotes, can be anything, and it doesn’t have to be the same for all properties. &lt;/p&gt;

&lt;p&gt;Just as the Category attribute, we can get a lot of things by using more of them, here’s a few:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;font face="Courier New"&gt;&lt;strong&gt;DefaultValue&lt;/strong&gt;&lt;/font&gt; – Value to set for the property by default &lt;/li&gt;

  &lt;li&gt;&lt;font face="Courier New"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/font&gt; – Description of what he property does &lt;/li&gt;

  &lt;li&gt;&lt;font face="Courier New"&gt;&lt;strong&gt;ReadOnly&lt;/strong&gt;&lt;/font&gt; – Indicates if the property can be changed at design time in the properties window. It can still be changed via code though. By default, this attribute is false. &lt;/li&gt;

  &lt;li&gt;&lt;font face="Courier New"&gt;&lt;strong&gt;DisplayName&lt;/strong&gt;&lt;/font&gt; – The name to show in the properties window &lt;/li&gt;

  &lt;li&gt;&lt;font face="Courier New"&gt;&lt;strong&gt;Browsable&lt;/strong&gt;&lt;/font&gt; – Indicates if the property should be shown at all at design time in the properties window. When set to false, it won’t be visible, although you can still access it at runtime, in your code. By default, this attribute is true, except for events, which is by default false. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, let’s elaborate our properties a bit more:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: [Category(&amp;quot;&lt;span style="color: #8b0000"&gt;Custom properties&lt;/span&gt;&amp;quot;), Description(&amp;quot;&lt;span style="color: #8b0000"&gt;Some string used&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SomeString { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: 
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: [Category(&amp;quot;&lt;span style="color: #8b0000"&gt;Custom properties&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: [Description(&amp;quot;&lt;span style="color: #8b0000"&gt;Some number used in the NiceTextBox&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: [ReadOnly(&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: [DisplayName(&amp;quot;&lt;span style="color: #8b0000"&gt;SomeNumber&lt;/span&gt;&amp;quot;)]
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; SomeInt { &lt;span style="color: #0000ff"&gt;get&lt;/span&gt;; &lt;span style="color: #0000ff"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Notice that, in the first property, SomeString, we set all attributes in the same line, separating them by commas, and in the second property, SomeInt, we did it in different lines. Both approaches are valid. So, this is what we get:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://lh6.ggpht.com/_GhBv2tIkK2o/SoLQg790G1I/AAAAAAAABX4/ZXK23Ehpaho/Properties4.jpg" /&gt;&lt;/p&gt;

&lt;p&gt;As put in our attributes, we included a description for both properties. In addition, SomeInt property will be read-only at design time, and will be shown as SomeNumber. &lt;/p&gt;

&lt;p&gt;Of course, there are a lot more attributes than the ones I listed, but I hope you get the general idea. In part 2 of this article, I’ll be showing you some more advanced features. See you then.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-7195047993897804511?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/7195047993897804511/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/08/creating-design-time-compatible.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7195047993897804511'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7195047993897804511'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/08/creating-design-time-compatible.html' title='Creating design-time compatible properties and events, part 1'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_GhBv2tIkK2o/SoLHhS9lPwI/AAAAAAAABXA/nux6n9E9Tu4/s72-c/Properties1.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-5106074212501838766</id><published>2009-08-05T15:03:00.001-07:00</published><updated>2009-08-10T13:08:45.134-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# - Not so basic'/><title type='text'>The String.Format method</title><content type='html'>&lt;p&gt;Believe it or not, one of the most powerful classes in the .NET Framework is the &lt;strong&gt;String&lt;/strong&gt; class. Yep, you read it right, the &lt;strong&gt;String&lt;/strong&gt; class. It has about ten millions uses, and every other class can be converted to one.&lt;/p&gt;  &lt;p&gt;The String class also has some pretty cool methods: &lt;strong&gt;String.Contains&lt;/strong&gt; and &lt;strong&gt;String.IsNullOrEmpty&lt;/strong&gt; are two of my favorites, no to mention a whole other bunch of methods that every programmer uses on a a daily basis: &lt;strong&gt;Substring&lt;/strong&gt;, &lt;strong&gt;CompareTo&lt;/strong&gt;, &lt;strong&gt;IndexOf&lt;/strong&gt;, &lt;strong&gt;Replace&lt;/strong&gt;, and so on. The &lt;a href="http://msdn.microsoft.com/en-us/library/system.string_methods.aspx" target="_blank"&gt;list&lt;/a&gt; is pretty huge, and they all have great uses. Specially the &lt;strong&gt;String.Format&lt;/strong&gt; method, which is what I’ll talk about in this post.&lt;/p&gt;  &lt;p&gt;Many .NET developers use &lt;strong&gt;String.Fomat&lt;/strong&gt;, but from my experience, most of them only use it to concatenate strings in a more fancy, readable way. So, instead of writing this:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; s = &amp;quot;&lt;span style="color: #8b0000"&gt;Your name is, '&lt;/span&gt;&amp;quot; + name + &amp;quot;&lt;span style="color: #8b0000"&gt;' and your age is &lt;/span&gt;&amp;quot; + age;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;They’ll write this:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; s = &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;Your name is '{0}' and your age is {1}&lt;/span&gt;&amp;quot;, name, age);&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;What tat line says is “&lt;em&gt;replace the {0} symbol with the first variable and the {1} symbol with the second variable&lt;/em&gt;” (and so on). Even though that’s a good start, and it’s a better solution performance-wise, there’s really a lot more we can get from the String.Format method. Take a look at this example (the dots at the beginning and end of the string are just for reference):&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;.{0,10}.&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color: #8b0000"&gt;right&lt;/span&gt;&amp;quot;) &lt;span style="color: #008000"&gt;//&amp;quot;.     right.&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Here we also have the {0} symbol, which will be replaced by the first variable, “right” in this case, but we’re also telling the method to fill with blank spaces the rest of the string until it reaches 10 characters long. The positive 10 indicates that the variable text should be placed to the right of those spaces. So if used a negative number, we’ll get the variable value to the left:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;.{0,-10}.&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color: #8b0000"&gt;left&lt;/span&gt;&amp;quot;); &lt;span style="color: #008000"&gt;// &amp;quot;.left      .&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;We could also format numbers and dates, like in this examples:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:g}&lt;/span&gt;&amp;quot;, DateTime.Now); &lt;span style="color: #008000"&gt;//5/08/2009 04:02 PM&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0,-9:g5}&lt;/span&gt;&amp;quot;, 3.14159); &lt;span style="color: #008000"&gt;// &amp;quot;3.1416   &amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;In line 1, what we said was “&lt;em&gt;give me the current date and time with a general (g) format&lt;/em&gt;”. In line 2, it was something like “&lt;em&gt;give me the number 3.14159, formatted to deliver 5 decimal places (which include the decimal point, so ‘g5’ will give only 4 decimal numbers), and complete result, aligned to the right, with spaces to reach 9 characters&lt;/em&gt;”. &lt;/p&gt;

&lt;p&gt;Is the above examples, the ‘g’ character is called a &lt;strong&gt;Formatting Specifier&lt;/strong&gt;. with a number to the right of a formatting specifier, we specify the number of decimal places. Here are all the others:&lt;/p&gt;

&lt;h5&gt;Numeric formatting specifiers&lt;/h5&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; x = 12345;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:g}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// 12345 (general)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:c}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// $12,345 (currency)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:e}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// 1.234500e+004 (scientific notation)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:f}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// 12345.00 (fixed-point)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:p}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// 12,345% (percent)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:r}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// 12345 (rounded)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:n}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// 12,345.00 (number with commas)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:d}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// 12345 (decimal)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:x}&lt;/span&gt;&amp;quot;, x); &lt;span style="color: #008000"&gt;// 3039 (hexadecimal)&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;h5&gt;Date and time formatting specifiers&lt;/h5&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; a = DateTime.Now;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:d}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 8/5/2009 (short date)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:D}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// Wednesday, 5 August 2009 (long date)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:f}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// Wednesday, 5 August 2009 04:35 PM (full short)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:F}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// Wednesday, 5 August 2009 04:35:17 PM (full long)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:g}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 5/08/2009 04:35 PM (general)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:G}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 5/08/2009 04:35:17 PM (general long)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:m}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 5 August (month day)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:o}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 2009-08-05T16:35:17.4687500-05:00 (round trip)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:R}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// Wed, 5 August 2009 16:35:17 GMT (RFC1123 pattern)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:s}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 2009-08-5T16:35:17 (sortable)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:t}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 04:35 PM (short time)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:T}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 04:35:17 PM (long time)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 14: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:u}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// 2009-08-5 16:35:17Z (universal)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 15: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:U}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// Wednesday, 5 August 2009 4:35 PM (Universal GMT)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 16: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:Y}&lt;/span&gt;&amp;quot;, a); &lt;span style="color: #008000"&gt;// August 2009 (Year month)&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Besides, you can always use custom formats for both numbers and dates:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #008000"&gt;//Numbers&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:0.0}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// 1234.6 (decimal point)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:0,0}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// 1,235 (thousands)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:0,.}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// 1 (number scaling)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:0%}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// 123456% (percent)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:00e+0}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// 12e+2 (scientific notation)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: &lt;span style="color: #008000"&gt;//Dates an times&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  8: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:MMM}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// Aug (month abbr)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  9: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:MMMM}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// August (month full)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 10: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:ss}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// 40 (seconds)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 11: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:tt}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// PM (am/pm)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 12: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:yy}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// 09 (year)&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt; 13: &amp;quot;&lt;span style="color: #8b0000"&gt;{0:yyyy}&lt;/span&gt;&amp;quot; &lt;span style="color: #008000"&gt;// 2009 (year full)&lt;/span&gt;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;A complete document about custom formats can de found &lt;a href="http://msdn.microsoft.com/en-us/library/0c899ak8.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The final thing I want to talk about in the String.Format method is the &lt;strong&gt;conditional formatting&lt;/strong&gt;. Let’s see:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; x = 1;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:positive;negative;zero}&lt;/span&gt;&amp;quot;, x);&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;The output of the String.Format depends on the value of the variable x. If the value is &amp;gt; 0, the output will be the word “positive”, which takes the first position after the “0:”. Obviously, if it’s &amp;lt; 0, it will display the word “negative”, and if it’s == 0, then it’ll show “zero”. Another example is:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; padding-left: 5px; width: 550px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&amp;quot;&lt;span style="color: #8b0000"&gt;{0:$#,##0.00;($#,##0.00);-}&lt;/span&gt;&amp;quot;, x);&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;In this case, we’re not only formatting the number as a currency value, but also using parenthesis for negative values and a dash for zeroes.&lt;/p&gt;

&lt;p&gt;So, getting to really know the String class, and in this particular case the String.Format method, is something that will open up a world of possibilities. Remember, most of the times the simplest things are the most important ones.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-5106074212501838766?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/5106074212501838766/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/08/stringformat-method.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/5106074212501838766'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/5106074212501838766'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/08/stringformat-method.html' title='The String.Format method'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-315780629256024673</id><published>2009-07-31T08:03:00.001-07:00</published><updated>2009-08-05T15:10:38.664-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# - Basic'/><title type='text'>Swap two variable values</title><content type='html'>&lt;p&gt;That’s the typical example when we’re all learning how to code, swap the values of a and b. Well, here’s what they probably taught us:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 250px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a = 4;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b = 3;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; temp;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: &lt;span style="color: #008000"&gt;//Swap&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: temp = a;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: a = b;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: b = temp;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;And here’s the way to do it with out using the temporary variable, with just boolean logic, and the ever underestimated XOR (^):&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 250px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a = 4;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b = 3;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: &lt;span style="color: #008000"&gt;//Swap&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: a = a ^ b;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: b = a ^ b;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6: a = a ^ b;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Another way, with just pluses and minuses…&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 250px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a = 4;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b = 3;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3: a -= b;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: b += a;
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5: a = b - a;&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;I’ve actually seen many ways to do it, specially on this &lt;a href="http://www.devmaster.net/forums/showthread.php?t=428" target="_blank"&gt;thread&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-315780629256024673?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/315780629256024673/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/07/swap-two-variable-values.html#comment-form' title='2 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/315780629256024673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/315780629256024673'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/07/swap-two-variable-values.html' title='Swap two variable values'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-7571467080505651433</id><published>2009-07-30T10:02:00.001-07:00</published><updated>2009-08-05T15:10:52.357-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# - Basic'/><title type='text'>Difference between logical operators in C#</title><content type='html'>&lt;p&gt;Yes, you’ve probably seen that the AND and OR logical operators have two ways of being written in C#: The first way is like this: &amp;amp;&amp;amp; (AND), || (OR), and the second is this: &amp;amp; (AND), | (OR). In case you didn’t notice the difference, the first ones use a double operator, while the last ones use only one. &lt;/p&gt;  &lt;p&gt;As you already might know, &lt;strong&gt;AND operator returns true if both sides of an expression are true&lt;/strong&gt;, and &lt;strong&gt;OR operator returns true if at least one side of the expression is true&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Even though most of us programmers actually know that there are two “ways” to write the AND and OR operators, few know (that was my case before I looked it up) the difference between both. Well, here it is:&lt;/p&gt;  &lt;p&gt;When you use &lt;strong&gt;double logical operators&lt;/strong&gt; (which is probably most of the time), &lt;strong&gt;the compiler only evaluates the right part of the expression if the first one doesn’t already make the expression false&lt;/strong&gt;. The opposite happens when you use &lt;strong&gt;single logical operators&lt;/strong&gt;: &lt;strong&gt;both sides of the expression get evaluated no matter what&lt;/strong&gt;. Let’s clear that out with an example:&lt;/p&gt;  &lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 463px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(myDataSet.Tables.Count &amp;gt; 0 &amp;amp;&amp;amp; myDataSet.Tables[0].Rows.Count &amp;gt; 0)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:    &lt;span style="color: #008000"&gt;//Do something&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Because we’re using the double AND operator (&amp;amp;&amp;amp;), the compiler will automatically mark the whole expression as &lt;strong&gt;false&lt;/strong&gt; if it finds that the left side of it is already false, without even bothering to evaluate the right side. This is very useful in this specific case with the DataSet, because sometimes we need to evaluate both that the DataSet has a table, and that table has at least one row. That will save us having to use two nested if statements:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 467px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(myDataSet.Tables.Count &amp;gt; 0)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(myDataSet.Tables[0].Rows.Count &amp;gt; 0)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4:    {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  5:       &lt;span style="color: #008000"&gt;//Do something&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  6:    }
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  7: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Now, if we use only the single AND operator, the compiler will evaluate both sides of the expression no matter what, so this code:&lt;/p&gt;

&lt;pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 463px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  1: &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(myDataSet.Tables.Count &amp;gt; 0 &amp;amp; myDataSet.Tables[0].Rows.Count &amp;gt; 0)
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  2: {
&lt;/pre&gt;&lt;pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  3:    &lt;span style="color: #008000"&gt;//Do something&lt;/span&gt;
&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 11px"&gt;  4: }&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;will result in an &lt;strong&gt;ArgumentNullException&lt;/strong&gt; if our DataSet doesn’t have a table, caused by the myDataSet.Tables[0].Rows.Count &amp;gt; 0 comparison.&lt;/p&gt;

&lt;p&gt;There’s one more logical operator, the &lt;strong&gt;XOR (^), which will return true only if both sides of the expression are different (one is true and the other is false)&lt;/strong&gt;. This, in practical terms, is the same as the DIFFERENT (!=) operator, and it doesn’t have a “double” counterpart, because obviously in this case, both sides of the expression have to always be evaluated.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-7571467080505651433?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/7571467080505651433/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/07/difference-between-logical-operators-in.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7571467080505651433'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/7571467080505651433'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/07/difference-between-logical-operators-in.html' title='Difference between logical operators in C#'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-300439418265524101.post-6393271772810478538</id><published>2009-07-30T09:20:00.001-07:00</published><updated>2009-07-30T17:54:14.889-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Blog thoughts'/><title type='text'>How could you NOT blog?</title><content type='html'>&lt;p&gt;Blogs are everywhere, and it’s so easy to create one that it’s impossible, specially if you’re in the programming world, not to get influenced by the hype. I actually did that a few years ago, but this is probably the first time I really want to make a serious blog, which might be helpful to people. &lt;/p&gt;  &lt;p&gt;All my previous blogs were written in Spanish (I’m form Colombia, South America), but i think that a a blog in English could get to more people. Besides, this is a blog about programming (yet another one, I know, but what can you do?), and most people, even non-English speakers, would search the Web in English when it comes to those topics.&lt;/p&gt;  &lt;p&gt;Anyway, I still have my Spanish programming blog, &lt;a href="http://puropuntonet.blogspot.com"&gt;Puro Punto Net&lt;/a&gt;, and I’m sure planning on not quitting on it.&lt;/p&gt;  &lt;p&gt;Hope you people find this blog interesting, and any comments or critiques are well appreciated. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/300439418265524101-6393271772810478538?l=hugonne.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hugonne.blogspot.com/feeds/6393271772810478538/comments/default' title='Enviar comentarios'/><link rel='replies' type='text/html' href='http://hugonne.blogspot.com/2009/07/how-could-you-not-blog.html#comment-form' title='0 comentarios'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/6393271772810478538'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/300439418265524101/posts/default/6393271772810478538'/><link rel='alternate' type='text/html' href='http://hugonne.blogspot.com/2009/07/how-could-you-not-blog.html' title='How could you NOT blog?'/><author><name>Hugonne</name><uri>http://www.blogger.com/profile/15901459733844224876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='http://2.bp.blogspot.com/-4engeL0nSwI/TnO_kzJxiuI/AAAAAAAABs4/nNpiygPs_kw/s220/Twitter01.png'/></author><thr:total>0</thr:total></entry></feed>
