<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>User Controls</title>
	<atom:link href="http://usercontrols.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://usercontrols.wordpress.com</link>
	<description>User Controls, Made Development Easy</description>
	<lastBuildDate>Fri, 19 Oct 2007 07:25:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='usercontrols.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>User Controls</title>
		<link>http://usercontrols.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://usercontrols.wordpress.com/osd.xml" title="User Controls" />
	<atom:link rel='hub' href='http://usercontrols.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Type Converters</title>
		<link>http://usercontrols.wordpress.com/2007/10/01/type-converters/</link>
		<comments>http://usercontrols.wordpress.com/2007/10/01/type-converters/#comments</comments>
		<pubDate>Mon, 01 Oct 2007 13:53:00 +0000</pubDate>
		<dc:creator>Gunatit</dc:creator>
				<category><![CDATA[Design Time Integration]]></category>

		<guid isPermaLink="false">http://usercontrols.wordpress.com/2007/10/01/type-converters/</guid>
		<description><![CDATA[Type converters allow you to convert values between different types of data; they can be implemented to convert values between built-in types and custom types. Type converters also provide the infrastructure to enable configuring a property at design time through a Property browser, and produce the required initialization code.When you select a component on a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usercontrols.wordpress.com&amp;blog=1812862&amp;post=3&amp;subd=usercontrols&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:130%;">Type converters allow you to convert values between different types of data; they can be<br />
implemented to convert values between built-in types and custom types. Type converters also<br />
provide the infrastructure to enable configuring a property at design time through a Property<br />
browser, and produce the required initialization code.</span><span style="font-size:130%;">When you select a component on a design surface, the entries in the Property Browser are rendered from the design-time control instance. When you edit properties in the Property Browser, the component&#8217;s design-time instance is updated with the new property values. This synchronization isn&#8217;t as straightforward as it seems, however, because the Property Browser displays properties only as text, even though the source properties can be of any type. As values shuttle between the Property Browser and the design-time instance, they must be converted back and forth between the string type and the type of the property.</span><span style="font-size:130%;">The type converter, the translator of .NET, whose main goal in life is to convert between types. For string-to-type conversion, a type converter is used for each property displayed in the Property Browser.</span></p>
<p><span style="font-size:130%;"></span><span style="font-size:130%;"><a href="http://bp3.blogger.com/_O7IPpuNfcEg/RwD4rWdEPiI/AAAAAAAAACs/iCz3nF_RobY/s1600-h/WinFormsCSharp9-20.jpg"></a></span><span style="font-size:130%;"><a href="http://bp3.blogger.com/_O7IPpuNfcEg/RwD4rWdEPiI/AAAAAAAAACs/iCz3nF_RobY/s1600-h/WinFormsCSharp9-20.jpg"><img border="0" src="http://bp3.blogger.com/_O7IPpuNfcEg/RwD4rWdEPiI/AAAAAAAAACs/iCz3nF_RobY/s400/WinFormsCSharp9-20.jpg" style="display:block;cursor:pointer;text-align:center;margin:0 auto 10px;" /></a></span><span style="font-size:130%;"><br />
.NET offers the <code>TypeConverter</code> class (from the <code>System.ComponentModel</code> namespace) as the base implementation type converter. .NET also gives you several derivations—including <code>StringConverter</code>, <code>Int32Converter</code>, and <code>DateTimeConverter</code>—that support conversion between common .NET types. If you know the type that needs conversion at compile time, you can create an appropriate converter directly:</span></p>
<pre><span style="font-size:130%;color:#008080;font-style:italic;">// Type is known at compile time</span></pre>
<pre><span style="font-size:130%;color:#008080;font-style:italic;"></span><span style="font-size:130%;">TypeConverter converter = </span><span style="font-size:130%;color:#0600ff;">new</span><span style="font-size:130%;"> Int32Converter</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;">;</span></pre>
<p style="text-align:justify;"><span style="font-size:130%;">Or, if you don&#8217;t know the type that needs conversion until run time, let the <code>TypeDescriptor</code> class (from the <code>System.ComponentModel</code> namespace) make the choice for you:<br />
</span></p>
<pre><span style="font-size:130%;color:#008080;font-style:italic;">// Don't know the type before run time</span></pre>
<pre><span style="font-size:130%;color:#008080;font-style:italic;"></span><span style="font-size:130%;"></span><span style="font-size:130%;color:#0600ff;">object</span><span style="font-size:130%;"> myData = </span><span style="font-size:130%;color:#ff0000;">0</span><span style="font-size:130%;">;</span></pre>
<pre><span style="font-size:130%;">TypeConverter converter = </span></pre>
<pre><span style="font-size:130%;">TypeDescriptor.</span><span style="font-size:130%;color:#000000;">GetConverter</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;">myData.</span><span style="font-size:130%;color:#000000;">GetType</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;">;</span></pre>
<p style="text-align:justify;"><span style="font-size:130%;">The <code>TypeDescriptor</code> class provides information about a particular type or object, including methods, properties, events, and attributes. <code>TypeDescriptor.GetConverter</code> evaluates a type to determine a suitable <code>TypeConverter</code> based on the following:<br />
</span></p>
<dl>
<dd><span style="font-size:130%;">1. Checking whether a type is adorned with an attribute that specifies a particular type converter. </span></dd>
</dl>
<dl>
<dd><span style="font-size:130%;">2. Comparing the type against the set of built-in type converters. </span></dd>
</dl>
<dl>
<dd><span style="font-size:130%;">3. Returning the <code>TypeConverter</code> base if no other type converters are found.</span></dd>
</dl>
<p><span style="font-size:130%;">Because the Property Browser is designed to display the properties of any component, it can&#8217;t know specific property types in advance. Consequently, it relies on <code>TypeDescriptor.GetConverter</code> to dynamically select the most appropriate type converter for each property.</span><span style="font-size:130%;">After a type converter is chosen, the Property Browser and the design-time instance can perform the required conversions, using the same fundamental steps as those shown in the following code:</span></p>
<pre><span style="font-size:130%;color:#008080;font-style:italic;">// Create the appropriate type converter</span></pre>
<pre><span style="font-size:130%;color:#008080;font-style:italic;"></span><span style="font-size:130%;"></span><span style="font-size:130%;color:#0600ff;">object</span><span style="font-size:130%;"> myData = </span><span style="font-size:130%;color:#ff0000;">0</span><span style="font-size:130%;">;</span></pre>
<pre><span style="font-size:130%;">TypeConverter converter = </span></pre>
<pre><span style="font-size:130%;">TypeDescriptor.</span><span style="font-size:130%;color:#000000;">GetConverter</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;">myData.</span><span style="font-size:130%;color:#000000;">GetType</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;">;</span></pre>
<pre><span style="font-size:130%;"><span style="font-size:130%;color:#008080;font-style:italic;">// Can converter convert int to string?</span>

</span></pre>
<pre><span style="font-size:130%;color:#008080;font-style:italic;"></span><span style="font-size:130%;"></span><span style="font-size:130%;color:#0600ff;">if</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;"> converter.</span><span style="font-size:130%;color:#000000;">CanConvertTo</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#0600ff;">typeof</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#0600ff;">string</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;"> </span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;"> </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#000000;">{</span><span style="font-size:130%;">  </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#008080;font-style:italic;">// Convert it</span><span style="font-size:130%;">  </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#0600ff;">object</span><span style="font-size:130%;"> intToString = converter.</span><span style="font-size:130%;color:#000000;">ConvertTo</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#ff0000;">77</span><span style="font-size:130%;">, </span><span style="font-size:130%;color:#0600ff;">typeof</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#0600ff;">string</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;">;  </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#000000;">}</span></pre>
<pre><span style="font-size:130%;color:#000000;"></span><span style="font-size:130%;"><span style="font-size:130%;color:#008080;font-style:italic;">// Can converter convert string to int?</span>

</span></pre>
<pre><span style="font-size:130%;color:#008080;font-style:italic;"></span><span style="font-size:130%;"></span><span style="font-size:130%;color:#0600ff;">if</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;"> converter.</span><span style="font-size:130%;color:#000000;">CanConvertFrom</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#0600ff;">typeof</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#0600ff;">string</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;"> </span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;"> </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#000000;">{</span><span style="font-size:130%;">  </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#008080;font-style:italic;">// Convert it</span><span style="font-size:130%;">  </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#0600ff;">object</span><span style="font-size:130%;"> stringToInt = converter.</span><span style="font-size:130%;color:#000000;">ConvertFrom</span><span style="font-size:130%;color:#000000;">(</span><span style="font-size:130%;color:#a31515;">"77"</span><span style="font-size:130%;color:#000000;">)</span><span style="font-size:130%;">;  </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#000000;">}</span></pre>
<p><span style="font-size:130%;">When the Property Browser renders itself, it uses the type converter to convert each design-time instance property to a string representation using the following steps:<br />
</span></p>
<dl>
<dd><span style="font-size:130%;">1. <code>CanConvertTo</code>: Can you convert from the design-time property type to a string? </span></dd>
</dl>
<dl>
<dd><span style="font-size:130%;">2. <code>ConvertTo</code>: If so, please convert property value to string. </span></dd>
</dl>
<p><span style="font-size:130%;">The string representation of the source value is then displayed at the property&#8217;s entry in the Property Browser. If the property is edited and the value is changed, the Property Browser uses the next steps to convert the string back to the source property value:<br />
</span></p>
<dl>
<dd><span style="font-size:130%;">1. <code>CanConvertFrom</code>: Can you convert back to the design-time property type? </span></dd>
</dl>
<dl>
<dd><span style="font-size:130%;">2. <code>ConvertFrom</code>: If so, please convert string to property value. </span></dd>
</dl>
<p><span style="font-size:130%;">Some intrinsic type converters can do more than just convert between simple types. To demonstrate, let&#8217;s expose a <code>Face</code> property of type <code>ClockFace</code>, allowing developers to decide how the clock is displayed, including options for <code>Analog</code>, <code>Digital</code>, or <code>Both</code>:<br />
</span></p>
<pre><span style="font-size:130%;color:#0600ff;">public</span><span style="font-size:130%;"> </span><span style="font-size:130%;color:#0600ff;">enum</span><span style="font-size:130%;"> ClockFace </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#000000;">{</span><span style="font-size:130%;">Analog = </span><span style="font-size:130%;color:#ff0000;">0</span><span style="font-size:130%;">,Digital = </span><span style="font-size:130%;color:#ff0000;">1</span><span style="font-size:130%;">,Both = </span><span style="font-size:130%;color:#ff0000;">2</span><span style="font-size:130%;"></span><span style="font-size:130%;color:#000000;">};</span></pre>
<pre><span style="font-size:130%;color:#000000;"></span><span style="font-size:130%;"><span style="font-size:130%;color:#0600ff;">class</span><span style="font-size:130%;"> ClockControl : Control </span>

</span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#000000;">{</span></pre>
<pre><span style="font-size:130%;color:#000000;"></span><span style="font-size:130%;">ClockFace face = ClockFace.</span><span style="font-size:130%;color:#000000;">Both</span><span style="font-size:130%;">;</span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#0600ff;">public</span><span style="font-size:130%;"> ClockFace Face </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;color:#000000;">{</span><span style="font-size:130%;"> </span></pre>
<pre><span style="font-size:130%;">get </span><span style="font-size:130%;color:#000000;">{</span><span style="font-size:130%;"> ... </span><span style="font-size:130%;color:#000000;">}</span><span style="font-size:130%;"> </span></pre>
<pre><span style="font-size:130%;">set </span><span style="font-size:130%;color:#000000;">{</span><span style="font-size:130%;"> ... </span><span style="font-size:130%;color:#000000;">}</span><span style="font-size:130%;"> </span><span style="font-size:130%;color:#000000;">}</span><span style="font-size:130%;">...

 </span></pre>
<pre><span style="font-size:130%;"></span><span style="font-size:130%;"></span><span style="font-size:130%;"><span style="font-size:130%;color:#000000;">}</span>
</span></pre>
<p><span style="font-size:130%;"><code>TypeDescriptor.GetConverter</code> returns an <code>EnumConverter</code>, which contains the smarts to examine the source enumeration and convert it to a drop-down list of descriptive string values.</span><span style="font-size:130%;"></span></p>
<p><span style="font-size:130%;"></span></p>
<p><span style="font-size:130%;"><a href="http://bp1.blogger.com/_O7IPpuNfcEg/RwD7c2dEPjI/AAAAAAAAAC0/Id40ZIqUy8Y/s1600-h/WinFormsCSharp9-21.jpg"><img border="0" src="http://bp1.blogger.com/_O7IPpuNfcEg/RwD7c2dEPjI/AAAAAAAAAC0/Id40ZIqUy8Y/s400/WinFormsCSharp9-21.jpg" style="display:block;cursor:pointer;text-align:center;margin:0 auto 10px;" /></a></span></p>
<p><span style="font-size:130%;">Enumeration Type Displayed in the Property Browser via EnumConverter.</span></p>
<p><span style="font-size:130%;"></span></p>
<p><span style="font-size:130%;"></span></p>
<p><span style="font-size:130%;"></span></p>
<p><span style="font-size:130%;"></span></p>
<p><span style="font-size:130%;">Reference <a href="http://msdn.microsoft.com">http://msdn.microsoft.com</a></span></p>
<p><span style="font-size:130%;"></span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/usercontrols.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/usercontrols.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usercontrols.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/usercontrols.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/usercontrols.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/usercontrols.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/usercontrols.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/usercontrols.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/usercontrols.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/usercontrols.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/usercontrols.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/usercontrols.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/usercontrols.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/usercontrols.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/usercontrols.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/usercontrols.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=usercontrols.wordpress.com&amp;blog=1812862&amp;post=3&amp;subd=usercontrols&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://usercontrols.wordpress.com/2007/10/01/type-converters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fb4775938525761090dbafa5890a0b86?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Gunatit</media:title>
		</media:content>

		<media:content url="http://bp3.blogger.com/_O7IPpuNfcEg/RwD4rWdEPiI/AAAAAAAAACs/iCz3nF_RobY/s400/WinFormsCSharp9-20.jpg" medium="image" />

		<media:content url="http://bp1.blogger.com/_O7IPpuNfcEg/RwD7c2dEPjI/AAAAAAAAAC0/Id40ZIqUy8Y/s400/WinFormsCSharp9-21.jpg" medium="image" />
	</item>
	</channel>
</rss>
