tag:blogger.com,1999:blog-21615768571044082092024-03-21T12:42:55.830-07:00Codes DirectoryIt is all about my knowledge consolidation, nothing more!Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.comBlogger36125tag:blogger.com,1999:blog-2161576857104408209.post-75438089062514732192018-09-01T09:18:00.000-07:002018-09-06T10:20:43.947-07:00RepoDb: .Net Lightweight ORM Library Extreme Performance<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;">What is RepoDb?</span></b><br />
<br />
<span style="font-family: "arial" , "helvetica" , sans-serif;">A dynamic, lightweight, and fast repository-based .Net ORM Library.</span><br />
<span style="background-color: #fafbfc; color: #666666; font-size: 14px;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b>Package</b>: <a href="https://www.nuget.org/packages/RepoDb" target="_blank">https://www.nuget.org/packages/RepoDb</a></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b>Project</b>: <a href="https://github.com/RepoDb/RepoDb">https://github.com/RepoDb/RepoDb</a></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b>Documentation</b>: <a href="https://repodb.readthedocs.io/en/latest/" target="_blank">https://repodb.readthedocs.io/en/latest/</a></span><br />
<br />
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: small;">RepoDb v1.5.2 Result:</span></b><br />
<br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b>Individual Fetches:</b></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjh4nQzT7QegTI-1itxGy1XD6ULzCvfTfHPU0XAaMUcj6gFr1HUFDRKuEujTMz9-BM5G3AkTpePeHk99PI344CA1fDK1_lD5D5c-kVCm2fnoitvPEpZAzbsWMqKGfhX8iMjr_5OKTpbynk/s1600/RepoDb+RawSql+Ind+Fetches+Result.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="529" data-original-width="1600" height="130" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjh4nQzT7QegTI-1itxGy1XD6ULzCvfTfHPU0XAaMUcj6gFr1HUFDRKuEujTMz9-BM5G3AkTpePeHk99PI344CA1fDK1_lD5D5c-kVCm2fnoitvPEpZAzbsWMqKGfhX8iMjr_5OKTpbynk/s400/RepoDb+RawSql+Ind+Fetches+Result.PNG" width="400" /></a></div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span><span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span>
<b style="font-family: arial, helvetica, sans-serif;">Set Fetches:</b><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrrh048K_mmn5XxUZt4Mw5NXpRxa5Yu7mdDBh0iT7AGvaNqPmlSoiT84peficAGa1_RAowL8UEAAuS3V0le2TdU5vnJX45pU34UwLh0Ubru8lcERrakRFRPPg06zPANFrD1Ic0IZpaUcE/s1600/RepoDb+RawSql+Set+Fetches+Result.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="527" data-original-width="1600" height="131" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrrh048K_mmn5XxUZt4Mw5NXpRxa5Yu7mdDBh0iT7AGvaNqPmlSoiT84peficAGa1_RAowL8UEAAuS3V0le2TdU5vnJX45pU34UwLh0Ubru8lcERrakRFRPPg06zPANFrD1Ic0IZpaUcE/s400/RepoDb+RawSql+Set+Fetches+Result.PNG" width="400" /></a></div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;"><br /></span></b>
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;"><br /></span></b>
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;"><br /></span></b><br />
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;"><br /></span></b>
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;">RepoDb Performance</span></b><br />
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Last July 2018, I have posted an initial thread for RepoDb at Reddit and claiming that our library is the fastest one. The thread can be found <a href="https://www.reddit.com/r/csharp/comments/8y5pm3/repodb_a_very_fast_lightweight_orm_and_has_the/" target="_blank">here </a>(<a href="https://www.reddit.com/r/csharp/comments/8y5pm3/repodb_a_very_fast_lightweight_orm_and_has_the/" target="_blank">https://www.reddit.com/r/csharp/comments/8y5pm3/repodb_a_very_fast_lightweight_orm_and_has_the/</a>).</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Many redditors commented and exchanged words with us about the library, specially with its performance, stability, purpose, features, syntax, differentiator etc etc.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">We know that our IL is very fast, and that's true that RepoDb was the fastest in a big-mapping objects (of like 1 million rows). However, the community suggested to test the library using the existing performance bench(ers) that is commonly used by the community to actually test the performance of the ORM library.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">With this, we used the benchmarker tool of FransBouma to test how's the performance of our library when compared to other.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;">Initial Performance Test Result with FransBouma's Tool</span></b></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRGrvXDYXEU-ttZUf1ArG-xi6DbVjd-ooc4tLhXXDHYVWhyphenhyphen9DtO9e5hyphenhyphenGUwc-SXQp3Lo3tVyPhsBZ_X5t4pJVedbWJ2gtIcIElohGQZe2oP9A5Nu-2B0d9REYEa4e18VSGiFwxh9fnuhA/s1600/RepoDb+Performance+Test+-+1.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><span style="clear: left; float: left; font-family: "arial" , "helvetica" , sans-serif; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="495" data-original-width="1521" height="130" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRGrvXDYXEU-ttZUf1ArG-xi6DbVjd-ooc4tLhXXDHYVWhyphenhyphen9DtO9e5hyphenhyphenGUwc-SXQp3Lo3tVyPhsBZ_X5t4pJVedbWJ2gtIcIElohGQZe2oP9A5Nu-2B0d9REYEa4e18VSGiFwxh9fnuhA/s400/RepoDb+Performance+Test+-+1.PNG" width="400" /></span></a></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;">It was a shame to us claiming that RepoDb was the fastest ORM .Net Library. It was personally my fault for not executing the proper benchmarking when it comes to performance before it to everyone.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The reason why RepoDb was slow in the result above, was because the performance benchmark tool are using the iterative ways to compare the performance of every ORM. During our development, we never considered this approach.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The version of RepoDb by this time was <b>v1.2.0</b>.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;">Improving the Performance of IL</span></b></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">First, we analyze the cause of the performance flaws, whether it is the IL or the actual reflection procedure we had. In the beginning, I saw that I did not cache the IL statically, though I am caching it in as per-call basis.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">With this, we first cache the IL statically by adding this logic.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<b><span style="font-family: "arial" , "helvetica" , sans-serif;">First logic:</span></b></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdlybV86qeIQk5qbc3OMPnzsQitlSxnbNHZh5gxaG0qWhRlVapGOi8JgEpCdpFOU9igys4EjerkoaViOMeXY8nQGb773I8Z8GFKTIaqS1tgRh5s91JFguJ8hNjiL88Pxq-dc5pW0vBTkQ/s1600/Delegate+Cache+1.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><img border="0" data-original-height="268" data-original-width="677" height="123" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdlybV86qeIQk5qbc3OMPnzsQitlSxnbNHZh5gxaG0qWhRlVapGOi8JgEpCdpFOU9igys4EjerkoaViOMeXY8nQGb773I8Z8GFKTIaqS1tgRh5s91JFguJ8hNjiL88Pxq-dc5pW0vBTkQ/s320/Delegate+Cache+1.PNG" width="320" /></span></a></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></b></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><span style="font-family: "arial" , "helvetica" , sans-serif;">After logic:</span></b></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeVfBu7ubFeFXNZQ9FS9E-YE8L64FtGlANZ5nVKr3_7DzFAzrXcz9qUaVJSEa7DQvzxHErrTrbCi1I3BKrpOsCqCsyHlJqWhiA9-IrUsqbIXmJ7RjTMpDkpcD4w0e5yczA9GgiX2CvyWA/s1600/Delegate+Cache+2.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><img border="0" data-original-height="348" data-original-width="656" height="168" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeVfBu7ubFeFXNZQ9FS9E-YE8L64FtGlANZ5nVKr3_7DzFAzrXcz9qUaVJSEa7DQvzxHErrTrbCi1I3BKrpOsCqCsyHlJqWhiA9-IrUsqbIXmJ7RjTMpDkpcD4w0e5yczA9GgiX2CvyWA/s320/Delegate+Cache+2.PNG" width="320" /></span></a></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<b><span style="font-family: "arial" , "helvetica" , sans-serif;">Code Level:</span></b></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Created a new class named </span><span style="font-family: "courier new" , "courier" , monospace;">DelegateCache</span><span style="font-family: "arial" , "helvetica" , sans-serif;">.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">public static class DelegateCache</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">{</span></div>
</div>
<div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> ...</span></div>
</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Actual class can be found <a href="https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/Reflection/DelegateCache.cs" target="_blank">here</a> (<a href="https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/Reflection/DelegateCache.cs" target="_blank">https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/Reflection/DelegateCache.cs</a>).</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">In our class </span><span style="font-family: "courier new" , "courier" , monospace;">DataReaderConverter</span><span style="font-family: "arial" , "helvetica" , sans-serif;">, we used the the newly created class above to get the corresponding delegate for our data reader's (it is a pre-compiled IL-written delegate).</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The approach above significantly improve the performance of RepoDb, however, we still have the flaws when it comes to memory usage. We are aware that we are heavy with the C# reflection.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;"><b>Improving the Performance of Reflection</b></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Secondly, we targetted to cache the reflected objects. We aim to make sure that we only call the </span><span style="font-family: "courier new" , "courier" , monospace;">typeof(Entity).GetProperties()</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> once all throughout the lifetime of the library, per class level.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">What we did is we introduce a class named </span><span style="font-family: "courier new" , "courier" , monospace;">PropertyCache</span> <span style="font-family: "arial" , "helvetica" , sans-serif;">to cache the call per class. Secondly, we added class named </span><span style="font-family: "courier new" , "courier" , monospace;">ClassExpression</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> to pre-compile the </span><span style="font-family: "courier new" , "courier" , monospace;">GetProperties()</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> operation via Expression Lambda so the next time we call it, it is already compiled.</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> public static class PropertyCache</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> public static IEnumerable<ClassProperty> Get<TEntity>(Command command = Command.None)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> where TEntity : class</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ...</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">We also created a class named </span><span style="font-family: "courier new" , "courier" , monospace;">ClassProperty</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> that will contain a </span><span style="font-family: "courier new" , "courier" , monospace;">PropertyInfo</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> object and necessary properties and methods to cache the definition of the property. We implemented the </span><span style="font-family: "courier new" , "courier" , monospace;">IEquatable<ClassProperty></span><span style="font-family: "arial" , "helvetica" , sans-serif;"> to make sure that the collection objects can maximize the performance of the comparison.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"> public class ClassProperty : IEquatable<ClassProperty></span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> {</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> ...</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> }</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Here is our way on simply caching the definition at instance level.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfc-KzIKgHKhEiX92wGQEdwepXuGoCf21rLhwgqvBCLwPVQjCLdJ6lWm4kdjXklWANwgX23D-nPi0qeg7xZrCBRuZ0ImMruAG8LV3uzLFonNKRCwCTc1px3Z_mPp9m9FU31Y6p0Z7K238/s1600/GetPrimaryAttribute.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><span style="font-family: "arial" , "helvetica" , sans-serif;"><img border="0" data-original-height="456" data-original-width="1007" height="144" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfc-KzIKgHKhEiX92wGQEdwepXuGoCf21rLhwgqvBCLwPVQjCLdJ6lWm4kdjXklWANwgX23D-nPi0qeg7xZrCBRuZ0ImMruAG8LV3uzLFonNKRCwCTc1px3Z_mPp9m9FU31Y6p0Z7K238/s320/GetPrimaryAttribute.PNG" width="320" /></span></a></div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span><span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;">Notice the checking of </span><span style="font-family: "courier new" , "courier" , monospace;">m_isPrimaryAttributeWasSet</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> variable, if this is set to </span><span style="font-family: "courier new" , "courier" , monospace;">true</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> already, this means that the call into this method is done already, even the result to the </span><span style="font-family: "courier new" , "courier" , monospace;">m_primaryAttribute</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> property is </span><span style="font-family: "courier new" , "courier" , monospace;">null</span><span style="font-family: "arial" , "helvetica" , sans-serif;">.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">We did the same to other definition methods all throughout the class. The actual class can be found <a href="https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/ClassProperty.cs" target="_blank">here</a> (<a href="https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/ClassProperty.cs" target="_blank">https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/ClassProperty.cs</a>).</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">And since we know that the call to </span><span style="font-family: "courier new" , "courier" , monospace;">GetProperties()</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> will only happen once per class after we defined the </span><span style="font-family: "courier new" , "courier" , monospace;">PropertyCache</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> class, then we are sure that the memory will be minimize here, as we have already removed the recurrent operation on this reflection approach.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">We are all set already with the implementation above, however, this was still not enough until we cache the actual activity of the caller (actual project that references RepoDb). With this, we came up an idea to cache the command text.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<b><span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;">Caching the CommandTexts</span></b><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">As we know that caching the outside calls would improve a lot the performance of the library as it would actually bypass all the operations we have mentioned above (earlier on this blog).</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">With this, we first implemented the requests classes as you see below.</span><br />
<br />
<ul>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">QueryRequest for Query</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">InsertRequest for Insert</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">DeleteRequest for Delete</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">UpdateRequest for Update</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">etc</span></li>
</ul>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The classes can be found <a href="https://github.com/RepoDb/RepoDb/tree/master/RepoDb/RepoDb/Requests" target="_blank">here</a> (<a href="https://github.com/RepoDb/RepoDb/tree/master/RepoDb/RepoDb/Requests" target="_blank">https://github.com/RepoDb/RepoDb/tree/master/RepoDb/RepoDb/Requests</a>).</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">In every class defined above, it accepts all the parameters the outside calls has in placed. This is to make sure that we are using the passed-values as a key to the uniqueness of the command texts that we are going to cache.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<b><span style="font-family: "arial" , "helvetica" , sans-serif;">Identifying the Differences of the Parameter Values</span></b></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">We used to override the </span><span style="font-family: "courier new" , "courier" , monospace;">GetHashCode()</span><span style="font-family: "arial" , "helvetica" , sans-serif;">, </span><span style="font-family: "courier new" , "courier" , monospace;">Equals()</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> and implemented the </span><span style="font-family: "courier new" , "courier" , monospace;">IEquatable<T></span><span style="font-family: "arial" , "helvetica" , sans-serif;"> interface to override the equality comparer of the following classes.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<ul>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">All Request Classses</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">ClassProperty</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">QueryField</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">Field</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">Parameter</span></li>
<li><span style="font-family: "arial" , "helvetica" , sans-serif;">QueryGroup</span></li>
</ul>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">It enable us to identify and define the correct equality of the object (internally to RepoDb only).</span></div>
</div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Inside the library, we forced the equality, let's say the </span><span style="font-family: "courier new" , "courier" , monospace;">FieldA</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> with name equals to "Name" is equal to the instance of </span><span style="font-family: "courier new" , "courier" , monospace;">FieldB</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> with name equals "Name" and so forth. The logic is very simple with below's code.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public override int GetHashCode()</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>return Name.GetHashCode();</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public override bool Equals(object obj)</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>return GetHashCode() == obj?.GetHashCode();</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public bool Equals(Field other)</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>return GetHashCode() == other?.GetHashCode();</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public static bool operator ==(Field objA, Field objB)</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>if (ReferenceEquals(null, objA))</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>{</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>return ReferenceEquals(null, objB);</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>}</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>return objA?.GetHashCode() == objB?.GetHashCode();</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public static bool operator !=(Field objA, Field objB)</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><span style="white-space: pre;"> </span>return (objA == objB) == false;</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The actual class can be found <a href="https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/Field.cs" target="_blank">here</a> (<a href="https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/Field.cs" target="_blank">https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/Field.cs</a>).</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b>Caching Process for </b></span><b style="font-family: Arial, Helvetica, sans-serif;">CommandText</b></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Lastly, we introduced a class named </span><span style="font-family: "courier new" , "courier" , monospace;">CommandTextCache</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> that holds the cached command text of the caller. See below the implementation of one of the method.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">internal static class CommandTextCache</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">{</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> private static readonly ConcurrentDictionary<BaseRequest, string> m_cache = new ConcurrentDictionary<BaseRequest, string>();</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> public static string GetBatchQueryText<TEntity>(BatchQueryRequest request)</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> where TEntity : class</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> var commandText = (string)null;</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> if (m_cache.TryGetValue(request, out commandText) == false)</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> {</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> commandText = <codes to get the BatchQuery command text>;</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> m_cache.TryAdd(request, commandText);</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> return commandText;</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> }</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">}</span></div>
</div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The actual class implementation can be found <a href="https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/CommandTextCache.cs" target="_blank">here</a> (<a href="https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/CommandTextCache.cs" target="_blank">https://github.com/RepoDb/RepoDb/blob/master/RepoDb/RepoDb/CommandTextCache.cs</a>).</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Let us say, somebody tried to call the repository's </span><span style="font-family: "courier new" , "courier" , monospace;">Query</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> method as below.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">using (var repository = new DbRepository<SqlConnection>(connectionString))<br />{</span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"> repository.Query<Person>(new { Id = 10220 });<br />}</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The suppose command text is below.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;">SELECT [Id], [Name], [Address], [DateOfBirth], [DateInsertedUtc], [LastUpdatedUtc] FROM [dbo].[Person];</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Inside RepoDb, the method repository </span><span style="font-family: "courier new" , "courier" , monospace;">Query</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> method has a created a new </span><span style="font-family: "courier new" , "courier" , monospace;">QueryRequest</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> object with the parameters defined by the caller. In this case is (</span><span style="font-family: "courier new" , "courier" , monospace;">new { Id = 10220 }</span><span style="font-family: "arial" , "helvetica" , sans-serif;">).</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Then we simply call the </span><span style="font-family: "courier new" , "courier" , monospace;">CommandText.GetQueryText(queryRequest)</span><span style="font-family: "arial" , "helvetica" , sans-serif;"> get the cached command text.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif; font-size: large;"><b>RepoDb Final Results</b></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">There is 2 way of calling the operations in RepoDb, persistent connection and with non-persistent connection. There is 2 way as well on how to do the query, object-based and raw-sql based.</span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The result below is only for RawSql approach as we have never injected the Object-Based approach. (Note: RawSql is always faster than the Object-Based). This result was personally executed by FransBouma on their Test Environment (with Release binaries version).</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b>Individual fetches:</b></span><br />
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span></div>
<div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwth_9GDQLw9sSHaNaRHyqYgF7-CyfhrmpgKdCRSX89cEvZemzGClIleA639HFpBNqP88NSqjEijS1dX4n8ZtoHi3iAQEvDhpdIzDCFI5OYOXX9C0022uOvGx58v4TG8xJLLsyW5kZRnk/s1600/RepoDb+RawSql+Ind+Fetches+Result+-+Frans.PNG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="510" data-original-width="1600" height="127" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwth_9GDQLw9sSHaNaRHyqYgF7-CyfhrmpgKdCRSX89cEvZemzGClIleA639HFpBNqP88NSqjEijS1dX4n8ZtoHi3iAQEvDhpdIzDCFI5OYOXX9C0022uOvGx58v4TG8xJLLsyW5kZRnk/s400/RepoDb+RawSql+Ind+Fetches+Result+-+Frans.PNG" width="400" /></a></div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><b><br /></b></span><br />
<b style="font-family: arial, helvetica, sans-serif;">Set fetches:</b><br />
<br />
<div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikAV_zIQfbZOAaBCNui5Kl44A_0Psokxrm6O3H-i-E0hmD45lTRAmAvcAMLaIJVi6CMpPUVPInZcsyXkNuyd5toPFqXRNPeCyRvwVXcoSPwigD8CWF591vvKACXoB56DlI_WiI1ATwaKU/s1600/RepoDb+RawSql+Set+Fetches+Result+-+Frans.PNG" imageanchor="1" style="clear: left; display: inline !important; margin-bottom: 1em; margin-right: 1em; text-align: center;"><img border="0" data-original-height="515" data-original-width="1600" height="127" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikAV_zIQfbZOAaBCNui5Kl44A_0Psokxrm6O3H-i-E0hmD45lTRAmAvcAMLaIJVi6CMpPUVPInZcsyXkNuyd5toPFqXRNPeCyRvwVXcoSPwigD8CWF591vvKACXoB56DlI_WiI1ATwaKU/s400/RepoDb+RawSql+Set+Fetches+Result+-+Frans.PNG" width="400" /></a></div>
<div>
<br /></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The results can be found </span><a href="https://github.com/RepoDb/RawDataAccessBencher/tree/master/Results" style="font-family: arial, helvetica, sans-serif;" target="_blank">here</a><span style="font-family: "arial" , "helvetica" , sans-serif;"> (</span><a href="https://github.com/RepoDb/RawDataAccessBencher/tree/master/Results" style="font-family: arial, helvetica, sans-serif;" target="_blank">https://github.com/RepoDb/RawDataAccessBencher/tree/master/Results</a><span style="font-family: "arial" , "helvetica" , sans-serif;">).</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="font-family: "arial" , "helvetica" , sans-serif;">The version of RepoDb by this time is <b>v1.5.3</b>.</span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;"><br /></span></div>
<div>
<span style="font-family: "arial" , "helvetica" , sans-serif;">Kindly share your thoughts, comments and inputs, do not forget to tag me if you would like an immediate response. </span><span style="font-family: "arial" , "helvetica" , sans-serif;">Thank you for reading this blog!</span></div>
</div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com2tag:blogger.com,1999:blog-2161576857104408209.post-8119176088828965642018-06-08T07:03:00.002-07:002018-06-08T22:25:59.798-07:00Apology for 5 years absence! I would like to apologize for being absent for the last 5 years. There are personal problems that keeps me out of focus on my technical stuffs blogging career.<br />
<br />
I will do my best to become more active and participated well on .Net communities.<br />
<br />
By the way, after 5 years, we barely become more stronger when it comes to Microsoft Programming and we should be focusing mostly on the usual business and industry problem, and will focus mostly on cloud and big data computing.<br />
<br />
This blog will soon be active again. Stay tuned!Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-22311103327703857482013-02-24T23:10:00.002-08:002013-02-26T18:35:02.323-08:00Enabling SQL Server Service Broker<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Below is the common script we used to enable the Service Broker in SQL Server 2008.</span><br />
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;"><span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ALTER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DATABASE</span>
<DatabaseName> <span style="color: blue;">SET</span> <span style="color: blue;">ENABLE_BROKER</span><span style="color: grey;">;</span></span></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><o:p></o:p></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><br /></span></span>
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Example:</span></span></span><br />
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><br /></span></span>
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey; font-family: Courier New, Courier, monospace;"><span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ALTER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DATABASE</span> Northwind <span style="color: blue;">SET</span> <span style="color: blue;">ENABLE_BROKER</span><span style="color: grey;">;</span></span></span></span><br />
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></span></span></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">If you feel your database has an outstanding open connection, you have to clear it first before running the script. For you to terminate all connection, you have to set the current database to be single-user and call the rollback keyword to terminate it all. After executing the script, set back the database to be a multi-user database.</span></span></span></span></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></span></span></span></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Example script:</span></span></span></span></span><br />
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey; font-family: Courier New, Courier, monospace;"><span style="color: blue;"><br /></span></span></span></span></span>
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey; font-family: Courier New, Courier, monospace;"><span style="color: blue;">ALTER</span> <span style="color: blue;">DATABASE</span> Northwind <span style="color: blue;">SET</span> <span style="color: blue;">SINGLE_USER</span> <span style="color: blue;">WITH</span> <span style="color: blue;">ROLLBACK</span> <span style="color: blue;">IMMEDIATE</span><span style="color: grey;">;</span></span></span></span></span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;">GO</span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;">ALTER</span><span style="font-family: 'Courier New', Courier, monospace;"> </span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">DATABASE</span><span style="font-family: 'Courier New', Courier, monospace;"> Northwind </span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">SET</span><span style="font-family: 'Courier New', Courier, monospace;"> </span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">ENABLE_BROKER;</span><br />
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;"><span style="color: blue; font-family: Courier New, Courier, monospace;">GO</span></span></span></span></span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;">ALTER</span><span style="font-family: 'Courier New', Courier, monospace;"> </span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">DATABASE</span><span style="font-family: 'Courier New', Courier, monospace;"> Northwind </span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">SET</span><span style="font-family: 'Courier New', Courier, monospace;"> </span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">MULTI_USER;</span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;">GO</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Microsoft Documentation: Please visit this </span><a href="http://msdn.microsoft.com/en-us/library/bb522893.aspx" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;" target="_blank">link</a><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> for more information about SQL Server Service Broker.</span></div>
</div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com1tag:blogger.com,1999:blog-2161576857104408209.post-66631021476716151252013-02-10T22:41:00.002-08:002013-02-10T22:45:30.163-08:00Querying Database Objects in SQL Server<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The SQL Server contains a built-in schema called </span><span style="font-family: Courier New, Courier, monospace;">[sys]</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> which can be used to query all the server objects created on the current database. This schema provides all the information used by each object of the database such as indexes, columns/fields, tables and views.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This is commonly used by other developer to actually developed an auto generator tool for their architecture.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">For Microsoft documentation, please visit this <a href="http://msdn.microsoft.com/en-us/library/ms189082(v=sql.105).aspx" target="_blank">link</a>.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">See below how to query the SQL Server Objects from the SQL Server.</span><br />
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Querying Objects</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Inside the </span><span style="font-family: Courier New, Courier, monospace;">[sys]</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> schema, if your SQL Server has intellisense, you can see all the schema definition objects if you put a dot after </span><span style="font-family: Courier New, Courier, monospace;">[sys]</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> keyword. See below the screenshot.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-aJASNKd179mDhyk7MC727Xy1oHPL8omKCcDm3Qf78qd1zeYomudC9xZOoW-7FUpQUE670yaIYytTl-7uXCxfYa1Y_uDBzyL-jTf1VdewPywiDhmAmB6510ciPMYuCrDTWEtKXt4GLSs/s1600/sys+schema.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-aJASNKd179mDhyk7MC727Xy1oHPL8omKCcDm3Qf78qd1zeYomudC9xZOoW-7FUpQUE670yaIYytTl-7uXCxfYa1Y_uDBzyL-jTf1VdewPywiDhmAmB6510ciPMYuCrDTWEtKXt4GLSs/s1600/sys+schema.PNG" /></a></div>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You can select what schema object you may query for. We can use a default </span><span style="font-family: Courier New, Courier, monospace;">SELECT</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> query to do a query.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Let say for example, we can query the tables, views, stored procedures and functions with the use of </span><span style="font-family: Courier New, Courier, monospace;">sys.objects</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> catalog.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /><b>Querying Tables</b><br /><br />We can use the </span><span style="font-family: Courier New, Courier, monospace;">sys.objects</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> catalog to query the list of tables from the database. See sample code below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span>
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> <span style="color: blue;">type<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> type_desc<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> create_date<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> modify_date<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">objects<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">type</span> <span style="color: grey;">=</span> <span style="color: red;">'u'<o:p></o:p></span></span></div>
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-no-proof: yes;">ORDER</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-no-proof: yes;"> <span style="color: blue;">BY</span> name<span style="color: grey;">;</span></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Type </span><span style="font-family: Courier New, Courier, monospace;">'U'</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> defined the object as the </span><span style="font-family: Courier New, Courier, monospace;">USER_TABLE</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. If we want to query the custom stored procedures, then we can filter the type </span><span style="font-family: Courier New, Courier, monospace;">'P'</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><br /></b></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Querying Fields</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We can use the </span><span style="font-family: Courier New, Courier, monospace;">sys.columns</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> catalog to query all the columns from the database. After querying the columns, we can use the </span><span style="font-family: Courier New, Courier, monospace;">object_id</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> field to determine what table it is belong to. See sample below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span>
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> column_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> max_length<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> user_type_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> system_type_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">columns</span><span style="color: grey;">;</span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Joining the table and only querying the custom table columns.</span></span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> o<span style="color: grey;">.</span><span style="color: magenta;">object_id<o:p></o:p></span></span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
</span>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> o<span style="color: grey;">.</span>name
<span style="color: blue;">as</span> tablename<o:p></o:p></span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
</span>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> c<span style="color: grey;">.</span>name
<span style="color: blue;">as</span> columnname<o:p></o:p></span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
</span>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> c<span style="color: grey;">.</span>column_id<o:p></o:p></span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> c<span style="color: grey;">.</span>max_length<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> c<span style="color: grey;">.</span>user_type_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> c<span style="color: grey;">.</span>system_type_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">columns</span> c<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">JOIN</span> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">objects</span> o <span style="color: blue;">ON</span> o<span style="color: grey;">.</span><span style="color: magenta;">object_id</span> <span style="color: grey;">=</span> c<span style="color: grey;">.</span><span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> o<span style="color: grey;">.</span><span style="color: blue;">type</span> <span style="color: grey;">=</span> <span style="color: red;">'U'<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">ORDER</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: blue;">BY</span> o<span style="color: grey;">.</span>name<span style="color: grey;">,</span> c<span style="color: grey;">.</span>name<span style="color: grey;">;</span></span></div>
</span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><br /></b></span></div>
Querying Indexes</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Same with the columns, we can use the sys.indexes to query the list of indexes under one table. See our sample below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span>
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> i<span style="color: grey;">.</span><span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> o<span style="color: grey;">.</span>name
<span style="color: blue;">as</span> tablename<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span>name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span>index_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span><span style="color: blue;">type<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span>type_desc<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span>is_unique<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span>is_primary_key<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">indexes</span> i<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">JOIN</span> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">objects</span> o <span style="color: blue;">ON</span> o<span style="color: grey;">.</span><span style="color: magenta;">object_id</span> <span style="color: grey;">=</span> i<span style="color: grey;">.</span><span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> o<span style="color: grey;">.</span><span style="color: blue;">type</span> <span style="color: grey;">=</span> <span style="color: red;">'u'<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">ORDER</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: blue;">BY</span> o<span style="color: grey;">.</span>name<span style="color: grey;">;</span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Querying the indexed columns by table can be filtered using the </span><span style="font-family: Courier New, Courier, monospace;">sys.index_columns</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. See below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span>
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> i<span style="color: grey;">.</span><span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> o<span style="color: grey;">.</span>name
<span style="color: blue;">as</span> tablename<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> c<span style="color: grey;">.</span>name
<span style="color: blue;">as</span> columnname<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span>index_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span>index_column_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> i<span style="color: grey;">.</span>column_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">index_columns</span> i<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">JOIN</span> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">objects</span> o <span style="color: blue;">ON</span> o<span style="color: grey;">.</span><span style="color: magenta;">object_id</span> <span style="color: grey;">=</span> i<span style="color: grey;">.</span><span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">JOIN</span> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">columns</span> c <span style="color: blue;">ON</span> c<span style="color: grey;">.</span><span style="color: magenta;">object_id</span> <span style="color: grey;">=</span> o<span style="color: grey;">.</span><span style="color: magenta;">object_id</span> <span style="color: grey;">AND</span> c<span style="color: grey;">.</span>column_id <span style="color: grey;">=</span> i<span style="color: grey;">.</span>column_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> o<span style="color: grey;">.</span><span style="color: blue;">type</span> <span style="color: grey;">=</span> <span style="color: red;">'u'<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">ORDER</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: blue;">BY</span> o<span style="color: grey;">.</span>name<span style="color: grey;">;</span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Base in your requirements, you can expand and filter more specific objects inside </span><span style="font-family: Courier New, Courier, monospace;">[sys]</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> schema.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-70128996461369189862013-02-02T23:46:00.001-08:002013-02-03T03:39:23.739-08:00Execute SQL Server Scripts in C#<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">With this tutorial we will guide you how to execute </span><span style="font-family: Courier New, Courier, monospace;">SQL Server scripts</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> from </span><span style="font-family: Courier New, Courier, monospace;">C#.Net</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. This topic is more about </span><span style="font-family: Courier New, Courier, monospace;">SMO</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">SQL Server Management Objects</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">For your reference regarding </span><span style="font-family: Courier New, Courier, monospace;">SMO</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, please visit Microsoft <a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx" target="_blank">documentation</a>.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>What is SQL Server Management Object?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><br /></b></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">SQL Server Management Object</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is a set of </span><span style="font-family: Courier New, Courier, monospace;">API</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> developed by Microsoft so that any object manipulations in </span><span style="font-family: Courier New, Courier, monospace;">SQL Server</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> can also be done in the client. This allow other developer to develop more a dynamic </span><span style="font-family: Courier New, Courier, monospace;">Query</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">Class</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> generator architecture.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Let us start with the set of procedure below.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">First, in your </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> project, add a reference to the list of </span><span style="font-family: Courier New, Courier, monospace;">DLL</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> below.</span><br />
<ul>
<li><span style="font-family: 'Courier New', Courier, monospace;">Microsoft.SqlServer.Management.Sdk.Sfc</span></li>
<li><span style="font-family: 'Courier New', Courier, monospace;">Microsoft.SqlServer.Smo</span></li>
<li><span style="font-family: 'Courier New', Courier, monospace;">Microsoft.SqlServer.SmoExtended</span></li>
<li><span style="font-family: 'Courier New', Courier, monospace;">Microsoft.SqlServer.SqlEnum</span></li>
</ul>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Folder Location: C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\</span><br />
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We will most likely only use 2 binary there (just add other for your future development).</span><br />
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">For you to make your SQL scripts runnable in the client, you should test it first in the </span><span style="font-family: Courier New, Courier, monospace;">SQL Management Studio</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> whether there are no syntax error existed. If you feel that the script is right then you're ready to go with the client manipulation.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Stored Procedure</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Suppose you have a database named </span><span style="font-family: Courier New, Courier, monospace;">Northwind</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and you have a table named </span><span style="font-family: Courier New, Courier, monospace;">User (userid, name, email, createddate)</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">; then we will create a sample stored procedure for that table.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">With our sample table </span><span style="font-family: Courier New, Courier, monospace;">User</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we will create a script to get the current user based on the </span><span style="font-family: Courier New, Courier, monospace;">UserID</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> parameter. See below our sample script.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span>
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ANSI_NULLS</span> <span style="color: blue;">ON<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">QUOTED_IDENTIFIER</span> <span style="color: blue;">ON<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">CREATE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">PROCEDURE</span> [dbo]<span style="color: grey;">.</span>[GetUser_sp]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @UserID <span style="color: blue;">BIGINT<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">AS<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SELECT</span> [UserID]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> [Name]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> [Email]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">,</span> [CreatedDate]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FROM</span> [dbo]<span style="color: grey;">.</span>[User]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">WHERE </span><span style="color: grey;">(</span>[UserID]
<span style="color: grey;">=</span> @UserID<span style="color: grey;">);<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">END</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Ensure that the script above is running successfully in the </span><span style="font-family: Courier New, Courier, monospace;">SQL Management Studio</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. If you found any problem or error (script problem or syntax error) then fix it before executing it in the client.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>.NET SMO Execution</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We will now guide you how to execute it in the client. Now, go back to our </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> project solution and do some code snippet.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">First add a reference to the namespaces </span><span style="font-family: Courier New, Courier, monospace;">Microsoft.SqlServer.Management.Smo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Microsoft.SqlServer.Management.Common</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> to your class above. See below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> Microsoft.SqlServer.Management.Smo;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> Microsoft.SqlServer.Management.Common;</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Then, create a </span><span style="font-family: Courier New, Courier, monospace;">SqlConnection</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object that connects to your database. See below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> (<span style="color: blue;">var</span> connection = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlConnection</span>(<span style="color: blue;">this</span>.ConnectionString))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Inside the </span><span style="font-family: Courier New, Courier, monospace;">using</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> block, create a </span><span style="font-family: Courier New, Courier, monospace;">Server</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Database</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. See below our codes.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> (<span style="color: blue;">var</span> connection = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlConnection</span>(<span style="color: blue;">this</span>.ConnectionString))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> server = <span style="color: blue;">new</span> <span style="color: #2b91af;">Server</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">ServerConnection</span>(connection));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> database = server.Databases[connection.Database];</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In the </span><span style="font-family: Courier New, Courier, monospace;">Server</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object </span><span style="font-family: Courier New, Courier, monospace;">ConnectionContext</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property, we need to set the property </span><span style="font-family: Courier New, Courier, monospace;">AutoDisconnectMode</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> to </span><span style="font-family: Courier New, Courier, monospace;">NoAutoDisconnect</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> for it to not disconnect when there are existing pooling operation in the database.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After that, call the </span><span style="font-family: Courier New, Courier, monospace;">Connect</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method to connect on the server and then call the </span><span style="font-family: Courier New, Courier, monospace;">ExecuteNonQuery</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method passing the string of our </span><span style="font-family: Courier New, Courier, monospace;">SQL Scripts</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Please m</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">ake sure to disconnect the connection once executed.</span><br />
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, our new code is below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> (<span style="color: blue;">var</span> connection = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlConnection</span>(<span style="color: blue;">this</span>.ConnectionString))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> server = <span style="color: blue;">new</span> <span style="color: #2b91af;">Server</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">ServerConnection</span>(connection));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> database = server.Databases[connection.Database];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
server.ConnectionContext.AutoDisconnectMode = <span style="color: #2b91af;">AutoDisconnectMode</span>.NoAutoDisconnect;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
server.ConnectionContext.Connect();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
server.ConnectionContext.ExecuteNonQuery(<span style="color: #cc0000;">"SQL SCRIPTS HERE"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
server.ConnectionContext.Disconnect();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Note:</b> If you are running the 4.0 version of .NET and if you however encountered an exception regarding version compatibility. You need to support the 2.0 version of .NET during start up. To do this, you have to modify some settings from your config file. See below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><?</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">xml</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">version</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">1.0</span>"<span style="color: blue;">?></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">configuration</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
<</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">startup</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">useLegacyV2RuntimeActivationPolicy</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">true</span>"<span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
<</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">supportedRuntime</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">version</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=</span><span style="font-family: Consolas; font-size: 9.5pt;">"<span style="color: blue;">v4.0</span>"<span style="color: blue;"> </span><span style="color: red;">sku</span><span style="color: blue;">=</span>"<span style="color: blue;">.NETFramework,Version=v4.0</span>"<span style="color: blue;">/></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
</</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">startup</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">configuration</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">useLegacyV2RuntimeActivationPolicy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> will do the trick.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">That's all. Have a happy coding.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com1tag:blogger.com,1999:blog-2161576857104408209.post-46070323836806399402013-01-30T06:29:00.000-08:002013-01-30T06:34:20.017-08:00Creating an Attribute in C#<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, attribute is one of the common declarative technique that most of us is using into our application. Usually, we use the attribute to tag or flag a class, method or property in the form of object base recognition. It also simplify our determination in every object.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Furthermore, in </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> there are lots of pre-defined attribute that we can embed into our application. One common sample is </span><span style="font-family: Courier New, Courier, monospace;">SerializableAttribute</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> which flag/tag our class to participate in </span><span style="font-family: Courier New, Courier, monospace;">serialization</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">/</span><span style="font-family: Courier New, Courier, monospace;">deserialization</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> process. </span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">If however you would like to extend the attribute functionality and define your own, you can extend/inherit the attribute class from </span><span style="font-family: Courier New, Courier, monospace;">System</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> namespace.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please visit <a href="http://codesdirectory.blogspot.com/2013/01/c-flags-attribute.html" target="_blank">C# Flags Attribute</a> blog before you continue. The reason is to let you explore the enumeration named </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, we are expecting that you already explore the </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> enumeration form mentioned blog above.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Let say for example you would like to define a scenario that gives your code-level to determine whether a class (or a stub object) can be deleted from database. See below our sample custom attribute.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">SecurityAccess</span> : <span style="color: #2b91af;">Attribute</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> SecurityAccess()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> SecurityAccess(<span style="color: #2b91af;">Privilege</span>
privilege)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> : <span style="color: blue;">this</span>()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Privilege = privilege;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">Privilege</span>
Privilege<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin;">}</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">What the class </span><span style="font-family: Courier New, Courier, monospace;">SecurityAccess</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is doing above is just simply handling the value for the </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> enumeration. This property defines whether the class that declare this attribute has an enough level of privilege before doing necessary action in the database.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>How to use custom attribute?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, in this section, we will going to guide you how to use the attribute we created above.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Suppose we have two class that inherits from one object. Let's call them </span><span style="font-family: Courier New, Courier, monospace;">Animal</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, and the two other class in </span><span style="font-family: Courier New, Courier, monospace;">Cat</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Dog</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. See below implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Animal</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">bool</span>
IsDeletable<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">protected</span> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Cat</span> : <span style="color: #2b91af;">Animal</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> Cat()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Dog</span> : <span style="color: #2b91af;">Animal</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> Dog()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">Dog</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Cat</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is an </span><span style="font-family: Courier New, Courier, monospace;">Animal</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> base on our implementation. With the use of our custom attribute named </span><span style="font-family: Courier New, Courier, monospace;">SecurityPrivilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we can declare each privilege in every class. Suppose we would like different breed of </span><span style="font-family: Courier New, Courier, monospace;">Cat</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> cannot be deleted from our database, then we can declare our custom attribute like below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">SecurityAccess</span>(Privilege
= <span style="color: #2b91af;">Privilege</span>.Read | <span style="color: #2b91af;">Privilege</span>.Write
| <span style="color: #2b91af;">Privilege</span>.Create)]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Cat</span> : <span style="color: #2b91af;">Animal</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> Cat()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And the </span><span style="font-family: Courier New, Courier, monospace;">Dog</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> breed is deletable. See below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">SecurityAccess</span>(Privilege
= <span style="color: #2b91af;">Privilege</span>.Read | <span style="color: #2b91af;">Privilege</span>.Write
| <span style="color: #2b91af;">Privilege</span>.Create | <span style="color: #2b91af;">Privilege</span>.Delete)]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Dog</span> : <span style="color: #2b91af;">Animal</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> Dog()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<b style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; font-size: x-large;">How to access an attribute value declared in the class/method and other object?</b></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now, in this section, we will going to guide you how to access the value of the attribute per class level.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, the only way to get the attribute value is to use </span><span style="font-family: Courier New, Courier, monospace;">Reflection</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. First, we need to get the current type of the object, from there check if there are declared custom attributes and determine the attribute type. If its match the attribute we're looking for, then that is the custom attribute we had created. See below the process.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;"><br /></span></span>
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="color: blue;">protected </span>T GetCustomAttribute<T>(<span style="color: blue;">object</span> @object)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(@object,
<span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> type = @object.GetType();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> attrs = type.GetCustomAttributes(<span style="color: blue;">false</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (attrs.Length > 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> attr <span style="color: blue;">in</span> attrs)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (attr <span style="color: blue;">is</span>
T)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">return</span> (T)(<span style="color: blue;">object</span>)attr;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">default</span>(T);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">What the code is doing above is to simply return the embedded attribute in the object you passed in the parameter named </span><span style="font-family: Courier New, Courier, monospace;">@object</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. If there is no attribute found, then it will return </span><span style="font-family: Courier New, Courier, monospace;">null</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Best to place this code in the </span><span style="font-family: Courier New, Courier, monospace;">Animal</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> base class so both derive class can use it.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And now, in the construction of </span><span style="font-family: Courier New, Courier, monospace;">Dog</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Cat</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class, you should call the method directly from there.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> Dog()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> attr = <span style="color: blue;">this</span>.GetCustomAttribute<<span style="color: #2b91af;">SecurityAccess</span>>(<span style="color: blue;">this</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">base</span>.IsDeletable = (attr.Privilege & <span style="color: #2b91af;">Privilege</span>.Delete) == <span style="color: #2b91af;">Privilege</span>.Delete;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> Cat()</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> attr = <span style="color: blue;">this</span>.GetCustomAttribute<<span style="color: #2b91af;">SecurityAccess</span>>(<span style="color: blue;">this</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">base</span>.IsDeletable = (attr.Privilege & <span style="color: #2b91af;">Privilege</span>.Delete) == <span style="color: #2b91af;">Privilege</span>.Delete;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, in the base class </span><span style="font-family: Courier New, Courier, monospace;">IsDeletable</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property, we then set it depends on the privilege level we declared on the class.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please note that we can override the attribute value declared in the base class into derive class. So if you want that the </span><span style="font-family: Courier New, Courier, monospace;">Siamese</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> </span><span style="font-family: Courier New, Courier, monospace;">Cat</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> breed be deletable, you can declare your own </span><span style="font-family: Courier New, Courier, monospace;">SecurityAccess</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> attribute in </span><span style="font-family: Courier New, Courier, monospace;">Siamese</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">That's all about this blog. Please follow us so you will get more interesting topics soon.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please visit Microsoft <a href="http://msdn.microsoft.com/en-us/library/z0w1kczw(v=vs.80).aspx" target="_blank">documentation</a> for further details.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-52817355310989710082013-01-27T23:53:00.001-08:002013-01-29T01:55:24.240-08:00WPF Binding to a Static Instance<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This tutorial will guide you how to bind in a static property of a class in a two directional way in WPF.</span><br />
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<b><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Two way Binding in a Static Property</span></b><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In order for our class </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> to participates in the two directional binding, we should implement the </span><span style="font-family: Courier New, Courier, monospace;">IPropertyNotifyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event. In our case, we are requiring you to read our pre-requisite blog so you will fully understand the implementation.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">List of pre-requisite blogs.</span><br />
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><a href="http://codesdirectory.blogspot.com/2013/01/wpf-binding-to-static-property.html" target="_blank">WPF Binding to Static Property</a></span></li>
<li><a href="http://codesdirectory.blogspot.com/2013/01/c-inotifypropertychanged-base-class.html" target="_blank"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">C# INotifyPropertyChanged Base Class Implementation</span></a></li>
<li><a href="http://codesdirectory.blogspot.com/2013/01/implementing-flexible-baseobject.html" target="_blank"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Implementing a flexible BaseObject</span></a></li>
</ul>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">And here we are expecting that you're finish reading the pre-requisite blogs above.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now let's start with our explanation.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We need to modify our existing implementation of </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class and force inherit from the </span><span style="font-family: Courier New, Courier, monospace;">BaseObject</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class. With this, the </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> will also inherit the implementation of the super class </span><span style="font-family: Courier New, Courier, monospace;">PropertyNotifier</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> that implements the </span><span style="font-family: Courier New, Courier, monospace;">IPropertyNotifier</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> interface.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In each property of the </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class, we should use the </span><span style="font-family: Courier New, Courier, monospace;">BaseObject</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> </span><span style="font-family: Courier New, Courier, monospace;">GetValue</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">/</span><span style="font-family: Courier New, Courier, monospace;">SetValue</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> accessor so it will notify the listener on every property value changed. Please note that you need not to implement each method in a static way. In our case, you must remove the </span><span style="font-family: Courier New, Courier, monospace;">static</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> keyword in each property.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, last thing to do within </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class is to declare an additional static property named </span><span style="font-family: Courier New, Courier, monospace;">Instance</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. This approach is a single-ton approach for the </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class. This new property named </span><span style="font-family: Courier New, Courier, monospace;">Instance</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> will participate in WPF binding. See below our new class implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">StaticBinder</span> : <span style="color: #2b91af;">BaseObject</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">StaticBinder</span> __instance = <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> StaticBinder()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
__instance = <span style="color: blue;">new</span> <span style="color: #2b91af;">StaticBinder</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> StaticBinder()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
MSG_Cancel = <span style="color: #a31515;">"Welcome"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
MSG_OK = <span style="color: #a31515;">"OK"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
MSG_Welcome = <span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"Welcome {0}!"</span>, <span style="color: #a31515;">"WPF
Binding"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span>
MSG_Cancel<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: blue;">string</span>>(<span style="color: #a31515;">"MSG_Cancel"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"MSG_Cancel"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> MSG_OK<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: blue;">string</span>>(<span style="color: #a31515;">"MSG_OK"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"MSG_OK"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span>
MSG_Welcome<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: blue;">string</span>>(<span style="color: #a31515;">"MSG_Welcome"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"MSG_Welcome"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">StaticBinder</span> Instance<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span>
__instance; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Binding in XAML</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, if we are binding a static property in </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we need to use the namespace referencing and direct call the static property of that instance. Additionally, you can bind directly to the property of that shared instance which is in our case it is the </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">With the help of the </span><span style="font-family: Courier New, Courier, monospace;">Binding (x:Static)</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> keyword, the trick will be addressed. See below our codes in the </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> in <span style="background-color: yellow;">yellow</span> background.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Class</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="CodesDirectory.WIN_StaticBinding"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Title</span><span style="color: blue;">="WIN Static Binding"</span><span style="color: red;"> Height</span><span style="color: blue;">="300"</span><span style="color: red;"> Width</span><span style="color: blue;">="300"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> <span style="background-color: yellow;">xmlns</span></span><span style="background-color: yellow;"><span style="color: blue;">:</span><span style="color: red;">classes</span><span style="color: blue;">="clr-namespace:CodesDirectory.Classes"</span></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> WindowStartupLocation</span><span style="color: blue;">="CenterScreen"</span><span style="color: red;">
ResizeMode</span><span style="color: blue;">="NoResize"></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="5"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid.RowDefinitions</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Height</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="*"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Height</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Auto"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid.RowDefinitions</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Label</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Grid.Row</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="0"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> <span style="background-color: yellow;">Content</span></span><span style="background-color: yellow;"><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;"> Source</span><span style="color: blue;">={</span><span style="color: #a31515;">x</span><span style="color: blue;">:</span><span style="color: #a31515;">Static</span><span style="color: red;"> classes</span><span style="color: blue;">:</span><span style="color: red;">StaticBinder</span><span style="color: blue;">.Instance},</span><span style="color: red;"> Path</span></span><span style="color: blue;"><span style="background-color: yellow;">=MSG_Welcome}"</span>></</span><span style="color: #a31515;">Label</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Grid.Row</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="1"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> HorizontalAlignment</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Right"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Orientation</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Horizontal"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Button</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="okayButton"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Click</span><span style="color: blue;">="okayButton_Click"</span><span style="color: red;">
<span style="background-color: yellow;">Content</span></span><span style="background-color: yellow;"><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;"> Source</span><span style="color: blue;">={</span><span style="color: #a31515;">x</span><span style="color: blue;">:</span><span style="color: #a31515;">Static</span><span style="color: red;"> classes</span><span style="color: blue;">:</span><span style="color: red;">StaticBinder</span><span style="color: blue;">.Instance},</span><span style="color: red;"> Path</span><span style="color: blue;">=MSG_OK}"</span></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Width</span><span style="color: blue;">="100"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Margin</span><span style="color: blue;">="5"></</span><span style="color: #a31515;">Button</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Button</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="cancelButton"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> <span style="background-color: yellow;">Content</span></span><span style="background-color: yellow;"><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;"> Source</span><span style="color: blue;">={</span><span style="color: #a31515;">x</span><span style="color: blue;">:</span><span style="color: #a31515;">Static</span><span style="color: red;"> classes</span><span style="color: blue;">:</span><span style="color: red;">StaticBinder</span><span style="color: blue;">.Instance},</span><span style="color: red;"> Path</span><span style="color: blue;">=MSG_Cancel}"</span></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Width</span><span style="color: blue;">="100"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Margin</span><span style="color: blue;">="5"></</span><span style="color: #a31515;">Button</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">For you to check the actual two-dimensional binding, please follow the code behind implementation below.</span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">WIN_StaticBinding</span>
: <span style="color: #2b91af;">Window</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> WIN_StaticBinding()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
InitializeComponent();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">void</span>
okayButton_Click(<span style="color: blue;">object</span> sender, <span style="color: #2b91af;">RoutedEventArgs</span> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Classes.<span style="color: #2b91af;">StaticBinder</span>.Instance.MSG_OK
= <span style="color: #a31515;">"Okay"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
</div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You will notice that upon click of the </span><span style="font-family: Courier New, Courier, monospace;">OK</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button, the changes will then reflect to the UI.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And that's all about it. You just finished reading this blog.</span></div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-66757877970744985612013-01-27T22:34:00.002-08:002013-01-29T01:55:24.283-08:00WPF Binding to a Static Property<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In </span><span style="font-family: Courier New, Courier, monospace;">WPF</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, the binding provides an efficient and easy way of data interaction between components. Every </span><span style="font-family: Courier New, Courier, monospace;">FrameworkElement</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> can be bound to any data sources in the form of </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">CLR</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The binding technique is one of the best way to simplify the implementations in </span><span style="font-family: Courier New, Courier, monospace;">WPF</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. But you should understand that every class in </span><span style="font-family: Courier New, Courier, monospace;">.NET</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> only participates in two way binding if you have implemented the </span><span style="font-family: Courier New, Courier, monospace;">IPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> interface or you used </span><span style="font-family: Courier New, Courier, monospace;">DependencyProperty</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Binding to a Static Property</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Binding to static property of the class is slightly complex than normal binding. If you have a class with static property, you atleast need to address the requirement whether the binding is one or two dimensional. Please note that static property is not an instance type property and you should do your own trick to notify the changes in the </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">First of all, we should create a class (note: don't make it </span><span style="font-family: Courier New, Courier, monospace;">static</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">). Then, place all static property you wish to put in this class. In our case, we will name the class </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and will put three property named </span><span style="font-family: Courier New, Courier, monospace;">MSG_OK</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">, </span><span style="font-family: Courier New, Courier, monospace;">MSG_Cancel</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">MSG_Welcome</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">. See below the implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">StaticBinder</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> StaticBinder()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
MSG_Cancel = <span style="color: #a31515;">"Welcome"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
MSG_OK = <span style="color: #a31515;">"OK"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
MSG_Welcome = <span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"Welcome {0}!"</span>, <span style="color: #a31515;">"WPF
Binding"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> MSG_Cancel<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> MSG_OK<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">string</span> MSG_Welcome<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">You will notice that we created one static constructor. In there, we place all the property initialization and call each of them statically. Now, in the </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">, we need to create a </span><span style="font-family: Courier New, Courier, monospace;">Window</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> with </span><span style="font-family: Courier New, Courier, monospace;">Label</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Button</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> controls embedded that can be bound on the class above. See below our implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Class</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="CodesDirectory.WIN_StaticBinding"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Title</span><span style="color: blue;">="WIN Static Binding"</span><span style="color: red;"> Height</span><span style="color: blue;">="300"</span><span style="color: red;"> Width</span><span style="color: blue;">="300"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> WindowStartupLocation</span><span style="color: blue;">="CenterScreen"</span><span style="color: red;">
ResizeMode</span><span style="color: blue;">="NoResize"></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="5"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid.RowDefinitions</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Height</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="*"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Height</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Auto"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid.RowDefinitions</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Label</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Grid.Row</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="0"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Label</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Grid.Row</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="1"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> HorizontalAlignment</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Right"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Orientation</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Horizontal"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Button</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="okayButton"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Click</span><span style="color: blue;">="okayButton_Click"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Width</span><span style="color: blue;">="100"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Margin</span><span style="color: blue;">="5"></</span><span style="color: #a31515;">Button</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Button</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="cancelButton"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Width</span><span style="color: blue;">="100"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Margin</span><span style="color: blue;">="5"></</span><span style="color: #a31515;">Button</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">><o:p></o:p></span></div>
<br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now before you can participate your Window in the binding, you should create a reference to the class we created above via </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">. You should put an </span><span style="font-family: Courier New, Courier, monospace;">xmlns (XML Namespace)</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> that directs to the namespace where the </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> class resides, and you should create a new instance of </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> class and put it in the Window resources. See below the additional codes in <span style="background-color: yellow;">yellow</span> background.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Class</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="CodesDirectory.WIN_StaticBinding"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Title</span><span style="color: blue;">="WIN Static Binding"</span><span style="color: red;"> Height</span><span style="color: blue;">="300"</span><span style="color: red;"> Width</span><span style="color: blue;">="300"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> <span style="background-color: yellow;">xmlns</span></span><span style="background-color: yellow;"><span style="color: blue;">:</span><span style="color: red;">classes</span><span style="color: blue;">="clr-namespace:CodesDirectory.Classes"</span></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> WindowStartupLocation</span><span style="color: blue;">="CenterScreen"</span><span style="color: red;">
ResizeMode</span><span style="color: blue;">="NoResize"></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="background-color: yellow;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window.Resources</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background-color: yellow;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">ObjectDataProvider</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Key</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="staticBinder"</span></span><span style="background-color: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><span style="background-color: white;"> </span><span style="color: red;"><span style="background-color: white;"> </span><span style="background-color: yellow;">ObjectType</span></span><span style="background-color: yellow;"><span style="color: blue;">="{</span><span style="color: #a31515;">x</span><span style="color: blue;">:</span><span style="color: #a31515;">Type</span><span style="color: red;"> classes</span><span style="color: blue;">:</span><span style="color: red;">StaticBinder</span><span style="color: blue;">}"></</span><span style="color: #a31515;">ObjectDataProvider</span><span style="color: blue;">></span></span><span style="background-color: white;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="background-color: yellow;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window.Resources</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="5"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid.RowDefinitions</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Height</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="*"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Height</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Auto"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid.RowDefinitions</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Label</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Grid.Row</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="0"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Label</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Grid.Row</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="1"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> HorizontalAlignment</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Right"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Orientation</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Horizontal"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Button</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="okayButton"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Click</span><span style="color: blue;">="okayButton_Click"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Width</span><span style="color: blue;">="100"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Margin</span><span style="color: blue;">="5"></</span><span style="color: #a31515;">Button</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Button</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="cancelButton"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Width</span><span style="color: blue;">="100"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Margin</span><span style="color: blue;">="5"></</span><span style="color: #a31515;">Button</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">With the </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> codes above, you will notice that our namespace name is '</span><span style="font-family: Courier New, Courier, monospace;">classes</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">'. You can change it in any name if you wish. Also, we created a new object named </span><span style="font-family: Courier New, Courier, monospace;">ObjectDataProvider</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> with key value </span><span style="font-family: Courier New, Courier, monospace;">staticBinder</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">. The </span><span style="font-family: Courier New, Courier, monospace;">ObjectDataProvider</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> is an object that provides us a dynamic resource data binder in the </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">. It can also be used to instantiate and call the class methods in your application.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Let's continue to the actual binding. Now in the </span><span style="font-family: Courier New, Courier, monospace;">Content</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property of the </span><span style="font-family: Courier New, Courier, monospace;">Button</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Label</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">, use the </span><span style="font-family: Courier New, Courier, monospace;">StaticResource</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> that connects the </span><span style="font-family: Courier New, Courier, monospace;">ObjectDataProvider</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and set the </span><span style="font-family: Courier New, Courier, monospace;">Path</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property equal to the actual </span><span style="font-family: Courier New, Courier, monospace;">Property</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> of the </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> class. See below the codes in <span style="background-color: yellow;">yellow</span> background.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Class</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="CodesDirectory.WIN_StaticBinding"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Title</span><span style="color: blue;">="WIN Static Binding"</span><span style="color: red;"> Height</span><span style="color: blue;">="300"</span><span style="color: red;"> Width</span><span style="color: blue;">="300"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">classes</span><span style="color: blue;">="clr-namespace:CodesDirectory.Classes"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> WindowStartupLocation</span><span style="color: blue;">="CenterScreen"</span><span style="color: red;">
ResizeMode</span><span style="color: blue;">="NoResize"></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window.Resources</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">ObjectDataProvider</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Key</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="staticBinder"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> ObjectType</span><span style="color: blue;">="{</span><span style="color: #a31515;">x</span><span style="color: blue;">:</span><span style="color: #a31515;">Type</span><span style="color: red;"> classes</span><span style="color: blue;">:</span><span style="color: red;">StaticBinder</span><span style="color: blue;">}"></</span><span style="color: #a31515;">ObjectDataProvider</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window.Resources</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="5"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid.RowDefinitions</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Height</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="*"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Height</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Auto"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">RowDefinition</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid.RowDefinitions</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Label</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Grid.Row</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="0"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> <span style="background-color: yellow;">Content</span></span><span style="background-color: yellow;"><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;"> Source</span><span style="color: blue;">={</span><span style="color: #a31515;">StaticResource</span><span style="color: red;"> ResourceKey</span><span style="color: blue;">=staticBinder},</span><span style="color: red;"> Path</span></span><span style="color: blue;"><span style="background-color: yellow;">=MSG_Welcome}"</span>></</span><span style="color: #a31515;">Label</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Grid.Row</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="1"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> HorizontalAlignment</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Right"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Orientation</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Horizontal"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Button</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="okayButton"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Click</span><span style="color: blue;">="okayButton_Click"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> <span style="background-color: yellow;">Content</span></span><span style="background-color: yellow;"><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;"> Source</span><span style="color: blue;">={</span><span style="color: #a31515;">StaticResource</span><span style="color: red;"> ResourceKey</span><span style="color: blue;">=staticBinder},</span><span style="color: red;"> Path</span><span style="color: blue;">=MSG_OK}"</span></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Width</span><span style="color: blue;">="100"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Margin</span><span style="color: blue;">="5"></</span><span style="color: #a31515;">Button</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Button</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="cancelButton"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> <span style="background-color: yellow;">Content</span></span><span style="background-color: yellow;"><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;"> Source</span><span style="color: blue;">={</span><span style="color: #a31515;">StaticResource</span><span style="color: red;"> ResourceKey</span><span style="color: blue;">=staticBinder},</span><span style="color: red;"> Path</span><span style="color: blue;">=MSG_Cancel}"</span></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Width</span><span style="color: blue;">="100"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: red;"> Margin</span><span style="color: blue;">="5"></</span><span style="color: #a31515;">Button</span><span style="color: blue;">></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Grid</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please note that the implementation above doesn't participates in the two directional binding. Any changes happens on the </span><span style="font-family: Courier New, Courier, monospace;">StaticBinder</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class property will not reflect in the UI. Please continue read the blog <a href="http://codesdirectory.blogspot.com/2013/01/wpf-binding-to-static-instance.html" target="_blank">WPF Binding to Static Instance</a> if you wish to understand how to implement two directional binding.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com2tag:blogger.com,1999:blog-2161576857104408209.post-12628322614479137402013-01-27T05:03:00.003-08:002013-01-29T21:32:43.141-08:00Uninstall SQL Server Instance<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>How to uninstall an existing installed SQL Server Instance?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">This blog will guide you how to uninstall an existing installed SQL Server Instance. If you have multiple installed instance of SQL Server in your local PC then this blog address your problem. Please follow the steps provided below.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">First and foremost, go to </span><span style="font-family: Courier New, Courier, monospace;">Control Panel</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and select the </span><span style="font-family: Courier New, Courier, monospace;">Microsoft SQL Server 2008 (64 bit)</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> entry from the list. Once selected, click the </span><span style="font-family: Courier New, Courier, monospace;">Uninstall/Change</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> menu from the panel. See the image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkYfxqCFjvyf_jlyoG43SLjXcUBHyOyMVjdllE4XB78d0yNqQ5n-d3cBI5WF41OrBSAGOXGRZLYaZCDs663CgAola7BGXm1QiruyZai589sdjgatvLO3R3OOw77hrM43KJOcJ6FJZ9xEk/s1600/Control+Panel.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="281" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkYfxqCFjvyf_jlyoG43SLjXcUBHyOyMVjdllE4XB78d0yNqQ5n-d3cBI5WF41OrBSAGOXGRZLYaZCDs663CgAola7BGXm1QiruyZai589sdjgatvLO3R3OOw77hrM43KJOcJ6FJZ9xEk/s400/Control+Panel.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After clicking of the Uninstall/Change button, the </span><span style="font-family: Courier New, Courier, monospace;">SQL Server Uninstallation Options</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> will be displayed. From there, please select the </span><span style="font-family: Courier New, Courier, monospace;">Remove</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> link. Please see the image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuHWmHrwf8U_ZLt0H9hTBNWCN-fsUdR3l7Nx6EOWjivNHzeO9yozXDxrachcFED0kO5OEDtG_9ByuTyshRx0v3mH4LBFuNDzEZh8qXA9RxvsFdLzM-j5RfWOuXY-Bmu9gATJL3ESddvgc/s1600/SQL+Server+Uninstall+Options.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuHWmHrwf8U_ZLt0H9hTBNWCN-fsUdR3l7Nx6EOWjivNHzeO9yozXDxrachcFED0kO5OEDtG_9ByuTyshRx0v3mH4LBFuNDzEZh8qXA9RxvsFdLzM-j5RfWOuXY-Bmu9gATJL3ESddvgc/s1600/SQL+Server+Uninstall+Options.PNG" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">Setup Support Rules</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> will then check the system requirements. Ensure that all </span><span style="font-family: Courier New, Courier, monospace;">Rule</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is passed otherwise you need to fix it (and the un-installation cannot continue). See image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIXmJEgO_hraZHfnCz_U7PechKB7L-nxD2voXDQFC7FO2g1oqlnQrBqIg-PQNqyT7bbKGSJhg1LuPYKb2AakVLyyxkTUV_d6vZjDPqtU5Les2YgPtOqNXtYnrZHGC5AGgnetkQnMQLSqw/s1600/Setup+Support+Rules.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIXmJEgO_hraZHfnCz_U7PechKB7L-nxD2voXDQFC7FO2g1oqlnQrBqIg-PQNqyT7bbKGSJhg1LuPYKb2AakVLyyxkTUV_d6vZjDPqtU5Les2YgPtOqNXtYnrZHGC5AGgnetkQnMQLSqw/s400/Setup+Support+Rules.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Just click the </span><span style="font-family: Courier New, Courier, monospace;">OK</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button if you see all the entries are pass.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In the next window, select the SQL Server Instance you wish to remove or modify. In our case, we are removing our existing installed </span><span style="font-family: Courier New, Courier, monospace;">Express</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> edition instance. Just simply select the proper entry in the '</span><span style="font-family: Courier New, Courier, monospace;">Instance to remove from</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">' field.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">See the image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5r1NpM3-wICB7zKwKk0PXo_KomUi6bmutsMiT52Ob1LycM5e5_cuSnIcrpfVnJXb8_GRqsgNzKatBHWhspS4zFRDFIKVMYLOVNcM-7tgb8IrA65cdqmHVGwC5axo8Y8fUqHi9xTTegAM/s1600/Select+Instance.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5r1NpM3-wICB7zKwKk0PXo_KomUi6bmutsMiT52Ob1LycM5e5_cuSnIcrpfVnJXb8_GRqsgNzKatBHWhspS4zFRDFIKVMYLOVNcM-7tgb8IrA65cdqmHVGwC5axo8Y8fUqHi9xTTegAM/s400/Select+Instance.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Once you select the proper entry, just click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button to proceed. The next window will show you what features are you going to remove from the SQL Server instance. Our advice is to select all features installed the current selected SQL Server instance. See the image below.</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDDgxuaGCTOQfpuuM4EaF3vSvZfZvaIoQkyiiraAMTaxpvEC99a8Hg4HEJs7o2RSNyxxQrWhn4WDsNiPhyphenhyphenepeIpzRt46e7hSmCuDT58z2Eu4Zm9x4a-BdghXIVrdnVW-iBzP0bgkZcqxc/s1600/Select+Features.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDDgxuaGCTOQfpuuM4EaF3vSvZfZvaIoQkyiiraAMTaxpvEC99a8Hg4HEJs7o2RSNyxxQrWhn4WDsNiPhyphenhyphenepeIpzRt46e7hSmCuDT58z2Eu4Zm9x4a-BdghXIVrdnVW-iBzP0bgkZcqxc/s1600/Select+Features.PNG" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Just click the </span><span style="font-family: Courier New, Courier, monospace;">Select All</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button to select all entries then click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button. After that the next window will display the removal rules. See the image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqCjtMhSSebKFVw4fqk9zIqPVt4JnEoFhm106gwFcz3e3-S48uR31VYqHgBmmFrx9Wlh74Zs0luuk_OhNJct1phHYzFF5S6EJbDCaW1Th3Eltd2GN6r8q2-TjQhyphenhyphenEe6_hlZCYlDQNQ8QY/s1600/Removal+Rules.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqCjtMhSSebKFVw4fqk9zIqPVt4JnEoFhm106gwFcz3e3-S48uR31VYqHgBmmFrx9Wlh74Zs0luuk_OhNJct1phHYzFF5S6EJbDCaW1Th3Eltd2GN6r8q2-TjQhyphenhyphenEe6_hlZCYlDQNQ8QY/s400/Removal+Rules.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button. In the next window the </span><span style="font-family: Courier New, Courier, monospace;">Ready to Remove</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> dialog will be displayed to confirm to you the list of features to remove. See the image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKl52N53AwDIjq1QsnnE6zrwawbLsAWaqZvg0godoQGWABd-1v4OPgeLsQMziWhtpOtQK9fLRS-5bMskCXGUd5SisTESpHt18eRHoKTd7VGqC15UC7Qn8PkmAVbTYdrWqthH5YGAUTODc/s1600/Ready+to+Remove.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKl52N53AwDIjq1QsnnE6zrwawbLsAWaqZvg0godoQGWABd-1v4OPgeLsQMziWhtpOtQK9fLRS-5bMskCXGUd5SisTESpHt18eRHoKTd7VGqC15UC7Qn8PkmAVbTYdrWqthH5YGAUTODc/s400/Ready+to+Remove.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">If you are sure to remove the features just click the </span><span style="font-family: Courier New, Courier, monospace;">Remove</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button, otherwise click </span><span style="font-family: Courier New, Courier, monospace;">Back</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button to reset the features to remove. In our case, we will just click the </span><span style="font-family: Courier New, Courier, monospace;">Remove</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The removal progress will be displayed in the next window. See the image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJT_rTeeFc0G8mlDiJnTnP8dNbkhyn3ERnJ8m3ZAc2w2pcEYyV2YcdSX7Mzz9xEg7XwfLGEJNAocbHozrCkMvG8OZWfjrmehbnJx4NnPdBPyubc1DO7DRepQrSargR5uLTy6-QkwDAfFE/s1600/Removal+Progress.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJT_rTeeFc0G8mlDiJnTnP8dNbkhyn3ERnJ8m3ZAc2w2pcEYyV2YcdSX7Mzz9xEg7XwfLGEJNAocbHozrCkMvG8OZWfjrmehbnJx4NnPdBPyubc1DO7DRepQrSargR5uLTy6-QkwDAfFE/s400/Removal+Progress.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Once completed, just click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button and in the next window it will only display the link to the log file for your un-installation report status. Just click the </span><span style="font-family: Courier New, Courier, monospace;">Close</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button to close the window. See image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFxpkhqne76nI0Cv-TZJPdnALWDDJQACqKP8GmMFRcIc485lqq3X2pnlWtLvdiP8KECoOiXiamubicCxXeJJSIlrJ81z_qYSGORgzHOpBQF2SVhgcC22OU2CtsfvlOp6VCgUXCj1-kYj8/s1600/Success.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="131" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFxpkhqne76nI0Cv-TZJPdnALWDDJQACqKP8GmMFRcIc485lqq3X2pnlWtLvdiP8KECoOiXiamubicCxXeJJSIlrJ81z_qYSGORgzHOpBQF2SVhgcC22OU2CtsfvlOp6VCgUXCj1-kYj8/s400/Success.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<b style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Other Solution</b><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Alternative solution from Microsoft can be found <a href="http://msdn.microsoft.com/en-us/library/ms191545.aspx#Remove" target="_blank">here</a>.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Note:</b> You need to restart the PC after un-installing the SQL Server instance to have effect in the system. So better do follow the process by restarting your PC to avoid conflicts.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You just learned how to un-installed an existing SQL Server instance.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-83495547239963769682013-01-26T23:05:00.000-08:002013-01-29T21:33:49.365-08:00Creating a Database and Tables in SQL Server<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">This blog will guide you how to create a database and tables in SQL Server. The audience of this blog are those who don't have enough experience in SQL Server yet. For those who are very keen in using SQL Server, please skip this blog.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">If you have your SQL Server installed in your PC then we are ready to go. Otherwise, please visit the <a href="http://codesdirectory.blogspot.com/2013/01/how-to-install-sql-server-2008.html" target="_blank">How to Install SQL Server 2008</a> blog to guide you how to install it.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Creating a Database</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now we are expecting that you already installed SQL Server 2008 Express Edition in your PC. First, you have to open the Microsoft SQL Server Management Studio by clicking the icon from your Desktop. If it has not placed in the Desktop, then please click the </span><span style="font-family: Courier New, Courier, monospace;">Start</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button All </span><span style="font-family: Courier New, Courier, monospace;">Programs->Microsoft SQL Server 2008->SQL Server Management Studio</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The SQL Server Management Studio looks like below.</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg62lB5deEr7t3bGGDPPq8uB595DCLx9AStLXTkVfyHGawyAhdV8i_Mf-Gr2w-IaqWLky-iDj24ZSj8Vozd5Vh_ONkZeF8UJUdX5T-JawTdYtuffpYJh4ZnEMKEo2IJDkgEB7mQUqnoGwQ/s1600/SQL+Server+Management+Studio.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="267" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg62lB5deEr7t3bGGDPPq8uB595DCLx9AStLXTkVfyHGawyAhdV8i_Mf-Gr2w-IaqWLky-iDj24ZSj8Vozd5Vh_ONkZeF8UJUdX5T-JawTdYtuffpYJh4ZnEMKEo2IJDkgEB7mQUqnoGwQ/s400/SQL+Server+Management+Studio.PNG" width="400" /></a></div>
<br />
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">By default, it will automatically display the </span><span style="font-family: Courier New, Courier, monospace;">Object Explorer</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. If you haven't seen the </span><span style="font-family: Courier New, Courier, monospace;">Object Explorer</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> then please go to menu </span><span style="font-family: Courier New, Courier, monospace;">View->Object Explorer</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or press </span><span style="font-family: Courier New, Courier, monospace;">F8</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After that, click the </span><span style="font-family: Courier New, Courier, monospace;">Connect</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> label located in the </span><span style="font-family: Courier New, Courier, monospace;">Object Explorer</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> to connect in the available </span><span style="font-family: Courier New, Courier, monospace;">SQL Server</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> instance you had created during the installation. See the image below the </span><span style="font-family: Courier New, Courier, monospace;">Connect to Server</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> window.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjT-DsMBaO9l40oNE9ookR3dCp-a7ZxVDPnSi7JYBNnSaN3e-IXmh3V3FalXlgiTD2BRQLH_UZ6K5u5eafQwt-rNO45dnBzNACuu846psiF8MFTs2Uh1ZacN3blJ9-CQcn1Kc3mgscmDec/s1600/Connect+To+Server.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="301" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjT-DsMBaO9l40oNE9ookR3dCp-a7ZxVDPnSi7JYBNnSaN3e-IXmh3V3FalXlgiTD2BRQLH_UZ6K5u5eafQwt-rNO45dnBzNACuu846psiF8MFTs2Uh1ZacN3blJ9-CQcn1Kc3mgscmDec/s400/Connect+To+Server.png" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">There you see the </span><span style="font-family: Courier New, Courier, monospace;">Server type</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, </span><span style="font-family: Courier New, Courier, monospace;">Server name</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Authentication</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> type of connection. Usually if you're working locally you have to place the </span><span style="font-family: Courier New, Courier, monospace;">(local)</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">dot</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> in the </span><span style="font-family: Courier New, Courier, monospace;">Server name</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. The server type usually </span><span style="font-family: Courier New, Courier, monospace;">Database Engine</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. If you're working locally or if you're working with windows authenticated environment, then leave the </span><span style="font-family: Courier New, Courier, monospace;">Authentication</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> value to </span><span style="font-family: Courier New, Courier, monospace;">Windows Authentication</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. However, if you know you're connecting in the secure database you have to provide the authentication process credentials. In the </span><span style="font-family: Courier New, Courier, monospace;">Authentication</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> field select the </span><span style="font-family: Courier New, Courier, monospace;">SQL Server Authentication</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and enter the </span><span style="font-family: Courier New, Courier, monospace;">Username</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Password</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> credential. Click </span><span style="font-family: Courier New, Courier, monospace;">Connect</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button and wait until the </span><span style="font-family: Courier New, Courier, monospace;">Object Explorer</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is filled with a new database connection instance.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Select the new database instance and expand it. Select the </span><span style="font-family: Courier New, Courier, monospace;">Databases</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and select </span><span style="font-family: Courier New, Courier, monospace;">New Database</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. See below the image.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXolPr3vhruh3OG-wxW5kTyu4lmLJccf8VT_oVKZn1gz3KNGrJHW2WLe2T0lnoBJExn8JqebMdLv-HD7TX8qc3u-eQOtqnSigKNaBhb5MYdWx6wECUu8nsFzl-vDe3aEi6CFlUfqDBsgQ/s1600/New+Database.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="255" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXolPr3vhruh3OG-wxW5kTyu4lmLJccf8VT_oVKZn1gz3KNGrJHW2WLe2T0lnoBJExn8JqebMdLv-HD7TX8qc3u-eQOtqnSigKNaBhb5MYdWx6wECUu8nsFzl-vDe3aEi6CFlUfqDBsgQ/s400/New+Database.png" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After clicking the </span><span style="font-family: Courier New, Courier, monospace;">New Database</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> menu, you will see the create database dialog. See below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhs8zwMUkOgy177K-CIAURaKWz1V_G9Wi7Ay46GgFeq7BdeD9gihM7d4wiWPkn0IShSiCHnlwGU-ZdrDtS0pXHHWuHZ9oHZDmo_Zjci8aug5Y_8gNIAIqcFbBzjofs847did01G-sYZZoo/s1600/New+Database+Dialog.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="357" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhs8zwMUkOgy177K-CIAURaKWz1V_G9Wi7Ay46GgFeq7BdeD9gihM7d4wiWPkn0IShSiCHnlwGU-ZdrDtS0pXHHWuHZ9oHZDmo_Zjci8aug5Y_8gNIAIqcFbBzjofs847did01G-sYZZoo/s400/New+Database+Dialog.png" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In the database name, please type the name of the database you wish to create. The most common name for the database is the actual name of you application (but it is up to you). If you wish to create an advance database, you can modify the </span><span style="font-family: Courier New, Courier, monospace;">Options</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> (type of connection and its properties) and </span><span style="font-family: Courier New, Courier, monospace;">Filegroups</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> to define what schema this database are part of. For your convenient let's just start in the basic functionality.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">If you're done with your database name, click </span><span style="font-family: Courier New, Courier, monospace;">OK</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> button. You will notice that the </span><span style="font-family: Courier New, Courier, monospace;">Object Explorer</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> databases node has been filled with your newly created database. See below the image.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrbLVyvjBZj92ZXS4UaPr3A4qEyVZZgMh_gJ2ZWreVF0vl5TEJFj51xFrbrg0Hx1eThFsPCiTAUHfR7hr0W2POarR1EAtyJlmY9ZNWbRPlsBcpAY-l9wi_yNlhcmucTUC9CoqatW0UGq8/s1600/Databases+Node+Object+Explorer.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrbLVyvjBZj92ZXS4UaPr3A4qEyVZZgMh_gJ2ZWreVF0vl5TEJFj51xFrbrg0Hx1eThFsPCiTAUHfR7hr0W2POarR1EAtyJlmY9ZNWbRPlsBcpAY-l9wi_yNlhcmucTUC9CoqatW0UGq8/s400/Databases+Node+Object+Explorer.PNG" width="294" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And that's all. You just learn how to create a SQL Database.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Creating a Table</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">If you follow our tutorial above weI created our own database named </span><span style="font-family: Courier New, Courier, monospace;">CodesDirectory</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. From there, we will start our process by expanding the </span><span style="font-family: Courier New, Courier, monospace;">CodesDirectory</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> database and right-click from the </span><span style="font-family: Courier New, Courier, monospace;">Tables</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> folder. Select </span><span style="font-family: Courier New, Courier, monospace;">New Table</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> menu and it will show you the table designer. From that designer please follow our image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiezLPnX4hLWpERlSCY0tETeB3THT_Th1nsbCZcvNsRimsncB_Tznc2lqys4bKSzu1XP8mDyQfviDE4nf1zVnDUDK8NLqzByE0urKgFfHO9Hwa6_L1nYbS9hAh88u243IfQ2Ra9e-diFco/s1600/Table+Designer.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="192" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiezLPnX4hLWpERlSCY0tETeB3THT_Th1nsbCZcvNsRimsncB_Tznc2lqys4bKSzu1XP8mDyQfviDE4nf1zVnDUDK8NLqzByE0urKgFfHO9Hwa6_L1nYbS9hAh88u243IfQ2Ra9e-diFco/s400/Table+Designer.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Once you're done with your designing, please save the table by pressing the </span><span style="font-family: Courier New, Courier, monospace;">Ctrl+S keys</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. It will show you a new dialog that prompts the table name. In our sample, we created it as </span><span style="font-family: Courier New, Courier, monospace;">User</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. See image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIAIZouUzFWp_y0Viiyiusr7oYnFpnhCEtVuT6vRO1TjcJd4KaruhWecpqcZ-K6Csszj_JWQMJH4fjWxF-VhCH4ik2HPsLLcaeoRWXtRuyX7B41B8UNQK0tSRlowmyWXKZkFpoIa2ZZdc/s1600/Table+Name+Prompt.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="147" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIAIZouUzFWp_y0Viiyiusr7oYnFpnhCEtVuT6vRO1TjcJd4KaruhWecpqcZ-K6Csszj_JWQMJH4fjWxF-VhCH4ik2HPsLLcaeoRWXtRuyX7B41B8UNQK0tSRlowmyWXKZkFpoIa2ZZdc/s400/Table+Name+Prompt.PNG" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now you will notice again that the </span><span style="font-family: Courier New, Courier, monospace;">CodesDirectory</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> database </span><span style="font-family: Courier New, Courier, monospace;">Tables</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> node will have a new entry named </span><span style="font-family: Courier New, Courier, monospace;">User</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. That is just the table we just created named </span><span style="font-family: Courier New, Courier, monospace;">User</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. In SQL Server, user has an option to create a database table by user interface or by SQL Text. If you wish to create a table by SQL Text, then you have to learn more in </span><span style="font-family: Courier New, Courier, monospace;">DDL</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> (</span><span style="font-family: Courier New, Courier, monospace;">Data Definition Language</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">) stuff.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Sample below is the way how to retrieve the corresponding SQL Text when creating a table.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Scripting table.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgod-J4RkcR3uSF_9ijAj7GnVmjHsYTYWxPm3WYZTgOmdkvyZtNnN1SMheknDvniMpih3ZtMueAGRiIrQuQYCw5eitrOeFr3xNvzM5hnT0oNY88TKjccoO3JxsV5XNfRh1wfUL5jHuP2zs/s1600/Scripting+Table.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="285" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgod-J4RkcR3uSF_9ijAj7GnVmjHsYTYWxPm3WYZTgOmdkvyZtNnN1SMheknDvniMpih3ZtMueAGRiIrQuQYCw5eitrOeFr3xNvzM5hnT0oNY88TKjccoO3JxsV5XNfRh1wfUL5jHuP2zs/s400/Scripting+Table.png" width="400" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You can choose other option in the </span><span style="font-family: Courier New, Courier, monospace;">Script Table as</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> menu if you wish. But in our case, we just displayed how the </span><span style="font-family: Courier New, Courier, monospace;">User</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> table has been created via SQL Text. Below is the actual script how to create a </span><span style="font-family: Courier New, Courier, monospace;">User</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> table.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span>
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">USE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
[CodesDirectory]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">/******
Object: Table [dbo].[User] Script Date: 01/27/2013 13:49:54 ******/<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ANSI_NULLS</span> <span style="color: blue;">ON<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">QUOTED_IDENTIFIER</span> <span style="color: blue;">ON<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">CREATE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">TABLE</span> [dbo]<span style="color: grey;">.</span>[User]<span style="color: grey;">(<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> [UserID] [uniqueidentifier] <span style="color: grey;">NOT</span>
<span style="color: grey;">NULL,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> [Username] [nvarchar]<span style="color: grey;">(</span>32<span style="color: grey;">)</span> <span style="color: grey;">NOT</span> <span style="color: grey;">NULL,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> [Firstname] [nvarchar]<span style="color: grey;">(</span>32<span style="color: grey;">)</span> <span style="color: grey;">NOT</span> <span style="color: grey;">NULL,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> [Lastname] [nvarchar]<span style="color: grey;">(</span>32<span style="color: grey;">)</span> <span style="color: grey;">NOT</span> <span style="color: grey;">NULL,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> [Birthdate] [datetime] <span style="color: grey;">NOT</span> <span style="color: grey;">NULL,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> [SerialNumber] [nvarchar]<span style="color: grey;">(</span>24<span style="color: grey;">)</span> <span style="color: grey;">NULL,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> [CreatedDate] [datetime] <span style="color: grey;">NOT</span> <span style="color: grey;">NULL<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ON</span> [PRIMARY]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ALTER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">TABLE</span> [dbo]<span style="color: grey;">.</span>[User] <span style="color: blue;">ADD</span> <span style="color: blue;">CONSTRAINT</span> [DF_User_UserID] <span style="color: blue;">DEFAULT </span><span style="color: grey;">(</span><span style="color: magenta;">newid</span><span style="color: grey;">())</span> <span style="color: blue;">FOR</span> [UserID]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ALTER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">TABLE</span> [dbo]<span style="color: grey;">.</span>[User] <span style="color: blue;">ADD</span> <span style="color: blue;">CONSTRAINT</span> [DF_User_CreatedDate] <span style="color: blue;">DEFAULT </span><span style="color: grey;">(</span><span style="color: magenta;">getutcdate</span><span style="color: grey;">())</span> <span style="color: blue;">FOR</span> [CreatedDate]<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">GO</span></div>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">If you wish to create a database by SQL Text, you need to study more the </span><span style="font-family: Courier New, Courier, monospace;">DML</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> vs </span><span style="font-family: Courier New, Courier, monospace;">DDL</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. The </span><span style="font-family: Courier New, Courier, monospace;">DML</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">Data Manipulation Language</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is an SQL Text in SQL Server that is being used for data manipulation operation. Basic keyword includes </span><span style="font-family: Courier New, Courier, monospace;">SELECT</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, </span><span style="font-family: Courier New, Courier, monospace;">UPDATE</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">DELETE</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. The </span><span style="font-family: Courier New, Courier, monospace;">DDL</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">Data Definition Language</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is the administrator task that manipulates around database schema. Basic keywords are </span><span style="font-family: Courier New, Courier, monospace;">CREATE</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, ALTER and </span><span style="font-family: Courier New, Courier, monospace;">DROP</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com1tag:blogger.com,1999:blog-2161576857104408209.post-4837544845192303342013-01-26T22:54:00.002-08:002013-01-29T21:34:29.686-08:00How to Install SQL Server 2008<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">This blog will guide you how to install SQL Server 2008 in your PC.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">First of all, if you don't have the SQL Server installer yet then you need visit Microsoft </span><a href="http://www.microsoft.com/en-us/download/details.aspx?id=1695" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;" target="_blank">Express Edition SQL Server 2008</a><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and download it. Once downloaded, click the </span><span style="font-family: Courier New, Courier, monospace;">Setup.exe</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> file to install the SQL Server 2008. It will take sometime because you downloaded only the installer file (but you haven't downloaded all the required files to install SQL Server 2008). But if you already have your complete SQL Server files, then we are ready to go.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The first dialog that will appear is the SQL Server Installation Center. See the image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2ONofIx1JAHq8SSlROwp452ADMOop30xB0IzvQZmFbi4HN-Lw-0E2u6nZHlNfVtNhwprXn-D0FOD73qtBtdeertzO1eou4IYAljIK3JBRDIRQOLCJB_2dqJJ-xRW1DmVvh0Ld1p6pLzs/s1600/SQL+Server+Installation+Center.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="261" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2ONofIx1JAHq8SSlROwp452ADMOop30xB0IzvQZmFbi4HN-Lw-0E2u6nZHlNfVtNhwprXn-D0FOD73qtBtdeertzO1eou4IYAljIK3JBRDIRQOLCJB_2dqJJ-xRW1DmVvh0Ld1p6pLzs/s400/SQL+Server+Installation+Center.png" width="400" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">From the left, select the menu </span><span style="font-family: Courier New, Courier, monospace;">Installation</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and select the first entry '</span><span style="font-family: Courier New, Courier, monospace;">New SQL Server stand-alone installation or add features to an existing installation</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">'. This selection will install the stand-alone SQL Server in your PC (or modify the existing installation if you wish to add other features).</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">After clicking it, the </span><span style="font-family: Courier New, Courier, monospace;">Setup Support Roles</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> dialog will check your system whether there are possible problem might occur when installing the SQL Server. This is a pro-active feature as it is checking the system configuration in advance. If there are problems found in your system, you need to fix it by following the guidance from Microsoft SQL Server installer guide. Otherwise, the screen below will display.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3_aSsPP29Pxv6QbJyeMon11Zr_-KVnKk-kFBIcMfmgidlgl4ORbKNpbmx4aq9ATrgHVJYfJgz6TzRxXE0PU627FqdtRmafbSpKDCB6cjZLtiKd9rzfu5oKPOTQX7EoNAJhTuacxvtdj4/s1600/Setup+Support+Rules.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3_aSsPP29Pxv6QbJyeMon11Zr_-KVnKk-kFBIcMfmgidlgl4ORbKNpbmx4aq9ATrgHVJYfJgz6TzRxXE0PU627FqdtRmafbSpKDCB6cjZLtiKd9rzfu5oKPOTQX7EoNAJhTuacxvtdj4/s400/Setup+Support+Rules.png" width="400" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Just click the </span><span style="font-family: Courier New, Courier, monospace;">OK</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button to proceed. On the next window just click the </span><span style="font-family: Courier New, Courier, monospace;">Install</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button. It will install the </span><span style="font-family: Courier New, Courier, monospace;">Setup Support Files</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> needed and it will then proceed to the installation. See image below after clicking the install button.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Please note that if you have a firewall running you need to disable it temporarily so the SQL Server installation will not be interrupted during the installation for the blocked contents.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyeUBA5-XCFr-A9BPreGq-S3A0mnfCYfL7jSo0juP4jjWV7EWNxb73io4Iy4n5f0L9Mt-ju13iIrwDUocg2IJ5Be1S3hry5oIoWDqBQmDt8qKYTH75uM6xMWYZo0W6PXAqOBqTgpiTk8U/s1600/Setup+Support+Rules+Installed.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="296" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyeUBA5-XCFr-A9BPreGq-S3A0mnfCYfL7jSo0juP4jjWV7EWNxb73io4Iy4n5f0L9Mt-ju13iIrwDUocg2IJ5Be1S3hry5oIoWDqBQmDt8qKYTH75uM6xMWYZo0W6PXAqOBqTgpiTk8U/s400/Setup+Support+Rules+Installed.png" width="400" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Click then </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button. In the </span><span style="font-family: Courier New, Courier, monospace;">Installation Type</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">, select the </span><span style="font-family: Courier New, Courier, monospace;">Perform a new installation of SQL Server 2008</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> option and click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button again. Enter product key in the next window. See image below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhktEV-rLlKhtScD-3WldNpUPkqUT1AkvbKuOPpaZ9Vh1vEJMfFM1R8vj4YZgBchQ4xrMC5NjK1K4MgWaUfLUY1aybi2Q1ItnTApbGwz4zOrs-S9p-fNhfUH2DQSog5yOL4dhdMMPdSYPk/s1600/Enter+Product+Key.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhktEV-rLlKhtScD-3WldNpUPkqUT1AkvbKuOPpaZ9Vh1vEJMfFM1R8vj4YZgBchQ4xrMC5NjK1K4MgWaUfLUY1aybi2Q1ItnTApbGwz4zOrs-S9p-fNhfUH2DQSog5yOL4dhdMMPdSYPk/s400/Enter+Product+Key.png" width="400" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button again. In the next window, you will the </span><span style="font-family: Courier New, Courier, monospace;">License Terms</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">, you have to read it to fully understand what is written in the terms. If you are okay with it already, just tick the </span><span style="font-family: Courier New, Courier, monospace;">I accept the license terms</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In the next window, you will be prompted if what features of SQL Server you wish to install. See below the image.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0O3zJu0jp86y_Si0x9uc7CF5CTqvI7JPg-hnsUpNL7LJRME4BzcIn6iaYp1ZE5NTZxAD-5YUAOByw1zXpISqI92Cb58LkRcQDAXWXXGCbyrsv9e-y4ep2a0jMuZ-txgXRsBpoBbzjS50/s1600/Features.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0O3zJu0jp86y_Si0x9uc7CF5CTqvI7JPg-hnsUpNL7LJRME4BzcIn6iaYp1ZE5NTZxAD-5YUAOByw1zXpISqI92Cb58LkRcQDAXWXXGCbyrsv9e-y4ep2a0jMuZ-txgXRsBpoBbzjS50/s400/Features.png" width="400" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Notice in the </span><span style="font-family: Courier New, Courier, monospace;">Shared Features</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">, there are disabled check items already. This is because we already installed the SQL Server in our PC and some of the </span><span style="font-family: Courier New, Courier, monospace;">Shared Features</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> has already been installed already. But for the basic feature, please check the </span><span style="font-family: Courier New, Courier, monospace;">Database Engine Services</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">, </span><span style="font-family: Courier New, Courier, monospace;">Management Tools - Basic</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">SQL Client Connectivity SDK</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">. But note that you can install other features if you wish.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">If you are done selecting the features, click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button. There you will be prompted what instance name you wish to create. For this blog we will be using the default instance.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">From the option, select the </span><span style="font-family: Courier New, Courier, monospace;">Default Instance</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and just click and click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button until you see the screen below.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfdEcM6oFZrOQjERnBszgcc66eLxsN6D1dBgwdwGS8tCBtTg67ZP0ZHueLMq06U6aCa-xh_xkSs5ccBWyxL9htHsGnWZCHZ6-a2MC4GV0f10KP6pwD_TNG1AdRfUeG3wSF1O10vefKwGA/s1600/SQL+Server+Credentials.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="299" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfdEcM6oFZrOQjERnBszgcc66eLxsN6D1dBgwdwGS8tCBtTg67ZP0ZHueLMq06U6aCa-xh_xkSs5ccBWyxL9htHsGnWZCHZ6-a2MC4GV0f10KP6pwD_TNG1AdRfUeG3wSF1O10vefKwGA/s400/SQL+Server+Credentials.png" width="400" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In the </span><span style="font-family: Courier New, Courier, monospace;">SQL Server Agent</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">SQL Server Database Engine</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> our advice is to set the </span><span style="font-family: Courier New, Courier, monospace;">Account Name</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Password</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> as same (j</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">ust select your windows credential and type the corresponding windows password). But it is all depends your perusal.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">After that click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In the authentication mode, just select the default </span><span style="font-family: Courier New, Courier, monospace;">Windows Authentication Mode</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> so the SQL Server Instance will automatically use the default windows credential during the authentication. In the administrator account section, just click the </span><span style="font-family: Courier New, Courier, monospace;">Add Current User</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button so you will be the administrator of the current SQL Server Instance. If you want to add other people in your organisation to be an administrator of this instance, just search their name and add them.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Click and click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button again until you arrived in the </span><span style="font-family: Courier New, Courier, monospace;">Installation Progress</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> screen. See below the screenshot.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF7d3jUJAVsBvVW7u8YqzwHInF6PCnkoNrDfdqfOHWeGOyoa5gpoeBhQFv5zMZBicyLYk5vvBsAH_xDrDz0mk4vBZ4lceGIIKJ1WDS6ZJwA9cjKZ5AAtUDbAwEVPEhoVZZ8rWBcO3CHx0/s1600/Installation+Progress+Screen.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF7d3jUJAVsBvVW7u8YqzwHInF6PCnkoNrDfdqfOHWeGOyoa5gpoeBhQFv5zMZBicyLYk5vvBsAH_xDrDz0mk4vBZ4lceGIIKJ1WDS6ZJwA9cjKZ5AAtUDbAwEVPEhoVZZ8rWBcO3CHx0/s400/Installation+Progress+Screen.png" width="400" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">And once the progress finished, you're done with your installation. Click the </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and the next window will give you an option to see the installation logs. Just click the </span><span style="font-family: Courier New, Courier, monospace;">Close</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> button to close the installer wizard.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">That's all the process how to install the SQL Server 2008 edition. Have a happy programming and SQL querying.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com1tag:blogger.com,1999:blog-2161576857104408209.post-65541826663576459302013-01-25T22:31:00.003-08:002013-01-29T01:55:24.278-08:00PART 2: C# WPF TreeView File Explorer with System Icons<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This blog is just a continuation of our previous blog <a href="http://codesdirectory.blogspot.com/2013/01/part-1-c-wpf-treeview-file-explorer.html">PART 1: C# WPF TreeView File Explorer with System Icons</a></span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">. So please visit it first before continuing in this blog.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">And here, we are expecting that you already read the previous blog. Now, you're ready to go in this blog.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><b>Actual Wrapper Implementation</b></span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Below you can see the actual implementation of the </span><span style="font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> wrapper class that we will going to use in our application.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">FileSystemObjectInfo</span> : <span style="color: #2b91af;">BaseObject</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> FileSystemObjectInfo(<span style="color: #2b91af;">FileSystemInfo</span>
info)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span> <span style="color: blue;">is</span> <span style="color: #2b91af;">DummyFileSystemObjectInfo</span>)
<span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Children
= <span style="color: blue;">new</span> <span style="color: #2b91af;">ObservableCollection</span><<span style="color: #2b91af;">FileSystemObjectInfo</span>>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.FileSystemInfo = info;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (info <span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.ImageSource = <span style="color: #2b91af;">FolderManager</span>.GetImageSource(info.FullName, <span style="color: #2b91af;">ItemState</span>.Close);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.AddDummy();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span> (info <span style="color: blue;">is</span> <span style="color: #2b91af;">FileInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.ImageSource = <span style="color: #2b91af;">FileManager</span>.GetImageSource(info.FullName);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.PropertyChanged += <span style="color: blue;">new</span>
System.ComponentModel.<span style="color: #2b91af;">PropertyChangedEventHandler</span>(FileSystemObjectInfo_PropertyChanged);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> FileSystemObjectInfo(<span style="color: #2b91af;">DriveInfo</span>
drive)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> : <span style="color: blue;">this</span>(drive.RootDirectory)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Drive = drive;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
Properties<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">ObservableCollection</span><<span style="color: #2b91af;">FileSystemObjectInfo</span>> Children<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">ObservableCollection</span><<span style="color: #2b91af;">FileSystemObjectInfo</span>>>(<span style="color: #a31515;">"Children"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"Children"</span>,
<span style="color: blue;">value</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">ImageSource</span>
ImageSource<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">ImageSource</span>>(<span style="color: #a31515;">"ImageSource"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"ImageSource"</span>,
<span style="color: blue;">value</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">bool</span>
IsExpanded<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: blue;">bool</span>>(<span style="color: #a31515;">"IsExpanded"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"IsExpanded"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">FileSystemInfo</span>
FileSystemInfo<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">FileSystemInfo</span>>(<span style="color: #a31515;">"FileSystemInfo"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"FileSystemInfo"</span>,
<span style="color: blue;">value</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: #2b91af;">DriveInfo</span>
Drive<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">DriveInfo</span>>(<span style="color: #a31515;">"Drive"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"Drive"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
Methods<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">void</span>
AddDummy()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Children.Add(<span style="color: blue;">new</span>
<span style="color: #2b91af;">DummyFileSystemObjectInfo</span>());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">bool</span>
HasDummy()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> !<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.GetDummy(), <span style="color: blue;">null</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: #2b91af;">DummyFileSystemObjectInfo</span>
GetDummy()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> list = <span style="color: blue;">this</span>.Children.OfType<<span style="color: #2b91af;">DummyFileSystemObjectInfo</span>>().ToList();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (list.Count > 0) <span style="color: blue;">return</span>
list.First();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">void</span>
RemoveDummy()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Children.Remove(<span style="color: blue;">this</span>.GetDummy());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">void</span>
ExploreDirectories()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.Drive, <span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">this</span>.Drive.IsReady)
<span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.FileSystemInfo
<span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">var</span> directories = ((<span style="color: #2b91af;">DirectoryInfo</span>)<span style="color: blue;">this</span>.FileSystemInfo).GetDirectories();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">foreach</span> (<span style="color: blue;">var</span>
directory <span style="color: blue;">in</span> directories.OrderBy(d =>
d.Name))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.Equals((directory.Attributes
& <span style="color: #2b91af;">FileAttributes</span>.System), <span style="color: #2b91af;">FileAttributes</span>.System) &&<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> !<span style="color: blue;">object</span>.Equals((directory.Attributes & <span style="color: #2b91af;">FileAttributes</span>.Hidden), <span style="color: #2b91af;">FileAttributes</span>.Hidden))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Children.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">FileSystemObjectInfo</span>(directory));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">/*throw;*/</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">void</span>
ExploreFiles()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.Drive, <span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">this</span>.Drive.IsReady)
<span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.FileSystemInfo
<span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">var</span> files = ((<span style="color: #2b91af;">DirectoryInfo</span>)<span style="color: blue;">this</span>.FileSystemInfo).GetFiles();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">foreach</span> (<span style="color: blue;">var</span>
file <span style="color: blue;">in</span> files.OrderBy(d => d.Name))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.Equals((file.Attributes
& <span style="color: #2b91af;">FileAttributes</span>.System), <span style="color: #2b91af;">FileAttributes</span>.System) && <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> !<span style="color: blue;">object</span>.Equals((file.Attributes & <span style="color: #2b91af;">FileAttributes</span>.Hidden), <span style="color: #2b91af;">FileAttributes</span>.Hidden))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Children.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">FileSystemObjectInfo</span>(file));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">/*throw;*/</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> FileSystemObjectInfo_PropertyChanged(<span style="color: blue;">object</span> sender, System.ComponentModel.<span style="color: #2b91af;">PropertyChangedEventArgs</span> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.FileSystemInfo
<span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.Equals(e.PropertyName,
<span style="color: #a31515;">"IsExpanded"</span>, <span style="color: #2b91af;">StringComparison</span>.CurrentCultureIgnoreCase))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (<span style="color: blue;">this</span>.IsExpanded)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.ImageSource = Shell.<span style="color: #2b91af;">FolderManager</span>.GetImageSource(<span style="color: blue;">this</span>.FileSystemInfo.FullName, <span style="color: #2b91af;">ItemState</span>.Open);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (<span style="color: blue;">this</span>.HasDummy())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.RemoveDummy();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.ExploreDirectories();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.ExploreFiles();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.ImageSource = Shell.<span style="color: #2b91af;">FolderManager</span>.GetImageSource(<span style="color: blue;">this</span>.FileSystemInfo.FullName, <span style="color: #2b91af;">ItemState</span>.Close);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">DummyFileSystemObjectInfo</span> : <span style="color: #2b91af;">FileSystemObjectInfo</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> DummyFileSystemObjectInfo()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> :
<span style="color: blue;">base</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">"DummyFileSystemObjectInfo"</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now, let's go the </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">WPF</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> stuff bindings.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">We need to create a new Window object in our solution and add a new TreeView inside it. See below the code.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Class</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="CodesDirectory.WIN_TreeViewWithIcon"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">classes</span><span style="color: blue;">="clr-namespace:CodesDirectory.Classes"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Title</span><span style="color: blue;">="WIN TreeView with System Icons"</span><span style="color: red;"> Height</span><span style="color: blue;">="300"</span><span style="color: red;"> Width</span><span style="color: blue;">="300"></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="treeView"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="5"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">We also need to override the default style of the items. In this case the </span><span style="font-family: Courier New, Courier, monospace;">ItemContainerStyle</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> value should be modified. But unlike with other </span><span style="font-family: Courier New, Courier, monospace;">Style</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> we only bind the </span><span style="font-family: Courier New, Courier, monospace;">IsExpanded</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property of the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> into the </span><span style="font-family: Courier New, Courier, monospace;">IsExpanded</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property of </span><span style="font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> class (two-way direction). So every user action in the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> state will also be applied in the bound objects. See below our new code.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Class</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="CodesDirectory.WIN_TreeViewWithIcon"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">classes</span><span style="color: blue;">="clr-namespace:CodesDirectory.Classes"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Title</span><span style="color: blue;">="WIN TreeView with System Icons"</span><span style="color: red;"> Height</span><span style="color: blue;">="300"</span><span style="color: red;"> Width</span><span style="color: blue;">="300"></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="treeView"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="5"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView.ItemContainerStyle</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Style</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> TargetType</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="{</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Type</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> TreeViewItem</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">}"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Setter</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Property</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="IsExpanded"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Value</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="{</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Binding</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> IsExpanded</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">,</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Mode</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=TwoWay}" /></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Setter</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Property</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="KeyboardNavigation.AcceptsReturn"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Value</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="True" /></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Style</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView.ItemContainerStyle</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">After that in the </span><span style="font-family: Courier New, Courier, monospace;">Resources</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property of the </span><span style="font-family: Courier New, Courier, monospace;">TreeView</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> object. We have to use the </span><span style="font-family: Courier New, Courier, monospace;">HierarchicalDataTemplate</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> object and targets the actual </span><span style="font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> class in the </span><span style="font-family: Courier New, Courier, monospace;">DataType</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property. With the use of this object, we can set the actual template that the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> has participated. In our case, we need to create a template where there is an </span><span style="font-family: Courier New, Courier, monospace;">Image</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> in the left and </span><span style="font-family: Courier New, Courier, monospace;">Label</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> in the right. The image will do display the actual icon of the file system, it binds the </span><span style="font-family: Courier New, Courier, monospace;">ImageSource</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property of the </span><span style="font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> class, and the </span><span style="font-family: Courier New, Courier, monospace;">Label</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> will be bind in the </span><span style="font-family: Courier New, Courier, monospace;">Name</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property of the </span><span style="font-family: Courier New, Courier, monospace;">FileSystemInfo</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> (of type </span><span style="font-family: Courier New, Courier, monospace;">System.IO.FileSystemInfo</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">) property of the </span><span style="font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> class. See below our actual codes now.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Class</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="CodesDirectory.WIN_TreeViewWithIcon"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">classes</span><span style="color: blue;">="clr-namespace:CodesDirectory.Classes"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Title</span><span style="color: blue;">="WIN TreeView with System Icons"</span><span style="color: red;"> Height</span><span style="color: blue;">="300"</span><span style="color: red;"> Width</span><span style="color: blue;">="300"></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="treeView"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="5"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView.ItemContainerStyle</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Style</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> TargetType</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="{</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Type</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> TreeViewItem</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">}"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Setter</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Property</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="IsExpanded"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Value</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="{</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Binding</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> IsExpanded</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">,</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Mode</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=TwoWay}" /></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Setter</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Property</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="KeyboardNavigation.AcceptsReturn"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Value</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="True" /></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Style</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView.ItemContainerStyle</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView.Resources</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">HierarchicalDataTemplate</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> DataType</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="{</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Type</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> classes</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">FileSystemObjectInfo</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">}"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> ItemsSource</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="{</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Binding</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Path</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=Children}"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Orientation</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="Horizontal"></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Image</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Source</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="{</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Binding</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Path</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=ImageSource,</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> UpdateSourceTrigger</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=PropertyChanged}"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="0,1,8,1"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Image</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TextBlock</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Text</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="{</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Binding</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Path</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">=FileSystemInfo.Name}"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TextBlock</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">StackPanel</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">HierarchicalDataTemplate</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView.Resources</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Lastly, in the code behind, we need to explore the top level drives and add each drive in the </span><span style="font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> wrapper class and append it to the </span><span style="font-family: Courier New, Courier, monospace;">TreeView.Items</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> property. See below the codes on how to do it.</span><br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">WIN_TreeViewWithIcon</span>
: <span style="color: #2b91af;">Window</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> WIN_TreeViewWithIcon()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
InitializeComponent();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> drives = <span style="color: #2b91af;">DriveInfo</span>.GetDrives();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> drive <span style="color: blue;">in</span> drives)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.treeView.Items.Add(<span style="color: blue;">new</span>
<span style="color: #2b91af;">FileSystemObjectInfo</span>(drive));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And, congratulations to you for finishing this blog. You are now equipped with a new interesting programming technique called Shell.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please follow us and be part of this blog site for the more very interesting stuff.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com8tag:blogger.com,1999:blog-2161576857104408209.post-87152447175574990222013-01-25T20:15:00.003-08:002013-01-29T01:55:24.296-08:00PART 1: C# WPF TreeView File Explorer with System Icons<b style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;"><span style="font-size: large;">How to make a file system explorer with system icons using WPF TreeView control?</span></b><br />
<span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;"><br /></span><span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;">Actually, this blog is one of the most exciting topic we have as of now. The purpose of this blog is to guide you how to implement a file system explorer control using </span><span style="background-color: white; color: #222222; line-height: 25px;"><span style="font-family: Courier New, Courier, monospace;">C# WPF TreeView</span></span><span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;"> control that displays the file system icons and its other file system information. </span><span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;">See the image below the target output of this blog.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEEfqbkFwtQA4lJIUeLjmFtsPEw8qP95gY6tIzrMBFdeXyBp9ZYwYNtMq1x38Y5-ek9FSj4nxrnYd4iZlxNxNyKlM84PrJbAoogDtCAT0Oxilu3FIwXKN07SuK4wUV1kq-MUUzxLiKvzk/s1600/TreeViewWithSystemIcons.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjEEfqbkFwtQA4lJIUeLjmFtsPEw8qP95gY6tIzrMBFdeXyBp9ZYwYNtMq1x38Y5-ek9FSj4nxrnYd4iZlxNxNyKlM84PrJbAoogDtCAT0Oxilu3FIwXKN07SuK4wUV1kq-MUUzxLiKvzk/s1600/TreeViewWithSystemIcons.PNG" /></a></div>
<span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;"><br /></span>
<span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;">In order for you to understand further how did we address the implementation, we are requiring you to visit the list of blogs below. They are the pre-requisite blogs and will guide you how did we re-use the existing implementation. </span><span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;">We are looking forward that you will not neglect them </span><span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;">as it will guide you to fully understand how the process and flow of the source code is happening.</span><br />
<span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;"><br /></span>
<span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;">Below are the list pre-requisite blogs you need to read.</span><br />
<ul>
<li><a href="http://codesdirectory.blogspot.com/2013/01/displaying-system-icon-in-c-wpf.html" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;" target="_blank">Displaying System Icon in C#, WPF</a></li>
<li><a href="http://codesdirectory.blogspot.com/2013/01/implementing-flexible-baseobject.html" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;" target="_blank">Implementing a Flexible Base Object</a></li>
</ul>
<span style="color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now that you're finish with the list of topics above, then you're ready to go.</span><br />
<div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Creating a Wrapper Class</b></span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">First of all, in </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">C#</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> file system information, the class </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is the base class of the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">DirectoryInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class. For us to create a dynamic wrapper of both </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">DirectoryInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we should somehow polymorph the implementation by creating a wrapper class for the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object. Let us call this class </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. See below the wrapper class for </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">FileSystemObjectInfo</span> : <span style="color: #2b91af;">BaseObject</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> FileSystemObjectInfo(<span style="color: #2b91af;">FileSystemInfo</span>
info)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> FileSystemObjectInfo(<span style="color: #2b91af;">DriveInfo</span>
drive)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> : <span style="color: blue;">this</span>(drive.RootDirectory)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please note that we have inherited the functionality of the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">BaseObject</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class we have discussed from the </span><a href="http://codesdirectory.blogspot.com/2013/01/implementing-flexible-baseobject.html" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;" target="_blank">Implementing a Flexible Base Object</a><span style="color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now that we have this wrapper class, we also need to create an additional dummy class for this object. Let us call this dummy class </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">DummyFileSystemObjectInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">DummyFileSystemObjectInfo</span> : <span style="color: #2b91af;">FileSystemObjectInfo</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> DummyFileSystemObjectInfo()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> : <span style="color: blue;">base</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">DirectoryInfo</span>(<span style="color: #a31515;">"DummyFileSystemObjectInfo"</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The purpose of this dummy is just a dummy item for the Children property of our wrapper class. This dummy item will be removed once the user interacts on the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> node </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">Expanded</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> item.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now we have to create the important properties that we will be using in this class. See below each property needed.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">ObservableCollection</span><<span style="color: #2b91af;">FileSystemObjectInfo</span>> Children<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">ObservableCollection</span><<span style="color: #2b91af;">FileSystemObjectInfo</span>>>(<span style="color: #a31515;">"Children"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"Children"</span>,
<span style="color: blue;">value</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">ImageSource</span>
ImageSource<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">ImageSource</span>>(<span style="color: #a31515;">"ImageSource"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"ImageSource"</span>,
<span style="color: blue;">value</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">bool</span> IsExpanded<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: blue;">bool</span>>(<span style="color: #a31515;">"IsExpanded"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"IsExpanded"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">FileSystemInfo</span>
FileSystemInfo<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">FileSystemInfo</span>>(<span style="color: #a31515;">"FileSystemInfo"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"FileSystemInfo"</span>,
<span style="color: blue;">value</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">DriveInfo</span> Drive<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">DriveInfo</span>>(<span style="color: #a31515;">"Drive"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"Drive"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">All properties above is very useful on our implementation. See below their used.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222;"><b><span style="font-family: Courier New, Courier, monospace;">Children</span></b><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: a collection of </span></span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> in which it will handle all the actual children of the actual directory of the file system.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Courier New, Courier, monospace;"><b>ImageSource</b></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: an image property corresponds to the current folder or file system icon.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Courier New, Courier, monospace;"><b>IsExpanded</b></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: this property participates in WPF binding. This is used to determine the current </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">TreeView</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> node state whether it is expanded or collapsed.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Courier New, Courier, monospace;"><b>FileSystemInfo</b></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: this property will handle the current bound object such as </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">DirectoryInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. In this property we have polymorph(ed) both </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">System.IO</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> classes.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Courier New, Courier, monospace;"><b>Drive</b></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: this property will handle the current drive info if we are exploring the system drives.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Furth</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">ermore, we have to create the important methods to do the actual file system exploration. Note that this wrapper class will be used by our binding in </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">WPF TreeView</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> control to display the content inside it.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> AddDummy()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Children.Add(<span style="color: blue;">new</span>
<span style="color: #2b91af;">DummyFileSystemObjectInfo</span>());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">bool</span> HasDummy()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> !<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.GetDummy(), <span style="color: blue;">null</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">DummyFileSystemObjectInfo</span>
GetDummy()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> list = <span style="color: blue;">this</span>.Children.OfType<<span style="color: #2b91af;">DummyFileSystemObjectInfo</span>>().ToList();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (list.Count > 0) <span style="color: blue;">return</span>
list.First();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> RemoveDummy()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Children.Remove(<span style="color: blue;">this</span>.GetDummy());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You will see four methods above that is related to a dummy object manipulation. See below our used on each method.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Courier New, Courier, monospace;"><b>AddDummy</b></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: adds a dummy object in the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">Children</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property of the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Courier New, Courier, monospace;"><b>HasDummy</b></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: checks whether there is dummy object in the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">Children</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property of the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Courier New, Courier, monospace;"><b>GetDummy</b></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: returns the current added </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">DummyFileSystemObjectInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Courier New, Courier, monospace;"><b>RemoveDummy</b></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: removes the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">DummyFileSystemObjectInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object that was already added in the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">Children</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">For your information, our class </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemObjectInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is participating the event notification by inheriting in the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">BaseObject</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class from our previous blog. With this, we have to listen in the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">PropertyChanged</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event of the class so we know in what property will we load the child folders and files of the current file system object. In the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">constructor</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we need to add the listener and implement the event handler method. See below our codes.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In the constructor.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> FileSystemObjectInfo(<span style="color: #2b91af;">FileSystemInfo</span>
info)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.PropertyChanged += <span style="color: blue;">new</span>
System.ComponentModel.<span style="color: #2b91af;">PropertyChangedEventHandler</span>(FileSystemObjectInfo_PropertyChanged);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The event handler of </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">PropertyChanged</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> FileSystemObjectInfo_PropertyChanged(<span style="color: blue;">object</span> sender, System.ComponentModel.<span style="color: #2b91af;">PropertyChangedEventArgs</span> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.FileSystemInfo
<span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.Equals(e.PropertyName,
<span style="color: #a31515;">"IsExpanded"</span>, <span style="color: #2b91af;">StringComparison</span>.CurrentCultureIgnoreCase))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.IsExpanded)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.ImageSource = <span style="color: #2b91af;">FolderManager</span>.GetImageSource(<span style="color: blue;">this</span>.FileSystemInfo.FullName, <span style="color: #2b91af;">ItemState</span>.Open);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (<span style="color: blue;">this</span>.HasDummy())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.RemoveDummy();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.ExploreDirectories();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.ExploreFiles();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.ImageSource = <span style="color: #2b91af;">FolderManager</span>.GetImageSource(<span style="color: blue;">this</span>.FileSystemInfo.FullName, <span style="color: #2b91af;">ItemState</span>.Close);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin;">}</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You will notice two new methods named </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">ExploreDirectories</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">ExploreFiles</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. This two methods is very important when exploring the child folders and files of the file system. See below our implementation for both methods.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span>
ExploreDirectories()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.Drive, <span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">this</span>.Drive.IsReady)
<span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.FileSystemInfo
<span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> directories = ((<span style="color: #2b91af;">DirectoryInfo</span>)<span style="color: blue;">this</span>.FileSystemInfo).GetDirectories();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span>
directory <span style="color: blue;">in</span> directories.OrderBy(d =>
d.Name))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.Equals((directory.Attributes
& <span style="color: #2b91af;">FileAttributes</span>.System), <span style="color: #2b91af;">FileAttributes</span>.System) &&<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
!<span style="color: blue;">object</span>.Equals((directory.Attributes
& <span style="color: #2b91af;">FileAttributes</span>.Hidden), <span style="color: #2b91af;">FileAttributes</span>.Hidden))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.Children.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">FileSystemObjectInfo</span>(directory));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">/*throw;*/</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span>
ExploreFiles()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.Drive, <span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">this</span>.Drive.IsReady)
<span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.FileSystemInfo
<span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> files = ((<span style="color: #2b91af;">DirectoryInfo</span>)<span style="color: blue;">this</span>.FileSystemInfo).GetFiles();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> file <span style="color: blue;">in</span> files.OrderBy(d => d.Name))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.Equals((file.Attributes
& <span style="color: #2b91af;">FileAttributes</span>.System), <span style="color: #2b91af;">FileAttributes</span>.System) && <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
!<span style="color: blue;">object</span>.Equals((file.Attributes & <span style="color: #2b91af;">FileAttributes</span>.Hidden), <span style="color: #2b91af;">FileAttributes</span>.Hidden))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.Children.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">FileSystemObjectInfo</span>(file));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">/*throw;*/</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, we have nothing left to explain on the content of this class. All we have to do is to loading the actual image and some initialization in the constructor. See below our new constructor implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> FileSystemObjectInfo(<span style="color: #2b91af;">FileSystemInfo</span>
info)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span> <span style="color: blue;">is</span> <span style="color: #2b91af;">DummyFileSystemObjectInfo</span>)
<span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Children = <span style="color: blue;">new</span> <span style="color: #2b91af;">ObservableCollection</span><<span style="color: #2b91af;">FileSystemObjectInfo</span>>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.FileSystemInfo = info;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (info <span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.ImageSource = <span style="color: #2b91af;">FolderManager</span>.GetImageSource(info.FullName, <span style="color: #2b91af;">ItemState</span>.Close);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.AddDummy();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span> (info <span style="color: blue;">is</span> <span style="color: #2b91af;">FileInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.ImageSource = <span style="color: #2b91af;">FileManager</span>.GetImageSource(info.FullName);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.PropertyChanged += <span style="color: blue;">new</span>
System.ComponentModel.<span style="color: #2b91af;">PropertyChangedEventHandler</span>(FileSystemObjectInfo_PropertyChanged);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">So what the code inside the constructor is doing? First, we initialized the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">Children</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property and then set the handled </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property into the argument of our constructor. After that, we checked whether the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileSystemInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is a </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">DirectoryInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FileInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. If it is a </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">DriveInfo</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object we have to load the actual folder image by using the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">FolderManager</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> discussed in the </span><a href="http://codesdirectory.blogspot.com/2013/01/displaying-system-icon-in-c-wpf.html" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif; line-height: 25px;" target="_blank">Displaying System Icon in C#, WPF</a>.<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> However, if it is a </span><span style="font-family: Courier New, Courier, monospace;">FileInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object, then we will use the </span><span style="font-family: Courier New, Courier, monospace;">FileManager</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> to load the actual file image.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And now we are complete with the wrapper implementation.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please visit the <a href="http://codesdirectory.blogspot.com/2013/01/part-2-c-wpf-treeview-file-explorer.html">PART 2: C# WPF TreeView File Explorer with System Icons</a> for the continuation.</span></div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com12tag:blogger.com,1999:blog-2161576857104408209.post-60461190452559773972013-01-25T18:46:00.002-08:002013-01-29T01:55:24.276-08:00Implementing a flexible BaseObject<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This blog is one of the exciting topic because this will give us a very common flexible base object class that we can inherit in every class of our application.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">But before we continue, we are requiring you to read the following blog.</span><br />
<ul>
<li><a href="http://codesdirectory.blogspot.com/2013/01/c-inotifypropertychanged-interface.html" target="_blank"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">C# </span><span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">INotifyPropertyChanged Interface</span></a></li>
<li><span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><a href="http://codesdirectory.blogspot.com/2013/01/c-inotifypropertychanged-base-class.html" target="_blank">C# INotifyPropertyChanged Base Class Implementation</a></span></li>
</ul>
<div>
<span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Once you're done in the topics above, then you're ready to go in this topic.</span></div>
<div>
<span style="background-color: white; color: #222222; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">First we need to create a class named </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">BaseObject</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> that inherits the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">PropertyNotifier</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> we created in the previous blog. See below.</span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">Serializable</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BaseObject</span> :
<span style="color: #2b91af;">PropertyNotifier</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> BaseObject()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
</div>
<div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Then we need to create a dictionary object that holds the value for our properties. This dictionary object will also be the variable that we will be using in the entire class manipulation. Let's name this dictionary object as </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">__values</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Note that we need to pass the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">StringComparer.CurrentCultureIgnoreCase</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> value during the construction as it will be eliminated the case sensitive checking the dictionary keys. See below the codes.</span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">IDictionary</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">, </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">object</span><span style="font-family: Consolas; font-size: 9.5pt;">>
__values = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Dictionary</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">, </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">object</span><span style="font-family: Consolas; font-size: 9.5pt;">>(</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">StringComparer</span><span style="font-family: Consolas; font-size: 9.5pt;">.CurrentCultureIgnoreCase);</span></div>
</div>
<div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After that we need to create a method that will set/get the property values on this class. In our case to make it very common in the purpose, let us name it </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">GetValue</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">SetValue</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. This method uses the dictionary object we had declared above to save the values in the memory. For the generic way implementation, we need to create a type-safe function of the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">GetValue</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> so it is easy for the derive class to use it. See below the codes.</span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> T GetValue<T>(<span style="color: blue;">string</span>
key)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.IsNullOrEmpty(key))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">default</span>(T);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> value = <span style="color: blue;">this</span>.GetValue(key);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (value <span style="color: blue;">is</span> T)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> (T)value;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">default</span>(T);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">object</span> GetValue(<span style="color: blue;">string</span> key)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.IsNullOrEmpty(key))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.__values.ContainsKey(key))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">this</span>.__values[key];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> SetValue(<span style="color: blue;">string</span> key, <span style="color: blue;">object</span>
value)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">this</span>.__values.ContainsKey(key))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__values.Add(key, value);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__values[key] = value;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">base</span>.OnPropertyChanged(key);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now base on the code above as you analyze it, the very common purpose of it is to save/get the value from the dictionary. Also notice that on the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">SetValue</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method, we have called the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">OnPropertyChanged</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event of the base class </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">PropertyNotifier</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. The purpose of this is to notify the listener of the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">PropertyChanged</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event on the current property.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now that's it. That is just the dynamic and flexible object that we can use on our entire application. See the codes below the actual class implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BaseObject</span> :
<span style="color: #2b91af;">PropertyNotifier</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
Privates<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: #2b91af;">IDictionary</span><<span style="color: blue;">string</span>, <span style="color: blue;">object</span>>
__values = <span style="color: blue;">new</span> <span style="color: #2b91af;">Dictionary</span><<span style="color: blue;">string</span>, <span style="color: blue;">object</span>>(<span style="color: #2b91af;">StringComparer</span>.CurrentCultureIgnoreCase);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
Methods<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> T GetValue<T>(<span style="color: blue;">string</span>
key)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.IsNullOrEmpty(key))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">default</span>(T);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> value = <span style="color: blue;">this</span>.GetValue(key);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (value <span style="color: blue;">is</span> T)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> (T)value;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">default</span>(T);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">object</span>
GetValue(<span style="color: blue;">string</span> key)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.IsNullOrEmpty(key))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.__values.ContainsKey(key))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">this</span>.__values[key];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">void</span> SetValue(<span style="color: blue;">string</span> key, <span style="color: blue;">object</span>
value)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">this</span>.__values.ContainsKey(key))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__values.Add(key, value);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__values[key] = value;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">base</span>.OnPropertyChanged(key);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, see the code below how did we use the newly created </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">BaseObject</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class in our derived class.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Person</span> : <span style="color: #2b91af;">BaseObject</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> Person(<span style="color: blue;">string</span>
name, <span style="color: #2b91af;">DateTime</span> birthDate)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.BirthDate = birthDate;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: blue;">string</span>>(<span style="color: #a31515;">"Name"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"Name"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">DateTime</span>
BirthDate<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span> { <span style="color: blue;">return</span> <span style="color: blue;">base</span>.GetValue<<span style="color: #2b91af;">DateTime</span>>(<span style="color: #a31515;">"BirthDate"</span>); }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span> { <span style="color: blue;">base</span>.SetValue(<span style="color: #a31515;">"BirthDate"</span>, <span style="color: blue;">value</span>);
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Then, after creating an object of type </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">Person</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, you will also notice that you can listen to the </span><span style="color: #222222; font-family: Courier New, Courier, monospace;">PropertyChanged</span><span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event of the object. See the codes belw.</span><br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> App()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> person = <span style="color: blue;">new</span> <span style="color: #2b91af;">Person</span>(<span style="color: #a31515;">"Stanley
Harris"</span>, <span style="color: blue;">new</span> <span style="color: #2b91af;">DateTime</span>(1978, 3, 22));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
person.PropertyChanged += <span style="color: blue;">new</span>
System.ComponentModel.<span style="color: #2b91af;">PropertyChangedEventHandler</span>(person_PropertyChanged);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> person_PropertyChanged(<span style="color: blue;">object</span>
sender, System.ComponentModel.<span style="color: #2b91af;">PropertyChangedEventArgs</span>
e)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">/* do more stuff here */</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Pretty interesting right? There are more interesting stuffs coming.</span><br />
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="color: #222222; font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">I hope you guys really understand how did we do the stuff above as this is very important and a pre-requisites on the next more exciting blogs on this site.</span></div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-44607590027707288682013-01-24T03:04:00.001-08:002013-01-29T01:55:24.234-08:00C# INotifyPropertyChanged Base Class Implementation<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This topic is relative to <a href="http://codesdirectory.blogspot.com/2013/01/c-inotifypropertychanged-interface.html" target="_blank">C# INotifyProperyChanged Interface</a> topic. So better you visit it first before proceeding in this topic.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The purpose of this topic is to create an inheritable </span><span style="font-family: Courier New, Courier, monospace;">PropertyNotifier</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class that can be used by any derive class of your application. You have to realize that putting base object in one file is good and that is the basic concept of object oriented programming. In this blog, we are also introducing the power of Inheritance part of the object oriented programming.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The class that we have in the previous topic </span><a href="http://codesdirectory.blogspot.com/2013/01/c-inotifypropertychanged-interface.html" style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;" target="_blank">C# INotifyProperyChanged Interface</a> <span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">is below. All we have to do is to make a perfect spice, adjust some code so it will be usable by the derived class.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">Serializable</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PropertyNotifier</span>
: <span style="color: #2b91af;">INotifyPropertyChanged</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> [<span style="color: blue;">field</span>: <span style="color: #2b91af;">NonSerialized</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">event</span> <span style="color: #2b91af;">PropertyChangedEventHandler</span> PropertyChanged;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> PropertyNotifier()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> : <span style="color: blue;">base</span>()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">string</span>
__myProperty = <span style="color: blue;">string</span>.Empty;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span>
MyProperty<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">this</span>.__myProperty;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__myProperty = <span style="color: blue;">value</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.OnPropertyChanged(<span style="color: #a31515;">"MyProperty"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
INotifyPropertyChanged Members<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">protected</span> <span style="color: blue;">void</span>
OnPropertyChanged(<span style="color: blue;">string</span> propertyName)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.PropertyChanged, <span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.PropertyChanged(<span style="color: blue;">this</span>,
<span style="color: blue;">new</span> <span style="color: #2b91af;">PropertyChangedEventArgs</span>(propertyName));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The first thing is to remove the </span><span style="font-family: Courier New, Courier, monospace;">MyProperty</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property as it is not needed anymore. Then we need to add an additional property named </span><span style="font-family: Courier New, Courier, monospace;">AllowRaiseEvent</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. This property should not call the </span><span style="font-family: Courier New, Courier, monospace;">OnPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event as it is not notifying the derive class in every property value change, instead this property will be used to determine whether we are allowed to raise the event or not. Declare the accessor as </span><span style="font-family: Courier New, Courier, monospace;">protected</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> so only derived class can change the value.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Then in the </span><span style="font-family: Courier New, Courier, monospace;">OnPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event handler, we have to check whether the </span><span style="font-family: Courier New, Courier, monospace;">AllowRaiseEvent</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property is true or false. If it is true then we should call the </span><span style="font-family: Courier New, Courier, monospace;">PropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event, otherwise not.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Lastly, we have to set the </span><span style="font-family: Courier New, Courier, monospace;">AllowRaiseEvent</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property value to true in the constructor so the default will be allow raising of event. See our new class structure below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">Serializable</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PropertyNotifier</span>
: <span style="color: #2b91af;">INotifyPropertyChanged</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;"> Events<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> [<span style="color: blue;">field</span>: <span style="color: #2b91af;">NonSerialized</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">event</span> <span style="color: #2b91af;">PropertyChangedEventHandler</span> PropertyChanged;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> PropertyNotifier()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> : <span style="color: blue;">base</span>()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.AllowRaiseEvent = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
Properties<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> [<span style="color: #2b91af;">XmlIgnore</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">protected</span> <span style="color: blue;">bool</span>
AllowRaiseEvent<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
INotifyPropertyChanged Members<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">protected</span> <span style="color: blue;">void</span>
OnPropertyChanged(<span style="color: blue;">string</span> propertyName)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.AllowRaiseEvent)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.PropertyChanged, <span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.PropertyChanged(<span style="color: blue;">this</span>, <span style="color: blue;">new</span> <span style="color: #2b91af;">PropertyChangedEventArgs</span>(propertyName));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now deriving from this class is flexible. You can set the </span><span style="font-family: Courier New, Courier, monospace;">AllowRaiseEvent</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property whether you want to notify property value change or not. Additionally, you can call the </span><span style="font-family: Courier New, Courier, monospace;">OnPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method to notify the property of value change. See below the class that inherit the </span><span style="font-family: Courier New, Courier, monospace;">PropertyNotifier</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BaseObject</span> :
<span style="color: #2b91af;">PropertyNotifier</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> BaseObject()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">base</span>.AllowRaiseEvent = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">string</span>
__myProperty = <span style="color: blue;">string</span>.Empty;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span>
MyProperty<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">this</span>.__myProperty;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__myProperty = <span style="color: blue;">value</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">base</span>.OnPropertyChanged(<span style="color: #a31515;">"MyProperty"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You will notice that we atleast have a little more flexible base notifier object.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now we're done with the </span><span style="font-family: Courier New, Courier, monospace;">INotifyPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> interface topic. Next topic would be <a href="http://codesdirectory.blogspot.com/2013/01/implementing-flexible-baseobject.html" target="_blank">implementing a flexible </a></span><span style="font-family: Courier New, Courier, monospace;"><a href="http://codesdirectory.blogspot.com/2013/01/implementing-flexible-baseobject.html" target="_blank">BaseObject</a></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-3073695263983543332013-01-24T02:46:00.001-08:002013-01-29T01:55:24.070-08:00C# INotifyPropertyChanged Interface<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>How to implement INotifyPropertyChanged interface in C#?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><br /></b></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">INotifyPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> interface is used and declared in the class level so all properties participated within the class can notify if the value has been changed. In </span><span style="font-family: Courier New, Courier, monospace;">WPF</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, this interface is very useful because most of the part of the </span><span style="font-family: Courier New, Courier, monospace;">XAML</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> participated in the binding. In binding, it is important to notify the changes in the listener so the listener could update itself or even the UI.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please visit Microsoft <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" target="_blank">documentation</a> for further details.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Below is an example code how to implement the </span><span style="font-family: Courier New, Courier, monospace;">INotifyPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> within the class. Let's call our class </span><span style="font-family: Courier New, Courier, monospace;">PropertyNotifier</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">Serializable</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">PropertyNotifier</span>
: <span style="color: #2b91af;">INotifyPropertyChanged</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> PropertyNotifier()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> : <span style="color: blue;">base</span>()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now after you implemented the </span><span style="font-family: Courier New, Courier, monospace;">INotifyPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> </span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">in your class, you're are required to explicitly/implicitly implement the members of that interface. In </span><span style="font-family: Courier New, Courier, monospace;">INotifyPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> interface has</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> one valuable member. It is an event called </span><span style="font-family: Courier New, Courier, monospace;">PropertyChanged</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">. This event is being called inside the class every time the value of the property has been changed. See below how we implemented it.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: blue;">field</span>: <span style="color: #2b91af;">NonSerialized</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">event</span> <span style="color: #2b91af;">PropertyChangedEventHandler</span> PropertyChanged;</span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">After that, we need to create an additional method to be the bridge of calling this event. The most common name for that method is </span><span style="font-family: Courier New, Courier, monospace;">OnPropertyChanged</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> as a standard naming convention of Microsoft. This method usually calls by each of the property of the class most likely in the </span><span style="font-family: Courier New, Courier, monospace;">set</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> accessor and the one who actually call the </span><span style="font-family: Courier New, Courier, monospace;">PropertyChanged</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> event. See below our implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">protected</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span>
OnPropertyChanged(<span style="color: blue;">string</span> propertyName)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.PropertyChanged, <span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.PropertyChanged(<span style="color: blue;">this</span>,
<span style="color: blue;">new</span> <span style="color: #2b91af;">PropertyChangedEventArgs</span>(propertyName));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Now if you're creating your own property within the class, you have to call the above's method and pass the actual literal property name on the method. See below our implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">string</span>
__myProperty = <span style="color: blue;">string</span>.Empty;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">string</span> MyProperty<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">this</span>.__myProperty;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__myProperty = <span style="color: blue;">value</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.OnPropertyChanged(<span style="color: #a31515;">"MyProperty"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You'll see that we created a new property named </span><span style="font-family: Courier New, Courier, monospace;">MyProperty</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. You will notice that in the set accessor, we called the </span><span style="font-family: Courier New, Courier, monospace;">OnPropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property just to notify the class that there is a value changed in the </span><span style="font-family: Courier New, Courier, monospace;">MyProperty</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">For the external library who are listening on the </span><span style="font-family: Courier New, Courier, monospace;">PropertyChanged</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event of the </span><span style="font-family: Courier New, Courier, monospace;">PropertyNotifier</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class, it will then automatically notify the value change.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Next topic is the <a href="http://codesdirectory.blogspot.com/2013/01/c-inotifypropertychanged-base-class.html" target="_blank">inheritable </a></span><span style="font-family: Courier New, Courier, monospace;"><a href="http://codesdirectory.blogspot.com/2013/01/c-inotifypropertychanged-base-class.html" target="_blank">PropertyNotifier</a></span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> object.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-80951051730495180242013-01-23T03:13:00.000-08:002013-01-29T01:55:24.293-08:00C# WPF TreeView File Explorer<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>How to explore file system using WPF TreeView control?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, one of the most exciting topic is </span><span style="font-family: Courier New, Courier, monospace;">Shell</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. As what we've discuss in <a href="http://codesdirectory.blogspot.com/2013/01/displaying-system-icon-in-c-wpf.html" target="_blank">Displaying System Icon in C#</a>, </span><span style="font-family: Courier New, Courier, monospace;">shelling</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is a technique of manipulating file system objects in the computer's operating system using the system </span><span style="font-family: Courier New, Courier, monospace;">APIs</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> provided. In the previous version of Microsoft programming language like </span><span style="font-family: Courier New, Courier, monospace;">VB6.0</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, the common and popular name for this is </span><span style="font-family: Courier New, Courier, monospace;">FSO (or FileSystemObject)</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. With the advent of the technology inventions, doing </span><span style="font-family: Courier New, Courier, monospace;">Shell</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is very easy compare to what we're doing with the older version of programming languages.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In this blog, we preferred to use the </span><span style="font-family: Courier New, Courier, monospace;">WPF</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> technology since we already engage in some project that already using the </span><span style="font-family: Courier New, Courier, monospace;">WPF</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. In short, we already have our environment ready for it.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">See below the image as what are our target output.</span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQhHjghxEPIC15UO04HhsspZHafn3Q4zSYrcPdkwfYVMBmd2ppbt78ls7QTdEN98wXHU1V6JwWOBK__IypylTU7-3DrC4-CYMpATN4g0D6YrEbJLMzYvQj1ZQIKS4g3_rGpSugIXpg5Ww/s1600/ExplorerTreeView.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQhHjghxEPIC15UO04HhsspZHafn3Q4zSYrcPdkwfYVMBmd2ppbt78ls7QTdEN98wXHU1V6JwWOBK__IypylTU7-3DrC4-CYMpATN4g0D6YrEbJLMzYvQj1ZQIKS4g3_rGpSugIXpg5Ww/s1600/ExplorerTreeView.PNG" /></a></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In the above Image, you can see a WPF </span><span style="font-family: Courier New, Courier, monospace;">TreeView</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> control that already filled with the </span><span style="font-family: Courier New, Courier, monospace;">File System</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> information. We just simply get the root drives and get the child directories and files of the drives in a lazy load technique. See below our actual code implementation and explanation.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>TreeView Explorer Implementation</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">First and foremost, we created a new </span><span style="font-family: Courier New, Courier, monospace;">Window</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and place a </span><span style="font-family: Courier New, Courier, monospace;">TreeView</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> control inside it. See the codes below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> x</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;">Class</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="CodesDirectory.WIN_TreeView"</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: red;"> Title</span><span style="color: blue;">="WIN TreeView"</span><span style="color: red;">
Height</span><span style="color: blue;">="300"</span><span style="color: red;"> Width</span><span style="color: blue;">="300"></span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Name</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="treeView"</span><span style="color: red; font-family: Consolas; font-size: 9.5pt;"> Margin</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">="5"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">TreeView</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"></</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">Window</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
In order for us to display the list of the available </span><span style="font-family: Courier New, Courier, monospace;">Drive</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> in the file system, we need to use the </span><span style="font-family: Courier New, Courier, monospace;">System.IO.DriveInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class to get the list of drives. The method </span><span style="font-family: Courier New, Courier, monospace;">GetDrives</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method will give us the information of the active system drives. See the code below our implementation.</span><br />
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span>
LoadDirectories()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> drives = <span style="color: #2b91af;">DriveInfo</span>.GetDrives();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> drive <span style="color: blue;">in</span> drives)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.treeView.Items.Add(<span style="color: blue;">this</span>.GetItem(drive));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In our code above, we created the method </span><span style="font-family: Courier New, Courier, monospace;">LoadDirectories</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> so it can be called during the construction of the </span><span style="font-family: Courier New, Courier, monospace;">Window</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. You will also notice that there is a method named </span><span style="font-family: Courier New, Courier, monospace;">GetItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> passing the drive information. This method will actually return the actual </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> that binds the </span><span style="font-family: Courier New, Courier, monospace;">DriveInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object into the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">'s </span><span style="font-family: Courier New, Courier, monospace;">DataContext</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Tag property</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. See below our implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">TreeViewItem</span>
GetItem(<span style="color: #2b91af;">DriveInfo</span> drive)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> item = <span style="color: blue;">new</span> <span style="color: #2b91af;">TreeViewItem</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Header = drive.Name,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
DataContext = drive,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Tag =
drive<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.AddDummy(item);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
item.Expanded += <span style="color: blue;">new</span> <span style="color: #2b91af;">RoutedEventHandler</span>(item_Expanded);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> item;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Again, the </span><span style="font-family: Courier New, Courier, monospace;">AddDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method and the </span><span style="font-family: Courier New, Courier, monospace;">item_Expanded</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method is needed in this implementation. The </span><span style="font-family: Courier New, Courier, monospace;">AddDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method is used to create a temporary dummy item inside each </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> (those bound to </span><span style="font-family: Courier New, Courier, monospace;">Directory</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Drive</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">). The purpose of this is to let the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> be collapsible (you will notice there is an arrow before the item) </span><b><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">WITHOUT</span></b><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> loading the child </span><span style="font-family: Courier New, Courier, monospace;">directories</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">files</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. In the other side, the </span><span style="font-family: Courier New, Courier, monospace;">item_Expanded</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method is just an event handler to the </span><span style="font-family: Courier New, Courier, monospace;">Expanded</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event of the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Later we'll explain how did we used this event when loading the child directories and files.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br />Additionally, in order for us to determine whether the item is dummy or not we are forced to create a new class that inherits the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. We called it </span><span style="font-family: Courier New, Courier, monospace;">DummyTreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. See the code below our implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">DummyTreeViewItem</span> : <span style="color: #2b91af;">TreeViewItem</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> DummyTreeViewItem()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> : <span style="color: blue;">base</span>()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">base</span>.Header = <span style="color: #a31515;">"Dummy"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">base</span>.Tag = <span style="color: #a31515;">"Dummy"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
Now, since we are exploring the </span><span style="font-family: Courier New, Courier, monospace;">File System Objects</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we are not just limited for a drive. We have to address also the other object like </span><span style="font-family: Courier New, Courier, monospace;">File</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Directory</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. In this case, we need to create an overloaded method of </span><span style="font-family: Courier New, Courier, monospace;">GetItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> that returns the actual </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> that binds the corresponding object. See below our implementation.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">TreeViewItem</span>
GetItem(<span style="color: #2b91af;">DirectoryInfo</span> directory)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> item = <span style="color: blue;">new</span> <span style="color: #2b91af;">TreeViewItem</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Header = directory.Name,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
DataContext = directory,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Tag =
directory<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.AddDummy(item);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
item.Expanded += <span style="color: blue;">new</span> <span style="color: #2b91af;">RoutedEventHandler</span>(item_Expanded);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> item;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">TreeViewItem</span>
GetItem(<span style="color: #2b91af;">FileInfo</span> file)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> item = <span style="color: blue;">new</span> <span style="color: #2b91af;">TreeViewItem</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Header = file.Name,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
DataContext = file,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Tag =
file<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> };<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> item;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
What you can see above is just an overloaded method of what like the </span><span style="font-family: Courier New, Courier, monospace;">GetItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method of the </span><span style="font-family: Courier New, Courier, monospace;">DriveInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. The only difference is just we used the </span><span style="font-family: Courier New, Courier, monospace;">DirectoryInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">FileInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object instead. Notice that in the </span><span style="font-family: Courier New, Courier, monospace;">FileInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> </span><span style="font-family: Courier New, Courier, monospace;">GetItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method there is no </span><span style="font-family: Courier New, Courier, monospace;">AddDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Expanded</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event listener. This is because the </span><span style="font-family: Courier New, Courier, monospace;">File</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object is not expandable and doesn't contain any children.<br /><br />Looks like very surprising, till now we're not yet written the </span><span style="font-family: Courier New, Courier, monospace;">AddDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> implementation. We just only would like to be in the proper order before doing so. The code is below is our implementation for the </span><span style="font-family: Courier New, Courier, monospace;">AddDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> AddDummy(<span style="color: #2b91af;">TreeViewItem</span> item)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
item.Items.Add(<span style="color: blue;">new</span> <span style="color: #2b91af;">DummyTreeViewItem</span>());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">bool</span> HasDummy(<span style="color: #2b91af;">TreeViewItem</span> item)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> item.HasItems &&
(item.Items.OfType<<span style="color: #2b91af;">TreeViewItem</span>>().ToList().FindAll(tvi
=> tvi <span style="color: blue;">is</span> <span style="color: #2b91af;">DummyTreeViewItem</span>).Count
> 0);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> RemoveDummy(<span style="color: #2b91af;">TreeViewItem</span> item)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> dummies = item.Items.OfType<<span style="color: #2b91af;">TreeViewItem</span>>().ToList().FindAll(tvi => tvi <span style="color: blue;">is</span> <span style="color: #2b91af;">DummyTreeViewItem</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> dummy <span style="color: blue;">in</span> dummies)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
item.Items.Remove(dummy);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
Yes, that's it. Very simple! We only added a new </span><span style="font-family: Courier New, Courier, monospace;">DummyTreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> (the class we created above). The other methods named </span><span style="font-family: Courier New, Courier, monospace;">HasDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">RemoveDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, they are usable once the item </span><span style="font-family: Courier New, Courier, monospace;">Expanded</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> will be triggered. For the explanation, the </span><span style="font-family: Courier New, Courier, monospace;">HasDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is used to check whether the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> has child </span><span style="font-family: Courier New, Courier, monospace;">DummyTreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, and the </span><span style="font-family: Courier New, Courier, monospace;">RemoveDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is to simply remove the </span><span style="font-family: Courier New, Courier, monospace;">DummyTreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> from the parent's </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> children.</span><br />
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Explore Directories and Files</b></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Exploring directories and files is very easy in </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. With the use of the </span><span style="font-family: Courier New, Courier, monospace;">DirectoryInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object from </span><span style="font-family: Courier New, Courier, monospace;">System.IO</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> namespace, we can get the list of child directories and files. Please visit Microsoft <a href="http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx" target="_blank">documentation</a> for your reference.</span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span>
ExploreDirectories(<span style="color: #2b91af;">TreeViewItem</span> item)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> directoryInfo = (<span style="color: #2b91af;">DirectoryInfo</span>)<span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (item.Tag <span style="color: blue;">is</span> <span style="color: #2b91af;">DriveInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
directoryInfo = ((<span style="color: #2b91af;">DriveInfo</span>)item.Tag).RootDirectory;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span> (item.Tag <span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
directoryInfo = (<span style="color: #2b91af;">DirectoryInfo</span>)item.Tag;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span> (item.Tag <span style="color: blue;">is</span> <span style="color: #2b91af;">FileInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
directoryInfo = ((<span style="color: #2b91af;">FileInfo</span>)item.Tag).Directory;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.ReferenceEquals(directoryInfo,
<span style="color: blue;">null</span>)) <span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span>
directory <span style="color: blue;">in</span> directoryInfo.GetDirectories())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> isHidden = (directory.Attributes & <span style="color: #2b91af;">FileAttributes</span>.Hidden) == <span style="color: #2b91af;">FileAttributes</span>.Hidden;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> isSystem = (directory.Attributes & <span style="color: #2b91af;">FileAttributes</span>.System) == <span style="color: #2b91af;">FileAttributes</span>.System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!isHidden && !isSystem)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
item.Items.Add(<span style="color: blue;">this</span>.GetItem(directory));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> ExploreFiles(<span style="color: #2b91af;">TreeViewItem</span> item)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> directoryInfo = (<span style="color: #2b91af;">DirectoryInfo</span>)<span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (item.Tag <span style="color: blue;">is</span> <span style="color: #2b91af;">DriveInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
directoryInfo = ((<span style="color: #2b91af;">DriveInfo</span>)item.Tag).RootDirectory;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span> (item.Tag <span style="color: blue;">is</span> <span style="color: #2b91af;">DirectoryInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
directoryInfo = (<span style="color: #2b91af;">DirectoryInfo</span>)item.Tag;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span> <span style="color: blue;">if</span> (item.Tag <span style="color: blue;">is</span> <span style="color: #2b91af;">FileInfo</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
directoryInfo = ((<span style="color: #2b91af;">FileInfo</span>)item.Tag).Directory;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.ReferenceEquals(directoryInfo,
<span style="color: blue;">null</span>)) <span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> file <span style="color: blue;">in</span> directoryInfo.GetFiles())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> isHidden = (file.Attributes & <span style="color: #2b91af;">FileAttributes</span>.Hidden) == <span style="color: #2b91af;">FileAttributes</span>.Hidden;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> isSystem = (file.Attributes & <span style="color: #2b91af;">FileAttributes</span>.System) == <span style="color: #2b91af;">FileAttributes</span>.System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!isHidden && !isSystem)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
item.Items.Add(<span style="color: blue;">this</span>.GetItem(file));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
The method above named </span><span style="font-family: Courier New, Courier, monospace;">ExploreDirectories</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">ExploreFiles</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is used to explore the directory's child directories and files. It accepts one argument </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> (in our case the one that was returned by </span><span style="font-family: Courier New, Courier, monospace;">GetItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method bound by </span><span style="font-family: Courier New, Courier, monospace;">File</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, </span><span style="font-family: Courier New, Courier, monospace;">Directory</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">Drive</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">), from there we start the exploration. You will also notice that in both method we used the </span><span style="font-family: Courier New, Courier, monospace;">GetFiles()</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">GetDirectories()</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method and iterate it. These two methods are very important to explore the child object of the directory.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Note:</b> The </span><span style="font-family: Courier New, Courier, monospace;"><a href="http://msdn.microsoft.com/en-us/library/4cyf24ss.aspx" target="_blank">GetFiles()</a></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;"><a href="http://msdn.microsoft.com/en-us/library/s7xk2b58.aspx" target="_blank">GetDirectories()</a></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method includes the </span><span style="font-family: Courier New, Courier, monospace;">System</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">Hidden</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> objects in their result. So we must be aware of that before displaying it in the UI.<br /><br />Also, you will notice that we used the </span><span style="font-family: Courier New, Courier, monospace;">Bitwise</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> comparison for the object </span><span style="font-family: Courier New, Courier, monospace;">Attributes</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. We first determined whether it is a </span><span style="font-family: Courier New, Courier, monospace;">System</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or a </span><span style="font-family: Courier New, Courier, monospace;">Hidden</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object, and if it is not we add it into the parent's </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> </span><span style="font-family: Courier New, Courier, monospace;">Items</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property.<br /><br /><b>Expanded Event</b></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now that we're almost done with our topic. One important thing we need to do is to manage how are we going to load the children of the file system object. We need to determine in what event and what our code is doing. In the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem.Expanded</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event, as you remember, we added the </span><span style="font-family: Courier New, Courier, monospace;">item_Expanded</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> event handler and there we did our job. We first checked whether the current item has a </span><span style="font-family: Courier New, Courier, monospace;">Dummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> item and remove it. After that, we loaded the child directories and files with the methods we implemented above. See below our codes.</span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> item_Expanded(<span style="color: blue;">object</span>
sender, <span style="color: #2b91af;">RoutedEventArgs</span> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> item = (<span style="color: #2b91af;">TreeViewItem</span>)sender;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.HasDummy(item))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Cursor = <span style="color: #2b91af;">Cursors</span>.Wait;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.RemoveDummy(item);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.ExploreDirectories(item);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.ExploreFiles(item);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Cursor = <span style="color: #2b91af;">Cursors</span>.Arrow;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">
You will also notice that we only call the </span><span style="font-family: Courier New, Courier, monospace;">RemoveDummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, </span><span style="font-family: Courier New, Courier, monospace;">ExploreDirectories</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">ExploreFiles</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method only if there is a </span><span style="font-family: Courier New, Courier, monospace;">Dummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> node. This is because we used the </span><span style="font-family: Courier New, Courier, monospace;">Dummy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object as a flag to the </span><span style="font-family: Courier New, Courier, monospace;">TreeViewItem</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> state whether it is explored or not.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Lastly, we need to call the first method we created named </span><span style="font-family: Courier New, Courier, monospace;">LoadDirectories</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> in the window's constructor so everything is initialized. See below the code.</span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> WIN_TreeView()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
InitializeComponent();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.LoadDirectories();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
</div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">And that's it. We just finish the WPF </span><span style="font-family: Courier New, Courier, monospace;">TreeView</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> file system explorer topic. Next topic is about the thing of loading the file system image into the </span><span style="font-family: Courier New, Courier, monospace;">TreeView</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Related topics:</span><br />
<br />
<ul>
<li><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><a href="http://codesdirectory.blogspot.com/2013/01/part-1-c-wpf-treeview-file-explorer.html" target="_blank">PART 1: C# WPF TreeView File Explorer with System Icons</a></span></li>
<li><a href="http://codesdirectory.blogspot.com/2013/01/displaying-system-icon-in-c-wpf.html" target="_blank"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Displaying System Icon in C#, WPF</span></a></li>
</ul>
</div>
</div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com10tag:blogger.com,1999:blog-2161576857104408209.post-19215815726968543842013-01-22T07:02:00.000-08:002013-01-30T22:16:46.312-08:00C# Flags Attribute<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">FlagsAttribute</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is an attribute that can be placed on your enumeration in order to be comparable with multiple enumeration values. </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Usually, this attribute uses </span><span style="font-family: Courier New, Courier, monospace;">bitwise </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">operation in the comparison. Please visit Microsoft <a href="http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx" target="_blank">documentation</a> for further details.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please visit <a href="http://codesdirectory.blogspot.com/2013/01/c-bitwise.html" target="_blank">C# Bitwise</a> blog for you to understand further the bitwise operation.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Here we created a very simple enumeration named </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> that enumerate the kind of privileges we can give to every user of our website.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">FlagsAttribute</span>]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">enum</span> <span style="color: #2b91af;">Privilege</span> : <span style="color: blue;">short</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Read = 1,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Write =
2,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Delete =
4,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Create =
8,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Share =
16,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Append =
32,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Assign =
64<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You will see above we declared the </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> enumeration with </span><span style="font-family: Courier New, Courier, monospace;">FlagsAttribute</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Unfortunately in order the value of the enumerations to be participated in</span><span style="font-family: Courier New, Courier, monospace;"> bitwise</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> operation we need to declare its value in proper decimal value. In our case, we need to enumerate the value </span><span style="font-family: Courier New, Courier, monospace;">1, 2, 4, 8, 16, 32</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and so on.</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, using this enumeration in your class can now be checked with </span><span style="font-family: Courier New, Courier, monospace;">bitwise</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> comparison. See our class below declaring the </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class.</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">User</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> User(<span style="color: blue;">string</span>
username)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Username = username;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span>
Username<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">Privilege</span>
Privilege<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The class declared above named </span><span style="font-family: Courier New, Courier, monospace;">User </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">has property </span><span style="font-family: Courier New, Courier, monospace;">Username</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> of type </span><span style="font-family: Courier New, Courier, monospace;">String</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> of type </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Now using this class, we can set the bitwise value of the </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property. See the codes below how to do it.</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> user = <span style="color: blue;">new</span> <span style="color: #2b91af;">User</span>(System.Security.Principal.<span style="color: #2b91af;">WindowsIdentity</span>.GetCurrent().Name);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">user.Privilege = (<span style="color: #2b91af;">Privilege</span>.Read
| <span style="color: #2b91af;">Privilege</span>.Write | <span style="color: #2b91af;">Privilege</span>.Delete
| <span style="color: #2b91af;">Privilege</span>.Create);</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">What the above code is doing is, it has created a new object </span><span style="font-family: Courier New, Courier, monospace;">User</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> passing the current </span><span style="font-family: Courier New, Courier, monospace;">Windows Log On </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">name. After that, it set the </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property value to </span><span style="font-family: Courier New, Courier, monospace;">Read, Write, Delete</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Create</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. With the help of the </span><span style="font-family: Courier New, Courier, monospace;">Flags</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> attribute, we now participated in </span><span style="font-family: Courier New, Courier, monospace;">bitwise</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> operation by the use of the </span><span style="font-family: Courier New, Courier, monospace;">"|"</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> operator. This mean that the privilege property has now the value of the </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> enum's </span><span style="font-family: Courier New, Courier, monospace;">Read, Write, Delete</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Create</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Checking the presence of the </span><span style="font-family: Courier New, Courier, monospace;">Enumeration</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> value in </span><span style="font-family: Courier New, Courier, monospace;">bitmap</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> operation is very easy in </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. We are now required to use the </span><span style="font-family: Courier New, Courier, monospace;">"&"</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> operator to address it. See the code below.</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">if</span><span style="font-family: Consolas; font-size: 9.5pt;"> ((user.Privilege & <span style="color: #2b91af;">Privilege</span>.Delete)
== <span style="color: #2b91af;">Privilege</span>.Delete)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">/* do the delete code */</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">else</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> ((user.Privilege
& <span style="color: #2b91af;">Privilege</span>.Append) == <span style="color: #2b91af;">Privilege</span>.Append)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">/* do the append code */</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">else</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">/* do nothing */</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">What the code doing above is to simply determine whether the </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property has the defined </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> enumeration value. In short, we are asking if the </span><span style="font-family: Courier New, Courier, monospace;">User.Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property has </span><span style="font-family: Courier New, Courier, monospace;">Privilege.Delete</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> value.</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Narrowing Bitwise Operations</b></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The operator </span><span style="font-family: Courier New, Courier, monospace;">"-"</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is used to narrow the values of the </span><span style="font-family: Courier New, Courier, monospace;">Bitwise</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> operation. See the code below how to do it.</span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">user.Privilege -= <span style="color: #2b91af;">Privilege</span>.Delete;</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">With the code above, we are removing the </span><span style="font-family: Courier New, Courier, monospace;">Delete Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> value from the </span><span style="font-family: Courier New, Courier, monospace;">User.Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property. You will then see that the only remaining </span><span style="font-family: Courier New, Courier, monospace;">bitwise</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> value for the </span><span style="font-family: Courier New, Courier, monospace;">User.Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property is </span><span style="font-family: Courier New, Courier, monospace;">Read, Write</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Create</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Widening Bitwise Operations</b></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The operator </span><span style="font-family: Courier New, Courier, monospace;">"|"</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is used in widening of the </span><span style="font-family: Courier New, Courier, monospace;">Bitwise</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> operation. See the code below how to do it.</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 12.222222328186035px;">user.Privilege |= </span><span style="color: #2b91af; font-family: Consolas; font-size: 12.222222328186035px;">Privilege</span><span style="font-family: Consolas; font-size: 12.222222328186035px;">.Assign;</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Same with narrowing, what we did is that we simply added a new </span><span style="font-family: Courier New, Courier, monospace;">Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> enumeration value </span><span style="font-family: Courier New, Courier, monospace;">Assign</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> in to the </span><span style="font-family: Courier New, Courier, monospace;">User.Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property. By checking the </span><span style="font-family: Courier New, Courier, monospace;">User.Privilege</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property value is now </span><span style="font-family: Courier New, Courier, monospace;">Read, Write, Create</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Assign</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now that you know how to do it, have a happy coding. Tweak tweak tweak your codes now ;)</span></div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-27677934556961585982013-01-22T06:28:00.002-08:002013-01-30T22:16:46.313-08:00C# Bitwise<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>Understanding Bitwise Operation</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">bitwise </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">comparison commonly used the binary to build bit-mask entries same of what by </span><span style="font-family: Courier New, Courier, monospace;">boolean</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> type used (</span><span style="font-family: Courier New, Courier, monospace;">AND, OR, XOR, NOT)</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. </span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In computer world, each bit is the sum of smaller bits based on the placement. It is very easy for the computer's algorithm to read and understand it as it only uses 1 or 0 value (same with the boolean type). Please see the table below for you to understand further.</span><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivoRYvJtQRNch2DcptZu8lNF_FmajFS2O74Aj6MLM7vMzljtBDtLFom1c-9C7uVTdLESzM6XlWsoZbyxxR1ZCtmpTvIBqMe8xfhpbfs83-NFKGdC0A9lzeSb6x8Ghv37_6haAg4PKl2fA/s1600/bit.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivoRYvJtQRNch2DcptZu8lNF_FmajFS2O74Aj6MLM7vMzljtBDtLFom1c-9C7uVTdLESzM6XlWsoZbyxxR1ZCtmpTvIBqMe8xfhpbfs83-NFKGdC0A9lzeSb6x8Ghv37_6haAg4PKl2fA/s1600/bit.png" /></a></div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">In the above's table, the bit value is </span><span style="font-family: Courier New, Courier, monospace;">00101100</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">. Also, you will notice that above each bit octet has its corresponding </span><span style="font-family: Courier New, Courier, monospace;">decimal</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> value. In every </span><span style="font-family: Courier New, Courier, monospace;">bit 1</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> value is equal to its corresponding </span><span style="font-family: Courier New, Courier, monospace;">decimal</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> value. So based on the table above we have:</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<b><span style="font-family: Courier New, Courier, monospace;">( 1 = 0, </span></b><b><span style="font-family: Courier New, Courier, monospace;">2 = 0, </span></b><b><span style="font-family: Courier New, Courier, monospace;">4 = 1, </span></b><b><span style="font-family: Courier New, Courier, monospace;">8 = 1, </span></b><b><span style="font-family: Courier New, Courier, monospace;">16 = 0, </span></b><b><span style="font-family: Courier New, Courier, monospace;">32 = 1, </span></b><b><span style="font-family: Courier New, Courier, monospace;">64 = 0, </span></b><b><span style="font-family: Courier New, Courier, monospace;">128 = 0 )</span></b><br />
<b><span style="font-family: Courier New, Courier, monospace;"><br /></span></b>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We have </span><span style="font-family: Courier New, Courier, monospace;">4</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, </span><span style="font-family: Courier New, Courier, monospace;">8 </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">and </span><span style="font-family: Courier New, Courier, monospace;">32</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> decimal values equates by </span><span style="font-family: Courier New, Courier, monospace;">bit 1</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. By adding this value, it would equal the decimal value of the bits total. In our case, </span><span style="font-family: Courier New, Courier, monospace;">4 + 8 + 32</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is equals to </span><span style="font-family: Courier New, Courier, monospace;">44</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">So the decimal value of binary value </span><span style="font-family: Courier New, Courier, monospace;">00101100</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> is </span><span style="font-family: Courier New, Courier, monospace;">44</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please visit Microsoft <a href="http://msdn.microsoft.com/en-us/library/z0zec0b2(v=vs.71).aspx" target="_blank">documentation</a> for the bitwise and its operators for more information.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-12700438680239766952013-01-21T03:19:00.004-08:002013-01-29T01:55:24.061-08:00Compress/Decompress String in C#<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>How can we compress and decompress a string in C#?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Some of us might want to narrow down the size of the strings before we uploaded it in the database. This blog will explain you how will you do that in </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">As compression and decompression terminology has mentioned, in </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> the class </span><span style="font-family: Courier New, Courier, monospace;">GZipStream </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">is very popular in this technique. The </span><span style="font-family: Courier New, Courier, monospace;">GZipStream </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class provides a mechanism to the user (or library adapter) to do the actual compression and decompression.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Please visit the Microsoft <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx" target="_blank">documentation</a> for more details. Please note that </span><span style="font-family: Courier New, Courier, monospace;">GZipStream </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class is not limited in </span><span style="font-family: Courier New, Courier, monospace;">String </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">compression but it can even use the </span><span style="font-family: Courier New, Courier, monospace;">FileStream </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">object to do the compression in the file system (such like other compressor did - winzip and etc).</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We will show you our way how did we do the compression in the </span><span style="font-family: Courier New, Courier, monospace;">String</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Below are our codes.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">string</span> Compress(<span style="color: blue;">this</span> <span style="color: blue;">string</span> text)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> buffer = <span style="color: #2b91af;">Encoding</span>.UTF8.GetBytes(text);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> memoryStream = <span style="color: blue;">new</span>
<span style="color: #2b91af;">MemoryStream</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">using</span> (<span style="color: blue;">var</span> stream = <span style="color: blue;">new</span> <span style="color: #2b91af;">GZipStream</span>(memoryStream,
<span style="color: #2b91af;">CompressionMode</span>.Compress, <span style="color: blue;">true</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
stream.Write(buffer, 0, buffer.Length);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
memoryStream.Position = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> compressed = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[memoryStream.Length];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
memoryStream.Read(compressed, 0, compressed.Length);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> gZipBuffer = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[compressed.Length + 4];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">Buffer</span>.BlockCopy(compressed, 0, gZipBuffer, 4,
compressed.Length);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">Buffer</span>.BlockCopy(<span style="color: #2b91af;">BitConverter</span>.GetBytes(buffer.Length),
0, gZipBuffer, 0, 4);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: #2b91af;">Convert</span>.ToBase64String(gZipBuffer);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">Compress</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method used the </span><span style="font-family: Courier New, Courier, monospace;">MemoryStream</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object to store the compressed string in the computer's memory. After that, with the help of other binary related classes (</span><span style="font-family: Courier New, Courier, monospace;">Buffer</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, </span><span style="font-family: Courier New, Courier, monospace;">BitConverter</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">) we managed to transfer the bits from one object to other and return back the proper </span><span style="font-family: Courier New, Courier, monospace;">Base64 </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">string based on that buffer.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The opposite way around is to decompress the string. The codes below is the one we wrote and used.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">string</span> Decompress(<span style="color: blue;">this</span>
<span style="color: blue;">string</span> compressedText)</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> gZipBuffer = <span style="color: #2b91af;">Convert</span>.FromBase64String(compressedText);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">using</span> (<span style="color: blue;">var</span>
memoryStream = <span style="color: blue;">new</span> <span style="color: #2b91af;">MemoryStream</span>())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> dataLength = <span style="color: #2b91af;">BitConverter</span>.ToInt32(gZipBuffer,
0);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> buffer = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[dataLength];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
memoryStream.Position = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">using</span> (<span style="color: blue;">var</span> gZipStream
= <span style="color: blue;">new</span> <span style="color: #2b91af;">GZipStream</span>(memoryStream,
<span style="color: #2b91af;">CompressionMode</span>.Decompress))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
gZipStream.Read(buffer, 0, buffer.Length);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: #2b91af;">Encoding</span>.UTF8.GetString(buffer);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">What the codes above is doing its just simply decompressed the existing compressed text. Same way on </span><span style="font-family: Courier New, Courier, monospace;">Compress</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method, however, you will notice that the </span><span style="font-family: Courier New, Courier, monospace;">CompressionMode</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> used was </span><span style="font-family: Courier New, Courier, monospace;">Decompress</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. It is the opposite flag used when compression an existing uncompress string.</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We hope you will get your own good impression with what we had written. But please be assure that you understand how the process of </span><span style="font-family: Courier New, Courier, monospace;">compression</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">/</span><span style="font-family: Courier New, Courier, monospace;">decompression</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> happens. You could also extend that in the actual file system.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-6741450469565715322013-01-20T18:31:00.000-08:002013-01-29T01:55:24.285-08:00Cloning an object in C# (deep copy)<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>How can we clone an object in C#?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, if we are inheriting the base class </span><span style="font-family: Courier New, Courier, monospace;">System.Object</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> we can see there is a </span><span style="font-family: Courier New, Courier, monospace;">protected </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">method named </span><span style="font-family: Courier New, Courier, monospace;">MemberwiseClone</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. This method provides a facility to create a copy of our current object. Below is the basic sample.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Person</span> : System.<span style="color: #2b91af;">Object</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">Person</span>
Clone()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> (<span style="color: #2b91af;">Person</span>)<span style="color: blue;">base</span>.MemberwiseClone();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This is a very good method as it provides a copy of the object we wanted to copy. <b>But what if we want to copy an object ignoring the references on that object?</b> This method cannot address our scenario since the method </span><span style="font-family: Courier New, Courier, monospace;">MemberwiseClone </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">only provide us a shallow copy of an object.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">For you to understand further, shallow copy only creates a pointer to the current </span><span style="font-family: Courier New, Courier, monospace;">object </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">without creating an another pointer to an object referenced to it. Meaning, if there is a </span><span style="font-family: Courier New, Courier, monospace;">"byref"</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> </span><span style="font-family: Courier New, Courier, monospace;">object</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> property in that </span><span style="font-family: Courier New, Courier, monospace;">object</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, it will not create an another </span><span style="font-family: Courier New, Courier, monospace;">object </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">of the </span><span style="font-family: Courier New, Courier, monospace;">object </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">(it only reference it). Below are the codes extending the </span><span style="font-family: Courier New, Courier, monospace;">object </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">above and will explain the shallow copy.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Person</span> : System.<span style="color: #2b91af;">Object</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> Person(<span style="color: blue;">string</span>
name, <span style="color: #2b91af;">Pet</span> pet)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Pet = pet;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">Person</span>
Clone()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> (<span style="color: #2b91af;">Person</span>)<span style="color: blue;">base</span>.MemberwiseClone();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">Pet</span> Pet<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Pet</span> : System.<span style="color: #2b91af;">Object</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> Type</span></div>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">We created an additional </span><span style="font-family: Courier New, Courier, monospace;">class </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">named </span><span style="font-family: Courier New, Courier, monospace;">Pet</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Note it is a </span><span style="font-family: Courier New, Courier, monospace;">class (</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">not</span><span style="font-family: Courier New, Courier, monospace;"> struct) </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">and is a </span><span style="font-family: Courier New, Courier, monospace;">"by-ref"</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object. We also add a new method named </span><span style="font-family: Courier New, Courier, monospace;">Clone </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">but calling the base object </span><span style="font-family: Courier New, Courier, monospace;">MemberwiseClone </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">method. See how the referencing happens below.</span><br />
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> michael = <span style="color: blue;">new</span> <span style="color: #2b91af;">Person</span>(<span style="color: #a31515;">"Michael"</span>,
<span style="color: blue;">new</span> <span style="color: #2b91af;">Pet</span> {
Type = <span style="color: #a31515;">"Dog"</span>, Name = <span style="color: #a31515;">"Scooby"</span> });</span><br />
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">MessageBox</span><span style="font-family: Consolas; font-size: 9.5pt;">.Show(michael.Pet.Name);</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> cloned = michael.Clone();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">cloned.Pet.Name = <span style="color: #a31515;">"Scrappy"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">MessageBox</span><span style="font-family: Consolas; font-size: 9.5pt;">.Show(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"{0}={1}"</span>, michael.Pet.Name,
cloned.Pet.Name));</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">What we did is we created a </span><span style="font-family: Courier New, Courier, monospace;">Person </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">object named </span><span style="font-family: Courier New, Courier, monospace;">Michael </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">and its </span><span style="font-family: Courier New, Courier, monospace;">Pet</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">'s name as </span><span style="font-family: Courier New, Courier, monospace;">Scooby</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. We then copy the object and change the </span><span style="font-family: Courier New, Courier, monospace;">Pet </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">name to </span><span style="font-family: Courier New, Courier, monospace;">Scrappy</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Notice that the </span><span style="font-family: Courier New, Courier, monospace;">MessageBox </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">will display the same </span><span style="font-family: Courier New, Courier, monospace;">Pet </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">name. This is the reason because the </span><span style="font-family: Courier New, Courier, monospace;">object </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">was </span><span style="font-family: Courier New, Courier, monospace;">cloned </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">in a shallow way. Any reference to the property object as long it is a "</span><span style="font-family: Courier New, Courier, monospace;">by-ref</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">" object will be referenced to the cloned object.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Deep Cloning</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">A deep cloning is a technique of </span><span style="font-family: Courier New, Courier, monospace;">serializing/deserializing</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> of an object into the computer's memory. All its doing is to create a copy of pointer in the heap (memory) - (an additional pointer) and will give you an ability to </span><span style="font-family: Courier New, Courier, monospace;">deserialize </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">it as the same object in </span><span style="font-family: Courier New, Courier, monospace;">.NET</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Below is the code how to do it (extending the class above).</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">Person</span> : System.<span style="color: #2b91af;">Object</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> Person(<span style="color: blue;">string</span>
name, <span style="color: #2b91af;">Pet</span> pet)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Name = name;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.Pet = pet;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">Person</span>
Clone()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> (<span style="color: #2b91af;">Person</span>)<span style="color: blue;">base</span>.MemberwiseClone();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">Person</span>
DeepClone()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">using</span> (<span style="color: blue;">var</span> stream = <span style="color: blue;">new</span> <span style="color: #2b91af;">MemoryStream</span>())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">var</span> formatter = <span style="color: blue;">new</span>
<span style="color: #2b91af;">BinaryFormatter</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
formatter.Serialize(stream, <span style="color: blue;">this</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
stream.Position = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">return</span> (<span style="color: #2b91af;">Person</span>)formatter.Deserialize(stream);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">Pet</span> Pet<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">get</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">set</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The new method created is </span><span style="font-family: Courier New, Courier, monospace;">DeepClone</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. With the help of </span><span style="font-family: Courier New, Courier, monospace;">MemoryStream </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">and </span><span style="font-family: Courier New, Courier, monospace;">BinaryFormatter</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we can do </span><span style="font-family: Courier New, Courier, monospace;">serialize </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">an object into the memory and </span><span style="font-family: Courier New, Courier, monospace;">deserialize </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">it as the same object in </span><span style="font-family: Courier New, Courier, monospace;">.NET</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. However, the difference are the properties bound to the object even it is a </span><span style="font-family: Courier New, Courier, monospace;">"by-ref"</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object will also be recreated. It has created a different pointer in our computer's memory.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">So with the code's above calling the </span><span style="font-family: Courier New, Courier, monospace;">DeepClone </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">method will address the issue.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Note:</b> For us to participate in the </span><span style="font-family: Courier New, Courier, monospace;">Serialization</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we need to put an </span><span style="font-family: Consolas; font-size: 9.5pt;">[</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Serializable</span><span style="font-family: Consolas; font-size: 9.5pt;">]</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"> attribute in all class participated in the operation. In our case, we need to place that attribute above </span><span style="font-family: Courier New, Courier, monospace;">Pet </span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">and </span><span style="font-family: Courier New, Courier, monospace;">Person </span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">class. Please visit Microsoft <a href="http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx" target="_blank">documentation</a> for more information.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">I am hoping you guys read the explanation as this is a very common question during in the interviews.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-87829860995764598312013-01-18T06:27:00.000-08:002013-01-29T01:55:24.318-08:00Displaying System Icon in C#, WPF<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>How can we display the file system image in our Image object?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b><br /></b></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, some system </span><span style="font-family: Courier New, Courier, monospace;">API </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">is needed before we can manage to manipulate with the system objects. In some cases, we are doing the technique of </span><span style="font-family: Courier New, Courier, monospace;">Interoperability </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">to do the inter-operation manipulation outside the application.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">This blog will show you one technique how to use system </span><span style="font-family: Courier New, Courier, monospace;">APIs </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">with the </span><span style="font-family: Courier New, Courier, monospace;">Shell </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">functionality (file system manipulation) implementation.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Shelling is the process and technique of file system manipulation. Before we can do this, we need to import some basic functions from system </span><span style="font-family: Courier New, Courier, monospace;">API</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Our target is to display the system icon base on the </span><span style="font-family: Courier New, Courier, monospace;">full path</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">extension </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">of the file. Let's start with the system </span><span style="font-family: Courier New, Courier, monospace;">APIs</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Shell System API (functions and variables)</b></span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">DllImport</span>(<span style="color: #a31515;">"shell32.dll"</span>, CharSet = <span style="color: #2b91af;">CharSet</span>.Auto)]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">extern</span> <span style="color: #2b91af;">IntPtr</span>
SHGetFileInfo(<span style="color: blue;">string</span> pszPath, <span style="color: blue;">uint</span> dwFileAttributes, <span style="color: blue;">out</span>
<span style="color: #2b91af;">SHFileInfo</span> psfi, <span style="color: blue;">uint</span>
cbFileInfo, <span style="color: blue;">uint</span> uFlags);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">DllImport</span>(<span style="color: #a31515;">"user32.dll"</span>, SetLastError = <span style="color: blue;">true</span>)]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: blue;">return</span>: <span style="color: #2b91af;">MarshalAs</span>(<span style="color: #2b91af;">UnmanagedType</span>.Bool)]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">extern</span> <span style="color: blue;">bool</span>
DestroyIcon(<span style="color: #2b91af;">IntPtr</span> hIcon);</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">const</span> <span style="color: blue;">uint</span> SHGFI_ICON = 0x000000100;</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">const</span> <span style="color: blue;">uint</span> SHGFI_USEFILEATTRIBUTES = 0x000000010;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">const</span> <span style="color: blue;">uint</span> SHGFI_OPENICON = 0x000000002;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">const</span> <span style="color: blue;">uint</span> SHGFI_SMALLICON = 0x000000001;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">const</span> <span style="color: blue;">uint</span> SHGFI_LARGEICON = 0x000000000;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">const</span> <span style="color: blue;">uint</span> FILE_ATTRIBUTE_DIRECTORY = 0x00000010;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">const</span> <span style="color: blue;">uint</span> FILE_ATTRIBUTE_FILE = 0x00000100;</span></div>
<div class="MsoNormal">
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">DllImport </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">is an attribute used to import the system library into our application. For more information regarding </span><span style="font-family: Courier New, Courier, monospace;">DllImport</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, please visit Microsoft <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx" target="_blank">documentation</a>. We used the </span><span style="font-family: Courier New, Courier, monospace;">SHGetFileInfo </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">and </span><span style="font-family: Courier New, Courier, monospace;">DestroyIcon </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">system function because the target of this blog is to only display the system icons (by </span><span style="font-family: Courier New, Courier, monospace;">file path</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">extension</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">).</span></div>
<span style="font-family: Courier New, Courier, monospace;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">SHGetFileInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: is the system function used to retrieve the information of file/folder from the file system (Windows operating system).</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Courier New, Courier, monospace;">DestroyIcon</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">: is the system function used to destroy the icon.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><b>Note</b>: For you to follow our implementation, please place the codes above in one static </span><span style="font-family: Courier New, Courier, monospace;">class </span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">named </span><span style="font-family: Courier New, Courier, monospace;">Interop</span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After declaring the system functions/constants above, you need to create additional </span><span style="font-family: Courier New, Courier, monospace;">struct </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">and named it </span><span style="font-family: Courier New, Courier, monospace;">SHFileInfo</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Please see the code below.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">StructLayout</span>(<span style="color: #2b91af;">LayoutKind</span>.Sequential, CharSet = <span style="color: #2b91af;">CharSet</span>.Auto)]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">struct</span> <span style="color: #2b91af;">SHFileInfo</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: #2b91af;">IntPtr</span>
hIcon;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">int</span> iIcon;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">uint</span>
dwAttributes;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> [<span style="color: #2b91af;">MarshalAs</span>(<span style="color: #2b91af;">UnmanagedType</span>.ByValTStr,
SizeConst = 260)]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span>
szDisplayName;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> [<span style="color: #2b91af;">MarshalAs</span>(<span style="color: #2b91af;">UnmanagedType</span>.ByValTStr,
SizeConst = 80)]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">string</span>
szTypeName;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">};</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The </span><span style="font-family: Courier New, Courier, monospace;">struct </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">above is the one who will be used by the </span><span style="font-family: Courier New, Courier, monospace;">SHGetFileInfo </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">system function and will be the object who will handle all the information of the system file attribute.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After declaring the </span><span style="font-family: Courier New, Courier, monospace;">struct</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, we need to call the </span><span style="font-family: Courier New, Courier, monospace;">SHGetFileInfo </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">system function passing the right attribute and the </span><span style="font-family: Courier New, Courier, monospace;">struct </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">above so we can get the information of the system file. We have to create our own function to get the proper icon from the file system (with the the help of the system </span><span style="font-family: Courier New, Courier, monospace;">variables/functions</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> above). Please see below our codes.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">class</span> <span style="color: #2b91af;">ShellManager</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">Icon</span> GetIcon(<span style="color: blue;">string</span>
path, <span style="color: #2b91af;">ItemType</span> type, <span style="color: #2b91af;">IconSize</span> size, <span style="color: #2b91af;">ItemState</span>
state)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> flags = (<span style="color: blue;">uint</span>)(<span style="color: #2b91af;">Interop</span>.SHGFI_ICON | <span style="color: #2b91af;">Interop</span>.SHGFI_USEFILEATTRIBUTES);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> attribute = (<span style="color: blue;">uint</span>)(<span style="color: blue;">object</span>.Equals(type, <span style="color: #2b91af;">ItemType</span>.Folder)
? <span style="color: #2b91af;">Interop</span>.FILE_ATTRIBUTE_DIRECTORY : <span style="color: #2b91af;">Interop</span>.FILE_ATTRIBUTE_FILE);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.Equals(type,
<span style="color: #2b91af;">ItemType</span>.Folder) && <span style="color: blue;">object</span>.Equals(state, <span style="color: #2b91af;">ItemState</span>.Open))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
flags += <span style="color: #2b91af;">Interop</span>.SHGFI_OPENICON;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.Equals(size,
<span style="color: #2b91af;">IconSize</span>.Small))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
flags += <span style="color: #2b91af;">Interop</span>.SHGFI_SMALLICON;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
flags += <span style="color: #2b91af;">Interop</span>.SHGFI_LARGEICON;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> shfi = <span style="color: blue;">new</span> <span style="color: #2b91af;">SHFileInfo</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> res = <span style="color: #2b91af;">Interop</span>.SHGetFileInfo(path,
attribute, <span style="color: blue;">out</span> shfi, (<span style="color: blue;">uint</span>)<span style="color: #2b91af;">Marshal</span>.SizeOf(shfi), flags);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.Equals(res,
<span style="color: #2b91af;">IntPtr</span>.Zero)) <span style="color: blue;">throw</span>
<span style="color: #2b91af;">Marshal</span>.GetExceptionForHR(<span style="color: #2b91af;">Marshal</span>.GetHRForLastWin32Error());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">Icon</span>.FromHandle(shfi.hIcon);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> (<span style="color: #2b91af;">Icon</span>)<span style="color: #2b91af;">Icon</span>.FromHandle(shfi.hIcon).Clone();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">finally</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">Interop</span>.DestroyIcon(shfi.hIcon);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">What the code is doing is. We created our own class named </span><span style="font-family: Courier New, Courier, monospace;">ShellManager</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, inside this </span><span style="font-family: Courier New, Courier, monospace;">class</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> we created a method named </span><span style="font-family: Courier New, Courier, monospace;">GetIcon </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">to return the Icon object based on the file path (or extension) by using the </span><span style="font-family: Courier New, Courier, monospace;">SHGetFileInfo </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">system function and </span><span style="font-family: Courier New, Courier, monospace;">SHFileInfo </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">defined struct. Once you had called the system function, the hIcon property of the struct will be the one who will handle the </span><span style="font-family: Courier New, Courier, monospace;">Pointer </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">(</span><span style="font-family: Courier New, Courier, monospace;">Handle of IntPtr </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">object) of the system icon. From there, we can load the actual icon using the </span><span style="font-family: Courier New, Courier, monospace;">FromHandle </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">method of </span><span style="font-family: Courier New, Courier, monospace;">Icon </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Note:</b> It is an </span><span style="font-family: Courier New, Courier, monospace;">Icon</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> class of </span><span style="font-family: Courier New, Courier, monospace;">System.Drawing</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> namespace.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Additional supportive enumeration (personalize enumerations).</span><br />
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">enum</span> <span style="color: #2b91af;">IconSize</span> : <span style="color: blue;">short</span></span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Small,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Large<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">enum</span> <span style="color: #2b91af;">ItemState</span> : <span style="color: blue;">short</span></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Undefined,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Open,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Close<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now, after placing everything in proper place. You can call the </span><span style="font-family: Courier New, Courier, monospace;">ShellManager GetIcon </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">method to get the icon</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Code below is the sample one.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> icon = </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">ShellManager</span><span style="font-family: Consolas; font-size: 9.5pt;">.GetIcon(<span style="color: #2b91af;">Path</span>.GetExtension(filename),
<span style="color: #2b91af;">ItemType</span>.File, <span style="color: #2b91af;">IconSize</span>.Small,
<span style="color: #2b91af;">ItemState</span>.Undefined);</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><br /></b></span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Implementing FolderManager and FileManager class</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Below are the codes we can use to manage the icon for both folder and icon.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">FolderManager</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">ImageSource</span> GetImageSource(<span style="color: blue;">string</span> directory, <span style="color: #2b91af;">ItemState</span>
folderType)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: #2b91af;">FolderManager</span>.GetImageSource(directory,
<span style="color: blue;">new</span> <span style="color: #2b91af;">Size</span>(16,
16), folderType);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">ImageSource</span> GetImageSource(<span style="color: blue;">string</span> directory, <span style="color: #2b91af;">Size</span> size, <span style="color: #2b91af;">ItemState</span> folderType)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">using</span> (<span style="color: blue;">var</span> icon = <span style="color: #2b91af;">ShellManager</span>.GetIcon(directory, <span style="color: #2b91af;">ItemType</span>.Folder, <span style="color: #2b91af;">IconSize</span>.Large,
folderType))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">return</span> <span style="color: #2b91af;">Imaging</span>.CreateBitmapSourceFromHIcon(icon.Handle,
System.Windows.<span style="color: #2b91af;">Int32Rect</span>.Empty, <span style="color: #2b91af;">BitmapSizeOptions</span>.FromWidthAndHeight((<span style="color: blue;">int</span>)size.Width, (<span style="color: blue;">int</span>)size.Height));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">FileManager</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">ImageSource</span> GetImageSource(<span style="color: blue;">string</span> filename)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: #2b91af;">FileManager</span>.GetImageSource(filename,
<span style="color: blue;">new</span> <span style="color: #2b91af;">Size</span>(16,
16));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">ImageSource</span> GetImageSource(<span style="color: blue;">string</span> filename, <span style="color: #2b91af;">Size</span> size)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">using</span> (<span style="color: blue;">var</span> icon = <span style="color: #2b91af;">ShellManager</span>.GetIcon(<span style="color: #2b91af;">Path</span>.GetExtension(filename),
<span style="color: #2b91af;">ItemType</span>.File, <span style="color: #2b91af;">IconSize</span>.Small,
<span style="color: #2b91af;">ItemState</span>.Undefined))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">return</span> <span style="color: #2b91af;">Imaging</span>.CreateBitmapSourceFromHIcon(icon.Handle,
System.Windows.<span style="color: #2b91af;">Int32Rect</span>.Empty, <span style="color: #2b91af;">BitmapSizeOptions</span>.FromWidthAndHeight((<span style="color: blue;">int</span>)size.Width, (<span style="color: blue;">int</span>)size.Height));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">throw</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
You'll notice that in the </span><span style="font-family: Courier New, Courier, monospace;">GetImageSource</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method, we used the </span><span style="font-family: Courier New, Courier, monospace;">ShellManager.GetIcon</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method and create a new </span><span style="font-family: Courier New, Courier, monospace;">ImageSource</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> object by mimicing the existing one and do the resize. This also give us an idea how resize an image.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The code above returned the </span><span style="font-family: Courier New, Courier, monospace;">ImageSource </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">object which you can use to the value of the </span><span style="font-family: Courier New, Courier, monospace;">Source </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">property of the </span><span style="font-family: Courier New, Courier, monospace;">Image </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">control.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Congratulations! You just finish the tutorial how to display system icon in your </span><span style="font-family: Courier New, Courier, monospace;">Image </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">object.</span>Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com7tag:blogger.com,1999:blog-2161576857104408209.post-9919620973883691072013-01-18T02:16:00.002-08:002013-01-29T01:55:24.320-08:00Throw vs Throw Ex<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>What is the difference between throw and throw ex in C#?</b></span><br />
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">See below direct point of the actual difference of </span><span style="font-family: Courier New, Courier, monospace;">throw </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">and </span><span style="font-family: Courier New, Courier, monospace;">throw ex</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Throw Ex</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Basically, </span><span style="font-family: Courier New, Courier, monospace;">throw ex</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> command resets the stack trace and will clear any originated </span><span style="font-family: Courier New, Courier, monospace;">exception </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">from handled exception. You will see that the </span><span style="font-family: Courier New, Courier, monospace;">StackTrace </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">value will be cleared and it will only include the exception that was thrown. Catching different type of exception will also differ the results. See below the code.</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt;"> CodesDirectory<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">WIN_ThrowThrowEx</span>
: <span style="color: #2b91af;">Window</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> WIN_ThrowThrowEx()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
InitializeComponent();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.ErrorMethod();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span> (<span style="color: #2b91af;">Exception</span>
ex)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: #2b91af;">MessageBox</span>.Show(<span style="color: blue;">string</span>.Format(<span style="color: #a31515;">"{0}\nStack:
{1}"</span>, ex.Message, ex.StackTrace), <span style="color: #a31515;">"Original
Message"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">void</span>
ErrorMethod()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.DivideError();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span> (<span style="color: #2b91af;">Exception</span>
ex)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="background-color: yellow;"><span style="color: blue;">throw</span> ex;</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">int</span>
DivideError()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> x = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> 1 / x;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">You will notice we throw the exception object in yellow background. The result is below.</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> at
CodesDirectory.WIN_ThrowThrowEx.ErrorMethod() in
C:\Users\mpendon\Documents\Visual Studio
2010\Projects\CodesDirectory\WIN_ThrowThrowEx.xaml.cs:line 40<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> at
CodesDirectory.WIN_ThrowThrowEx..ctor() in C:\Users\mpendon\Documents\Visual
Studio 2010\Projects\CodesDirectory\WIN_ThrowThrowEx.xaml.cs:line 24</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b><br /></b></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Throw</b></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">However, if you however change the </span><span style="font-family: Courier New, Courier, monospace;">throw ex</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> to only </span><span style="font-family: Courier New, Courier, monospace;">throw</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. Then the result is below.</span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span>
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> at
CodesDirectory.WIN_ThrowThrowEx.DivideError() in
C:\Users\mpendon\Documents\Visual Studio
2010\Projects\CodesDirectory\WIN_ThrowThrowEx.xaml.cs:line 47<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> at
CodesDirectory.WIN_ThrowThrowEx.ErrorMethod() in
C:\Users\mpendon\Documents\Visual Studio
2010\Projects\CodesDirectory\WIN_ThrowThrowEx.xaml.cs:line 40<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> at
CodesDirectory.WIN_ThrowThrowEx..ctor() in C:\Users\mpendon\Documents\Visual
Studio 2010\Projects\CodesDirectory\WIN_ThrowThrowEx.xaml.cs:line 24</span></div>
<div class="MsoNormal">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">So please always catch the exception and just simply call the </span><span style="font-family: Courier New, Courier, monospace;">throw </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">instead of </span><span style="font-family: Courier New, Courier, monospace;">throw ex</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> so it would not affect any debugging session of the user. Our advice is to let you just use </span><span style="font-family: Courier New, Courier, monospace;">throw </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">command.</span></div>
<br />Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0tag:blogger.com,1999:blog-2161576857104408209.post-57158137868155544372013-01-17T03:19:00.002-08:002013-01-29T01:55:24.273-08:00Cropping an Image in C#<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>Explanation on how to crop an image in C#.Net</b></span><br />
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">I wrote this code when I was working with my own social network site and run-through with the images problem in Web (large images were displayed late in the pages). In </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> there is no built-in </span><span style="font-family: Courier New, Courier, monospace;">Class </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">or </span><span style="font-family: Courier New, Courier, monospace;">API </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">to be used to crop an image file or an image object. I atleast need to write my own function or class to accomplish my requirements.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In business needs, some of the images of your application were saved in the database. If you're working with the </span><span style="font-family: Courier New, Courier, monospace;">CRM </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">or </span><span style="font-family: Courier New, Courier, monospace;">ECommerce </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">enterprise applications and systems, this scenario is very common. </span><span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">With this technique, before you upload the image in the database, you should resize it first to a small one.</span></div>
<div>
<span style="font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">I'll explain in detail how are we going to accomplish it.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Explanation</b></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">When you have your file converted to an object </span><span style="font-family: Courier New, Courier, monospace;">Image</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> (of </span><span style="font-family: Courier New, Courier, monospace;">System.Drawing</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> namespace) in </span><span style="font-family: Courier New, Courier, monospace;">C#</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">, then you can do anything with the help of </span><span style="font-family: Courier New, Courier, monospace;">Bitmap </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class and other image related classes. </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In .NET 4.0, the class </span><span style="font-family: Courier New, Courier, monospace;">Bitmap </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">(of </span><span style="font-family: Courier New, Courier, monospace;">System.Drawing</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> namespace) will do the job.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">First, you need to create an image from a file (or you can create with your own). The </span><span style="font-family: Courier New, Courier, monospace;">FromFile</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> method of </span><span style="font-family: Courier New, Courier, monospace;">Image </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class will load the image file from the file system. See the codes below.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="SV" style="color: blue; font-family: Consolas; font-size: 9.5pt; mso-ansi-language: SV;">var</span><span lang="SV" style="font-family: Consolas; font-size: 9.5pt; mso-ansi-language: SV;">
filename = <span style="color: #a31515;">@"c:\personal\images\horizon.png"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> img = <span style="color: #2b91af;">Image</span>.FromFile(filename);</span></div>
</div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After you have loaded the image, use the </span><span style="font-family: Courier New, Courier, monospace;">Bitmap </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class to crop the </span><span style="font-family: Courier New, Courier, monospace;">Image </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">object. Cloning an image via </span><span style="font-family: Courier New, Courier, monospace;">Bitmap </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class will require an argument of </span><span style="font-family: 'Courier New', Courier, monospace;">Rectangle </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">and </span><span style="font-family: Courier New, Courier, monospace;">PixelFormat</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">. It depends on your desire size, you can pass it as an argument value in the </span><span style="font-family: Courier New, Courier, monospace;">Rectangle Size</span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> rect = <span style="color: blue;">new</span> <span style="color: #2b91af;">Rectangle</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">Point</span>(0, 0), img.Size);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> cloned = <span style="color: blue;">new</span> <span style="color: #2b91af;">Bitmap</span>(img).Clone(rect, img.PixelFormat);<o:p></o:p></span></div>
</div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After cloning, you have your option to create another </span><span style="font-family: Courier New, Courier, monospace;">Bitmap </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">object and set your desired size there. Then dispose the cloned object. See below the code.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> bitmap = <span style="color: blue;">new</span> <span style="color: #2b91af;">Bitmap</span>(cloned, <span style="color: blue;">new</span>
<span style="color: #2b91af;">Size</span>(50, 50));<o:p></o:p></span></div>
</div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">cloned.Dispose();</span></div>
</div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The code above shows you how to do the actual cropping.</span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>My Class Implementation</b></span></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Collections.Generic;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Linq;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Text;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.IO;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Drawing;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt;"> CodesDirectory<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ImageUtility</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">Image</span> Crop(<span style="color: #2b91af;">Image</span>
image, <span style="color: blue;">int</span> height, <span style="color: blue;">int</span>
width)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.ReferenceEquals(image,
<span style="color: blue;">null</span>)) <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">NullReferenceException</span>(<span style="color: #a31515;">"Image"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> squareLength = image.Width < image.Height ?
image.Width : image.Height;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> top = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> left = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (image.Width > image.Height)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
left = (image.Width / 2) - (image.Height / 2);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
top = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
left = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> top = (image.Height / 2) -
(image.Width / 2);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> rect = <span style="color: blue;">new</span> <span style="color: #2b91af;">Rectangle</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">Point</span>(left, top), <span style="color: blue;">new</span>
<span style="color: #2b91af;">Size</span>(squareLength, squareLength));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> cloned = <span style="color: blue;">new</span> <span style="color: #2b91af;">Bitmap</span>(image).Clone(rect, image.PixelFormat);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> bitmap = <span style="color: blue;">new</span> <span style="color: #2b91af;">Bitmap</span>(cloned,
<span style="color: blue;">new</span> <span style="color: #2b91af;">Size</span>(width,
height));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
cloned.Dispose();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> bitmap;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">Image</span> CropRatio(<span style="color: #2b91af;">Image</span>
image, <span style="color: blue;">int</span> max)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.ReferenceEquals(image,
<span style="color: blue;">null</span>)) <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">NullReferenceException</span>(<span style="color: #a31515;">"Image"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">double</span> ratio = image.Height > image.Width ? (<span style="color: blue;">double</span>)image.Width / (<span style="color: blue;">double</span>)image.Height
: (<span style="color: blue;">double</span>)image.Height / (<span style="color: blue;">double</span>)image.Width;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">double</span> height = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">double</span> width = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (image.Height > image.Width)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
height = image.Height > max ? max : image.Height;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
width = height * ratio;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
width = image.Width > max ? max : image.Width;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
height = width * ratio;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> rect = <span style="color: blue;">new</span> <span style="color: #2b91af;">Rectangle</span>(<span style="color: blue;">new</span> <span style="color: #2b91af;">Point</span>(0, 0), <span style="color: blue;">new</span> <span style="color: #2b91af;">Size</span>(image.Width, image.Height));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> cloned = <span style="color: blue;">new</span> <span style="color: #2b91af;">Bitmap</span>(image).Clone(rect, image.PixelFormat);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">var</span> bitmap = <span style="color: blue;">new</span> <span style="color: #2b91af;">Bitmap</span>(cloned, <span style="color: blue;">new</span>
<span style="color: #2b91af;">Size</span>((<span style="color: blue;">int</span>)width,
(<span style="color: blue;">int</span>)height));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
cloned.Dispose();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> bitmap;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> System.<span style="color: #2b91af;">Boolean</span> IsValidImage(<span style="color: blue;">string</span>
filename)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">var</span> bitmap = <span style="color: blue;">new</span>
<span style="color: #2b91af;">Bitmap</span>(filename);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
bitmap.Dispose();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">static</span> System.<span style="color: #2b91af;">Boolean</span> IsValidImage(<span style="color: #2b91af;">Stream</span>
stream)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">var</span> bitmap = <span style="color: blue;">new</span>
<span style="color: #2b91af;">Bitmap</span>(stream);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
bitmap.Dispose();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">return</span> <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
</div>
<div>
<div class="MsoNormal">
<o:p><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now we just finish the job cropping of image.</span></o:p></div>
<br /></div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com3tag:blogger.com,1999:blog-2161576857104408209.post-22415424415170389202013-01-16T20:55:00.001-08:002013-01-29T01:55:24.067-08:00Animating an Image in WPF using Timer<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><b>How to animate and Image in WPF using Timer?</b></span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif; font-size: large;"><br /></span>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">For further discussion please visit <a href="http://codesdirectory.blogspot.com/2013/01/animating-image-in-wpf.html" target="_blank">Animating an Image in WPF</a> blog. Here I just simply pasted the alternative solution on how to animate an image in WPF using the </span><span style="font-family: Courier New, Courier, monospace;">Timer </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class (instead of </span><span style="font-family: Courier New, Courier, monospace;">Animation </span><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">class).</span><br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span>
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Collections.Generic;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Linq;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Text;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Controls;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Data;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Documents;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Input;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Media;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Media.Imaging;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Navigation;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Shapes;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Windows.Media.Animation;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="font-family: Consolas; font-size: 9.5pt;"> System.Timers;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">namespace</span><span style="font-family: Consolas; font-size: 9.5pt;"> CodesDirectory.Controls<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">AnimatableImage</span>
: <span style="color: #2b91af;">Image</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
Privates<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">const</span> <span style="color: blue;">int</span> TIME_MILLISECONDS_PER_IMAGEFRAME = 75;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: #2b91af;">Timer</span>
__timer = <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">int</span>
__currentFrameIndex = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> AnimatableImage()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
InitializeComponent();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__timer = <span style="color: blue;">new</span> <span style="color: #2b91af;">Timer</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">this</span>.__timer.Elapsed += <span style="color: blue;">new</span>
<span style="color: #2b91af;">ElapsedEventHandler</span>(__timer_Elapsed);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span> __timer_Elapsed(<span style="color: blue;">object</span>
sender, <span style="color: #2b91af;">ElapsedEventArgs</span> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">this</span>.Dispatcher.CheckAccess())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.Dispatcher.Invoke(<span style="color: blue;">new</span> <span style="color: #2b91af;">Action</span><<span style="color: blue;">object</span>, <span style="color: #2b91af;">ElapsedEventArgs</span>>(<span style="color: blue;">this</span>.__timer_Elapsed), <span style="color: blue;">new</span>
<span style="color: blue;">object</span>[] { sender, e });<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.Source, <span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.Source = ((<span style="color: #2b91af;">BitmapFrame</span>)<span style="color: blue;">this</span>.Source).Decoder.Frames[<span style="color: blue;">this</span>.__currentFrameIndex];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
__currentFrameIndex++;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (<span style="color: blue;">this</span>.__currentFrameIndex
>= <span style="color: blue;">this</span>.FrameCount) <span style="color: blue;">this</span>.__currentFrameIndex
= 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
Methods<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">void</span>
InitializeImageFrames()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.ReferenceEquals(<span style="color: blue;">this</span>.Source, <span style="color: blue;">null</span>)) <span style="color: blue;">return</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">BitmapFrame</span> bf = <span style="color: blue;">this</span>.Source
<span style="color: blue;">as</span> <span style="color: #2b91af;">BitmapFrame</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.ReferenceEquals(bf,
<span style="color: blue;">null</span>)) bf = <span style="color: #2b91af;">BitmapFrame</span>.Create((<span style="color: #2b91af;">BitmapSource</span>)<span style="color: blue;">this</span>.Source);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">object</span>.ReferenceEquals(bf.Decoder,
<span style="color: blue;">null</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.FrameCount =
bf.Decoder.Frames.Count;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.IsFramesInitiated = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">protected</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> OnPropertyChanged(<span style="color: #2b91af;">DependencyPropertyChangedEventArgs</span>
e)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">object</span>.Equals(e.Property,
<span style="color: #2b91af;">Image</span>.SourceProperty))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.InitializeImageFrames();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.__timer.Interval = <span style="color: #2b91af;">AnimatableImage</span>.TIME_MILLISECONDS_PER_IMAGEFRAME;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">base</span>.OnPropertyChanged(e);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">void</span>
StartAnimate()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (!<span style="color: blue;">this</span>.IsAnimating
&& <span style="color: blue;">this</span>.IsFramesInitiated)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.__timer.Start();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.IsAnimating = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">void</span>
StopAnimation()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (<span style="color: blue;">this</span>.IsAnimating)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.__timer.Stop();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">this</span>.IsAnimating = <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#region</span><span style="font-family: Consolas; font-size: 9.5pt;">
Properties<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">bool</span>
IsAnimating { <span style="color: blue;">get</span>; <span style="color: blue;">private</span>
<span style="color: blue;">set</span>; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">public</span> <span style="color: blue;">int</span> FrameCount
{ <span style="color: blue;">get</span>; <span style="color: blue;">private</span> <span style="color: blue;">set</span>; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">private</span> <span style="color: blue;">bool</span>
IsFramesInitiated { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">
#endregion</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
Michael Pendonhttp://www.blogger.com/profile/17155737871042332091noreply@blogger.com0