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

<channel>
	<title>Wizards of Smart &#187; Samples</title>
	<atom:link href="http://wizardsofsmart.net/category/samples/feed/" rel="self" type="application/rss+xml" />
	<link>http://wizardsofsmart.net</link>
	<description>.NET Design Patterns</description>
	<lastBuildDate>Thu, 29 Jul 2010 16:40:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-beta2-14896</generator>
		<item>
		<title>Linq &#8211; Sum</title>
		<link>http://wizardsofsmart.net/samples/linq-sum/</link>
		<comments>http://wizardsofsmart.net/samples/linq-sum/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 19:32:33 +0000</pubDate>
		<dc:creator>rookieone</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://wizardsofsmart.net/samples/linq-sum/</guid>
		<description><![CDATA[Last week I started an ongoing section during the Virtual Brown Bag where I go over a Linq function (or two). The general idea is that although most people ‘use’ Linq, there are lots of functions that are overlooked. In this blog post I will demonstrate basic usage cases for the Linq Sum function. Numbers [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I started an ongoing section during the Virtual Brown Bag where I go over a Linq function (or two). The general idea is that although most people ‘use’ Linq, there are lots of functions that are overlooked.</p>
<p>In this blog post I will demonstrate basic usage cases for the Linq Sum function.</p>
<h3>Numbers</h3>
<p>If you have a collection of numbers and you want the sum of their values, then&#160; simply call Sum. It can’t get any easier.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:272901ca-8772-4d65-8f69-c791799fc380" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #161616; overflow: auto; padding: 2px 5px;"><span style="background:#161616;color:#ffffff">[</span><span style="background:#161616;color:#ff7400">Test</span><span style="background:#161616;color:#ffffff">]</span><br /> <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">public</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#2b91af">void</span><span style="background:#161616;color:#ffffff"> should_sum_values()</span><br /> <span style="background:#161616;color:#ffffff">{</span><br />     <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">var</span><span style="background:#161616;color:#ffffff"> nums = </span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">List</span><span style="background:#161616;color:#ffffff">&lt;</span><span style="background:#161616;color:#2b91af">int</span><span style="background:#161616;color:#ffffff">&gt;</span><br />                    <span style="background:#161616;color:#ffffff">{</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#bda265">5</span><span style="background:#161616;color:#ffffff">,</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#bda265">9</span><span style="background:#161616;color:#ffffff">,</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#bda265">1</span><span style="background:#161616;color:#ffffff">,</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#bda265">10</span><br />                    <span style="background:#161616;color:#ffffff">};</span><br />     <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">int</span><span style="background:#161616;color:#ffffff"> sum = nums.Sum();</span></p>
<p>     <span style="background:#161616;color:#ffffff">sum.ShouldBe(</span><span style="background:#161616;color:#bda265">25</span><span style="background:#161616;color:#ffffff">);</span><br /> <span style="background:#161616;color:#ffffff">}</span></div>
</p></div>
</p></div>
<h3>Non-Numbers</h3>
<p>If you want to sum a collection that isn’t made up of numbers, then you will be forced to specify which number property you would like to sum.</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:37a00393-8d97-40fd-b4b4-8943d6a11b05" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #161616; overflow: auto; padding: 2px 5px;"><span style="background:#161616;color:#ffffff">[</span><span style="background:#161616;color:#ff7400">Test</span><span style="background:#161616;color:#ffffff">]</span><br /> <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">public</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#2b91af">void</span><span style="background:#161616;color:#ffffff"> summing_non_number_collection()</span><br /> <span style="background:#161616;color:#ffffff">{</span><br />     <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">var</span><span style="background:#161616;color:#ffffff"> jedi = </span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">List</span><span style="background:#161616;color:#ffffff">&lt;</span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">&gt;</span><br />                    <span style="background:#161616;color:#ffffff">{</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">(</span><span style="background:#161616;color:#668f5f">&quot;Yoda&quot;</span><span style="background:#161616;color:#ffffff">).MidichlorianCountIs(</span><span style="background:#161616;color:#bda265">1000</span><span style="background:#161616;color:#ffffff">),</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">(</span><span style="background:#161616;color:#668f5f">&quot;Anakin Skywalker&quot;</span><span style="background:#161616;color:#ffffff">).MidichlorianCountIs(</span><span style="background:#161616;color:#bda265">3000</span><span style="background:#161616;color:#ffffff">),</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">(</span><span style="background:#161616;color:#668f5f">&quot;Luke Skywalker&quot;</span><span style="background:#161616;color:#ffffff">).MidichlorianCountIs(</span><span style="background:#161616;color:#bda265">1500</span><span style="background:#161616;color:#ffffff">),</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">(</span><span style="background:#161616;color:#668f5f">&quot;Obi-wan Kenobi&quot;</span><span style="background:#161616;color:#ffffff">).MidichlorianCountIs(</span><span style="background:#161616;color:#bda265">500</span><span style="background:#161616;color:#ffffff">),</span><br />                    <span style="background:#161616;color:#ffffff">};</span><br />     <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#008000">// can&#39;t compile</span><br />     <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#008000">//var sum = jedi.Sum();</span><br /> <span style="background:#161616;color:#ffffff">}</span></div>
</p></div>
</p></div>
</p>
<p>&#160;</p>
</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:29c522ec-e67b-4959-beac-ec6fe2e4eb4b" class="wlWriterEditableSmartContent">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt">
<div style="background-color: #161616; overflow: auto; padding: 2px 5px;"><span style="background:#161616;color:#ffffff">[</span><span style="background:#161616;color:#ff7400">Test</span><span style="background:#161616;color:#ffffff">]</span><br /> <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">public</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#2b91af">void</span><span style="background:#161616;color:#ffffff"> should_be_able_to_specify_property_to_sum()</span><br /> <span style="background:#161616;color:#ffffff">{</span><br />     <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">var</span><span style="background:#161616;color:#ffffff"> jedi = </span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">List</span><span style="background:#161616;color:#ffffff">&lt;</span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">&gt;</span><br />                    <span style="background:#161616;color:#ffffff">{</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">(</span><span style="background:#161616;color:#668f5f">&quot;Yoda&quot;</span><span style="background:#161616;color:#ffffff">).MidichlorianCountIs(</span><span style="background:#161616;color:#bda265">1000</span><span style="background:#161616;color:#ffffff">),</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">(</span><span style="background:#161616;color:#668f5f">&quot;Anakin Skywalker&quot;</span><span style="background:#161616;color:#ffffff">).MidichlorianCountIs(</span><span style="background:#161616;color:#bda265">3000</span><span style="background:#161616;color:#ffffff">),</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">(</span><span style="background:#161616;color:#668f5f">&quot;Luke Skywalker&quot;</span><span style="background:#161616;color:#ffffff">).MidichlorianCountIs(</span><span style="background:#161616;color:#bda265">1500</span><span style="background:#161616;color:#ffffff">),</span><br />                        <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">new</span><span style="background:#161616;color:#ffffff"> </span><span style="background:#161616;color:#ff7400">Jedi</span><span style="background:#161616;color:#ffffff">(</span><span style="background:#161616;color:#668f5f">&quot;Obi-wan Kenobi&quot;</span><span style="background:#161616;color:#ffffff">).MidichlorianCountIs(</span><span style="background:#161616;color:#bda265">500</span><span style="background:#161616;color:#ffffff">),</span><br />                    <span style="background:#161616;color:#ffffff">};</span></p>
<p>     <span style="background:#161616;color:#ffffff"></span><span style="background:#161616;color:#2b91af">int</span><span style="background:#161616;color:#ffffff"> sum = jedi.Sum(j =&gt; j.MidichlorianCount);</span></p>
<p>     <span style="background:#161616;color:#ffffff">sum.ShouldBe(</span><span style="background:#161616;color:#bda265">6000</span><span style="background:#161616;color:#ffffff">);</span><br /> <span style="background:#161616;color:#ffffff">}</span></div>
</p></div>
</p></div>
</p>
<p>These tests can be found in my Learning Solution on GitHub and found in the Learning CSharp project : <a href="http://github.com/RookieOne/Learning" onclick="urchinTracker('/outgoing/github.com/RookieOne/Learning?referer=');">http://github.com/RookieOne/Learning</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/samples/linq-sum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming the Semantic Web with F#</title>
		<link>http://wizardsofsmart.net/samples/programming-the-semantic-web-with-f/</link>
		<comments>http://wizardsofsmart.net/samples/programming-the-semantic-web-with-f/#comments</comments>
		<pubDate>Wed, 26 May 2010 05:13:54 +0000</pubDate>
		<dc:creator>panesofglass</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://wizardsofsmart.net/?p=327</guid>
		<description><![CDATA[I&#8217;m currently reading O&#8217;Reilly&#8217;s Programming the Semantic Web (when I have a chance), and I decided I would try the samples in F# instead of Python. Why? Well, 1) I love F#, and 2) I want to see how closely the two languages really are related. They seem very much related syntactically, and I&#8217;m finding [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently reading O&#8217;Reilly&#8217;s Programming the Semantic Web (when I have a chance), and I decided I would try the samples in F# instead of Python. Why? Well, 1) I love F#, and 2) I want to see how closely the two languages really are related. They seem very much related syntactically, and I&#8217;m finding that&#8217;s somewhat true. Either way, it&#8217;s fun, I get to explore several things I&#8217;m interested in at once, and it still (loosely) relates to all the other stuff I have going on. <img src='http://wizardsofsmart.net/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Anyway, here&#8217;s some snippets from Chapter 2. If you have feedback or fixes, please feel free to share!</p>
<script src="http://gist.github.com/414082.js"></script>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/samples/programming-the-semantic-web-with-f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVVM &#8211; The no code behind challenge</title>
		<link>http://wizardsofsmart.net/patterns/mvvm-the-no-code-behind-challenge-2/</link>
		<comments>http://wizardsofsmart.net/patterns/mvvm-the-no-code-behind-challenge-2/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 04:27:05 +0000</pubDate>
		<dc:creator>rookieone</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://wizardsofsmart.net/patterns/mvvm-the-no-code-behind-challenge-2/</guid>
		<description><![CDATA[Glenn Block has an excellent post titled “The spirit of MYYM, it’s not a code counting exercise”. The post can be found at CodeBetter. I thoroughly enjoyed the post and greatly appreciate practical discussions on WPF/SL application patterns. I am a bit of a WPF nut having lived and breathed xaml for over 2 years. [...]]]></description>
			<content:encoded><![CDATA[<p>Glenn Block has an excellent post titled “The spirit of MYYM, it’s not a code counting exercise”. The <a href="http://codebetter.com/blogs/glenn.block/archive/2009/08/02/the-spirit-of-mvvm-viewmodel-it-s-not-a-code-counting-exercise.aspx" onclick="urchinTracker('/outgoing/codebetter.com/blogs/glenn.block/archive/2009/08/02/the-spirit-of-mvvm-viewmodel-it-s-not-a-code-counting-exercise.aspx?referer=');">post</a> can be found at CodeBetter.</p>
<p>I thoroughly enjoyed the post and greatly appreciate practical discussions on WPF/SL application patterns. I am a bit of a WPF nut having lived and breathed xaml for over 2 years. I’ve been a part of 2 successful WPF projects (both in production). In both scenarios I acted as the defacto WPF guru. </p>
<p>This last project was a typical line of business application and we used the ViewModel pattern with Prism. I bring all this up to share my background with WPF and ViewModels.</p>
<p>Concerning the content of the post, I agree with Glenn on the majority of the content. The Commands and Parameters especially rings true. I found that whenever I went with option A [using element binding to a command parameter] I ended up refactoring it to option B [binding the ‘selected’ item to a property on the ViewModel]. Although a neat use of xaml, in practice the binding to the property just allowed more flexibility that indubitable becomes necessary with future changes and demands (not to mention testing opportunities).</p>
<p>With both projects we didn’t have the luxury of a full time designer. In both scenarios I argued there was plenty of work for a designer, but alas the resources just weren’t there. We were able to use a designer for a decent chunk of hours, but more could have been done. The applications were definitely better off for having every hour we could get from any designer resource. I believe the last project persuaded many that indeed there was enough work for a full time designer, so maybe I can hope for a designer with a future application.</p>
<p>I say this because I can not speak towards what its like having a designer in Blend all the time working on Views. When we used a designer resource, they would work in Blend and then a go-between developer / designer would take the xaml and integrate it with our ResourceDictionaries.</p>
<p>What I would like to address is the ‘no code behind’ philosophy compared to the code behind practicality.</p>
<p>Two years ago when I was floundering for WPF information and blogs, I ran across a blog post (I want to say it was Josh Smith) where the writer stated something like : “if something is easier to do in the code behind then its okay to do it in the code behind.”</p>
<p>He was arguing that a crap load of xaml vs a couple lines of code behind isn’t an argument at all. The code behind is the best choice. Naturally clearer intentions and code maintainability takes precedence over having a dogmatic “everything in xaml” approach. </p>
<p>With this last LoB Prism application, we had some choices to make when we were deciding on how the client should be made up.</p>
<p>Do we use UserControls as Views or (as Ryan Riley brought up) do we use Data Templates as views. I felt at the time the team wasn’t ready for that leap to Data Templates as views. I considered the code behind of a User Control as a placebo safety net for a team struggling with the WPF learning curve. What I did do was firmly encouraged a no code behind policy. A “no code behind” challenge if you will.</p>
<p>If I were to go back to the beginning and make that View decision again, I would go with the Data Templates as views. The team did very well without using the code behind. A Data Template view would have brought more benefits than the cost of having no code behind. Often we found ourselves thinking.. if this view was a data template and not a user control, then we could more easier do X.</p>
<p>I have championed the “no code behind” stance internal to Catapult and to other developers. Yes you can legitimately use the code behind to accomplish certain tasks. More often than not though (in my experience), the use of the code behind has more to do with the prejudices carried over from WinForms development. Having a strict no code behind stance forces developers to learn and change the way they think about coding functionality into the UI. </p>
<p>Glenn came up with a scenario (albiet a overly simplified one) of where he felt a code behind had some value. The scenario described is:</p>
<p>&#8216;”The user clicks a Save button on the Edit Order screen which requires some UI cue to the user such as an hourglass while the order is saving, and another cue once the Order has saved.”</p>
<p>He argues that the code in the xaml is less testable. He even suggests the solution I made. He also recognizes others my disagree. Count me in the disagreement camp.</p>
<p>I created a simple project that uses Attached Dependency properties to accomplish this simple scenario. This isn’t a “throw down the gauntlet” sort of thing and I didn’t really write it for Glenn. A coworker emailed Glenn’s blog and I responded in an email describing my solution. He then asked to see it in action. So I coded it up.</p>
<p>He also asked me to post a comment on Glenn’s post, but I don’t really see the point especially since I agree with the information Glenn is sharing and his opinions are built on experience &amp; expertise. I really had no disagreement and the solution Glenn already knows of. /shrug</p>
<p>Anyway.. you can find the solution here : <a title="http://github.com/RookieOne/AttachedAnimation/tree/master" href="http://github.com/RookieOne/AttachedAnimation/tree/master" onclick="urchinTracker('/outgoing/github.com/RookieOne/AttachedAnimation/tree/master?referer=');">http://github.com/RookieOne/AttachedAnimation/tree/master</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/patterns/mvvm-the-no-code-behind-challenge-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toggling Read Only with WPF</title>
		<link>http://wizardsofsmart.net/samples/toggling-read-only-with-wpf/</link>
		<comments>http://wizardsofsmart.net/samples/toggling-read-only-with-wpf/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 02:56:33 +0000</pubDate>
		<dc:creator>rookieone</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://wizardsofsmart.net/samples/toggling-read-only-with-wpf/</guid>
		<description><![CDATA[Ayende tweeted a brief code snippet of some WPF he threw together (well I assume he threw it together). You can see the code snippet here : http://pastie.org/568259 He asked : Someone PLEASE tell me there is a better way than this (WPF) I tweeted a brief snippet response to improve that tiny bit. The [...]]]></description>
			<content:encoded><![CDATA[<p>Ayende tweeted a brief code snippet of some WPF he threw together (well I assume he threw it together).</p>
<p>You can see the code snippet here : <a title="http://pastie.org/568259" href="http://pastie.org/568259" onclick="urchinTracker('/outgoing/pastie.org/568259?referer=');">http://pastie.org/568259</a></p>
<p>He asked : Someone PLEASE tell me there is a better way than this (WPF)</p>
<p>I tweeted a brief snippet response to improve that tiny bit. The snippet can be found here : <a title="http://codesnippets.joyent.com/posts/show/2221" href="http://codesnippets.joyent.com/posts/show/2221" onclick="urchinTracker('/outgoing/codesnippets.joyent.com/posts/show/2221?referer=');">http://codesnippets.joyent.com/posts/show/2221</a></p>
<p>But I couldn’t just leave it at that. I wanted to show a different approach to solving the same problem. </p>
<p>How can we make a control ‘read only’ through binding?</p>
<p>The solution can be found here on my Git Hub Repository : <a title="http://github.com/RookieOne/WpfTogglingReadOnly/tree/master" href="http://github.com/RookieOne/WpfTogglingReadOnly/tree/master" onclick="urchinTracker('/outgoing/github.com/RookieOne/WpfTogglingReadOnly/tree/master?referer=');">http://github.com/RookieOne/WpfTogglingReadOnly/tree/master</a></p>
<p>Ignoring all the other machinations, the money xaml is in the TextBoxStyle found in the Styles\TextBoxStyles.xaml resource dictionary.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;Style x:Key=<span style="color: #006080">&quot;TextBoxStyle&quot;</span> TargetType=<span style="color: #006080">&quot;TextBox&quot;</span>&gt;        </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;Style.Triggers&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        &lt;DataTrigger Binding=<span style="color: #006080">&quot;{Binding IsReadOnly}&quot;</span> Value=<span style="color: #006080">&quot;True&quot;</span>&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            &lt;!-- Readonly Template --&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            &lt;Setter Property=<span style="color: #006080">&quot;Template&quot;</span>&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                &lt;Setter.Value&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    &lt;ControlTemplate TargetType=<span style="color: #006080">&quot;TextBox&quot;</span>&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                        &lt;TextBlock Text=<span style="color: #006080">&quot;{TemplateBinding Text}&quot;</span> /&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                    &lt;/ControlTemplate&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                &lt;/Setter.Value&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            &lt;/Setter&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        &lt;/DataTrigger&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;/Style.Triggers&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/Style&gt;</pre>
</p></div>
</div>
<p>As you can see I use the IsReadOnly property on the base view model as a data trigger and then completely swap out the control template for the text box. This allows for complete customization of the read only look. It also pushes this functionality out into a style to be reused through the entire application.</p>
<p>Other things to note with the project (for those unfamiliar with the following practices):</p>
<h5>1. Data Templates as Views</h5>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;DataTemplate DataType=<span style="color: #006080">&quot;{x:Type PersonView:PersonViewModel}&quot;</span>&gt;</pre>
</p></div>
</div>
<p>So whenever WPF tries to resolve the PersonViewModel in a container, it will use this DataTemplate since I did not provide a key but only a DataType.</p>
<p>So in Window1 I can do…</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;ContentControl&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;PersonView:PersonViewModel /&gt;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/ContentControl&gt;</pre>
</p></div>
</div>
<div>&#160;</div>
<div>Again this is a very simple scenario so I am ignoring any presentation patterns on how the ViewModel is placed or ‘shown’. Also ignoring how dependencies can be resolved, etc.</div>
<div>&#160;</div>
<div>And even though this is a content control, this works for items controls as well. So consider a stack of ‘views’. You could be accomplish this simply by placing a collection of ViewModels in an ItemsControl.</div>
<div>&#160;</div>
<div>But really… that’s for another post.</div>
<div>&#160;</div>
<h5>2. Expression for INotifyPropertyChanged</h5>
<p>I am also using a LambdaExpression to implement INotifyPropertyChanged. Essentially achieving safe notification and eliminating those nasty magic strings.</p>
<div>
<div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> FirstName</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    get { <span style="color: #0000ff">return</span> _firstName; }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    set</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _firstName = <span style="color: #0000ff">value</span>;</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        OnPropertyChanged(<span style="color: #0000ff">this</span>, m =&gt; m.FirstName);</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
</p></div>
</div>
<p>Anyway.. that was a quick project I threw together. I hope it was helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/samples/toggling-read-only-with-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Acase().for(patternType.FluentInterfaces).create()</title>
		<link>http://wizardsofsmart.net/patterns/acaseforpatterntypefluentinterfacescreate/</link>
		<comments>http://wizardsofsmart.net/patterns/acaseforpatterntypefluentinterfacescreate/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 21:53:05 +0000</pubDate>
		<dc:creator>rookieone</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Samples]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://wizardsofsmart.net/uncategorized/acaseforpatterntypefluentinterfacescreate</guid>
		<description><![CDATA[When I was first exposed to the idea of fluent interfaces my response was… big flipping deal. It wasn’t until I actively started using fluent interfaces in my code did I begin to see their power and elegance. What is a fluent interface? Here is the obligatory link to wikipedia In simple terms, its returning [...]]]></description>
			<content:encoded><![CDATA[<p>When I was first exposed to the idea of fluent interfaces my response was… big flipping deal.</p>
<p>It wasn’t until I actively started using fluent interfaces in my code did I begin to see their power and elegance.</p>
<p>What is a fluent interface? Here is the obligatory <a href="http://en.wikipedia.org/wiki/Fluent_interface" target="_blank" onclick="urchinTracker('/outgoing/en.wikipedia.org/wiki/Fluent_interface?referer=');">link</a> to wikipedia <img src='http://wizardsofsmart.net/wp/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  </p>
<p>In simple terms, its returning a type instead of void in order to achieve method chaining.</p>
<p>The end goal of the method chaining is to provide a more readable interface and code.</p>
<p>First, does it break the Law of Demeter?</p>
<p>Law of Demeter doesn’t equal the number of periods. I am of the opinion fluent interfaces do not violate the Law of Demeter.</p>
<p>What is LoD? Another <a href="http://en.wikipedia.org/wiki/Law_Of_Demeter" target="_blank" onclick="urchinTracker('/outgoing/en.wikipedia.org/wiki/Law_Of_Demeter?referer=');">link</a> to wiki and here is a <a href="http://stackoverflow.com/questions/67561/do-fluent-interfaces-violate-the-law-of-demeter" target="_blank" onclick="urchinTracker('/outgoing/stackoverflow.com/questions/67561/do-fluent-interfaces-violate-the-law-of-demeter?referer=');">link</a> to a stack overflow on this very subject! <img src='http://wizardsofsmart.net/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To me, Law of Demeter is about restricting communication between objects.</p>
<p>A common violation of LoD (imho) is the exposure of a list property on an object. I believe this serves as a good example of LoD as well as being an example on how to make a fluent interface. <img src='http://wizardsofsmart.net/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>A Person object has a name and a list of friends.</p>
<div class="csharpcode">
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> Person</pre>
<pre><span class="lnum">   2:  </span>    {</pre>
<pre class="alt"><span class="lnum">   3:  </span>        <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>
<pre><span class="lnum">   4:  </span>        <span class="kwrd">public</span> List&lt;Person&gt; Friends { get; set; }</pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span>        <span class="kwrd">public</span> Person()</pre>
<pre class="alt"><span class="lnum">   7:  </span>        {</pre>
<pre><span class="lnum">   8:  </span>            Friends = <span class="kwrd">new</span> List&lt;Person&gt;();</pre>
<pre class="alt"><span class="lnum">   9:  </span>        }</pre>
<pre><span class="lnum">  10:  </span>    }</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<pre class="alt"></pre>
</div>
<p>I could then add friends by…</p>
<div class="csharpcode">&nbsp;</div>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>var roger = <span class="kwrd">new</span> Person{ Name=<span class="str">"Roger"</span> };</pre>
<pre><span class="lnum">   2:  </span>roger.Friends.Add(<span class="kwrd">new</span> Person { Name = <span class="str">"Eddie"</span> });</pre>
<pre class="alt"><span class="lnum">   3:  </span>roger.Friends.Add(<span class="kwrd">new</span> Person { Name = <span class="str">"Freddie"</span> });</pre>
<pre><span class="lnum">   4:  </span>roger.Friends.Add(<span class="kwrd">new</span> Person { Name = <span class="str">"Gordie"</span> });</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Now this is pretty typical, so what’s the problem?</p>
<p>Well my code knows WAY too much about Person. I know it has a list of friends and if they change that list to say a dictionary, I am hosed.</p>
<p>Not only that, but poor Person class… people can add friends to his collection without him every knowing. What if some additional logic needs to be done when adding a friend? </p>
<p>What if the person wants to record the birth date of every friend so they can send out birthday cards? Person is SOL.</p>
<p>As for Law of Demeter, my code is using the Person object to communicate through to the List&lt;Person&gt;. I am exposing my code to changes in Person.</p>
<p>Let’s change Person to eliminate this problem.</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> Person2</pre>
<pre><span class="lnum">   2:  </span>    {</pre>
<pre class="alt"><span class="lnum">   3:  </span>        <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>
<pre><span class="lnum">   4:  </span>        <span class="kwrd">private</span> <span class="kwrd">readonly</span> List&lt;Person2&gt; _friends;</pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span>        <span class="kwrd">public</span> Person2()</pre>
<pre class="alt"><span class="lnum">   7:  </span>        {</pre>
<pre><span class="lnum">   8:  </span>            _friends = <span class="kwrd">new</span> List&lt;Person2&gt;();</pre>
<pre class="alt"><span class="lnum">   9:  </span>        }</pre>
<pre><span class="lnum">  10:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  11:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> AddFriend(Person2 friend)</pre>
<pre><span class="lnum">  12:  </span>        {</pre>
<pre class="alt"><span class="lnum">  13:  </span>            _friends.Add(friend);</pre>
<pre><span class="lnum">  14:  </span>        }</pre>
<pre class="alt"><span class="lnum">  15:  </span>    }</pre>
</div>
<p><style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Now the only way to add a friend is through Person. Yippie!</p>
<p>&nbsp;</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>var roger2 = <span class="kwrd">new</span> Person2 {Name = <span class="str">"Roger"</span>};</pre>
<pre><span class="lnum">   2:  </span>roger2.AddFriend(<span class="kwrd">new</span> Person2 { Name = <span class="str">"Eddie"</span> });</pre>
<pre class="alt"><span class="lnum">   3:  </span>roger2.AddFriend(<span class="kwrd">new</span> Person2 { Name = <span class="str">"Freddie"</span> });</pre>
<pre><span class="lnum">   4:  </span>roger2.AddFriend(<span class="kwrd">new</span> Person2 { Name = <span class="str">"Gordie"</span> });</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>&nbsp;</p>
</p>
</p>
</p>
<p>If I wanted to make this a fluent interface all I would need to do is instead of returning void, return the Person class. </p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> Person3</pre>
<pre><span class="lnum">   2:  </span>    {</pre>
<pre class="alt"><span class="lnum">   3:  </span>        <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>
<pre><span class="lnum">   4:  </span>        <span class="kwrd">private</span> <span class="kwrd">readonly</span> List&lt;Person3&gt; _friends;</pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span>        <span class="kwrd">public</span> Person3()</pre>
<pre class="alt"><span class="lnum">   7:  </span>        {</pre>
<pre><span class="lnum">   8:  </span>            _friends = <span class="kwrd">new</span> List&lt;Person3&gt;();</pre>
<pre class="alt"><span class="lnum">   9:  </span>        }</pre>
<pre><span class="lnum">  10:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  11:  </span>        <span class="kwrd">public</span> Person3 AddFriend(Person3 friend)</pre>
<pre><span class="lnum">  12:  </span>        {</pre>
<pre class="alt"><span class="lnum">  13:  </span>            _friends.Add(friend);</pre>
<pre><span class="lnum">  14:  </span>            <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>
<pre class="alt"><span class="lnum">  15:  </span>        }</pre>
<pre><span class="lnum">  16:  </span>    }</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Now I can do the same logic like…</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>var roger3 = <span class="kwrd">new</span> Person3 { Name = <span class="str">"Roger"</span> }</pre>
<pre><span class="lnum">   2:  </span>                            .AddFriend(<span class="kwrd">new</span> Person3 { Name = <span class="str">"Eddie"</span> })</pre>
<pre class="alt"><span class="lnum">   3:  </span>                            .AddFriend(<span class="kwrd">new</span> Person3 { Name = <span class="str">"Freddie"</span> })</pre>
<pre><span class="lnum">   4:  </span>                            .AddFriend(<span class="kwrd">new</span> Person3 { Name = <span class="str">"Gordie"</span> });</pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>That is pretty cool, but fluent interfaces really shine with the builder pattern.</p>
<p>This time around I want to force construction of a person through a builder and have the builder use a fluent interface. Notice my Person’s constructor and fields are private. I embed the builder class within the Person class so the builder has access to the constructor and fields. In this manner only my builder class has access to constructing a person. My builder can now act as an anti-corruption layer for Person.</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> Person4</pre>
<pre><span class="lnum">   2:  </span>    {</pre>
<pre class="alt"><span class="lnum">   3:  </span>        <span class="kwrd">private</span> <span class="kwrd">string</span> _name;</pre>
<pre><span class="lnum">   4:  </span>        <span class="kwrd">private</span> List&lt;Person4&gt; _friends;</pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span>        <span class="kwrd">private</span> Person4()</pre>
<pre class="alt"><span class="lnum">   7:  </span>        {</pre>
<pre><span class="lnum">   8:  </span>            _friends = <span class="kwrd">new</span> List&lt;Person4&gt;();</pre>
<pre class="alt"><span class="lnum">   9:  </span>        }</pre>
<pre><span class="lnum">  10:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  11:  </span>        <span class="kwrd">public</span> Person4 AddFriend(Person4 friend)</pre>
<pre><span class="lnum">  12:  </span>        {</pre>
<pre class="alt"><span class="lnum">  13:  </span>            _friends.Add(friend);</pre>
<pre><span class="lnum">  14:  </span>            <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>
<pre class="alt"><span class="lnum">  15:  </span>        }</pre>
<pre><span class="lnum">  16:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  17:  </span>        <span class="kwrd">public</span> <span class="kwrd">static</span> PersonBuilder createAPerson()</pre>
<pre><span class="lnum">  18:  </span>        {</pre>
<pre class="alt"><span class="lnum">  19:  </span>            <span class="kwrd">return</span> <span class="kwrd">new</span> PersonBuilder();</pre>
<pre><span class="lnum">  20:  </span>        }</pre>
<pre class="alt"><span class="lnum">  21:  </span>&nbsp;</pre>
<pre><span class="lnum">  22:  </span>        <span class="kwrd">public</span> <span class="kwrd">class</span> PersonBuilder</pre>
<pre class="alt"><span class="lnum">  23:  </span>        {</pre>
<pre><span class="lnum">  24:  </span>            <span class="kwrd">private</span> Person4 _person;</pre>
<pre class="alt"><span class="lnum">  25:  </span>            <span class="kwrd">public</span> PersonBuilder()</pre>
<pre><span class="lnum">  26:  </span>            {</pre>
<pre class="alt"><span class="lnum">  27:  </span>                _person = <span class="kwrd">new</span> Person4();</pre>
<pre><span class="lnum">  28:  </span>            }</pre>
<pre class="alt"><span class="lnum">  29:  </span>&nbsp;</pre>
<pre><span class="lnum">  30:  </span>            <span class="kwrd">public</span> PersonBuilder named(<span class="kwrd">string</span> name)</pre>
<pre class="alt"><span class="lnum">  31:  </span>            {</pre>
<pre><span class="lnum">  32:  </span>                _person._name = name;</pre>
<pre class="alt"><span class="lnum">  33:  </span>                <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>
<pre><span class="lnum">  34:  </span>            }</pre>
<pre class="alt"><span class="lnum">  35:  </span>&nbsp;</pre>
<pre><span class="lnum">  36:  </span>            <span class="kwrd">public</span> PersonBuilder withFriend(Person4 friend)</pre>
<pre class="alt"><span class="lnum">  37:  </span>            {</pre>
<pre><span class="lnum">  38:  </span>                _person.AddFriend(friend);</pre>
<pre class="alt"><span class="lnum">  39:  </span>                <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>
<pre><span class="lnum">  40:  </span>            }</pre>
<pre class="alt"><span class="lnum">  41:  </span>&nbsp;</pre>
<pre><span class="lnum">  42:  </span>            <span class="kwrd">public</span> PersonBuilder withaFriendNamed(<span class="kwrd">string</span> name)</pre>
<pre class="alt"><span class="lnum">  43:  </span>            {</pre>
<pre><span class="lnum">  44:  </span>                var friend = createAPerson().named(name).finish();</pre>
<pre class="alt"><span class="lnum">  45:  </span>                _person.AddFriend(friend);</pre>
<pre><span class="lnum">  46:  </span>                <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>
<pre class="alt"><span class="lnum">  47:  </span>            }</pre>
<pre><span class="lnum">  48:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  49:  </span>            <span class="kwrd">public</span> Person4 finish()</pre>
<pre><span class="lnum">  50:  </span>            {</pre>
<pre class="alt"><span class="lnum">  51:  </span>                <span class="kwrd">return</span> _person;</pre>
<pre><span class="lnum">  52:  </span>            }</pre>
<pre class="alt"><span class="lnum">  53:  </span>        }</pre>
<pre><span class="lnum">  54:  </span>    }</pre>
</div>
<p><style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
</p>
<p>Now to create Roger and his friends I do..</p>
<div class="csharpcode">&nbsp;</div>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>var roger4 = Person4.createAPerson()</pre>
<pre><span class="lnum">   2:  </span>                    .named(<span class="str">"Roger"</span>)</pre>
<pre class="alt"><span class="lnum">   3:  </span>                    .withaFriendNamed(<span class="str">"Eddie"</span>)</pre>
<pre><span class="lnum">   4:  </span>                    .withaFriendNamed(<span class="str">"Freddie"</span>)</pre>
<pre class="alt"><span class="lnum">   5:  </span>                    .withaFriendNamed(<span class="str">"Gordie"</span>)</pre>
<pre><span class="lnum">   6:  </span>                    .finish();       </pre>
</div>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>It’s a simple example but maybe it sparked some ideas on how to use fluent interfaces to chain methods and make your code more approachable to people without Computer Science degrees. <img src='http://wizardsofsmart.net/wp/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>JB</p>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/patterns/acaseforpatterntypefluentinterfacescreate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Future-proofing Office 2003 Custom Menu Development</title>
		<link>http://wizardsofsmart.net/samples/future-proofing-office-2003-custom-menu-development/</link>
		<comments>http://wizardsofsmart.net/samples/future-proofing-office-2003-custom-menu-development/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 12:30:56 +0000</pubDate>
		<dc:creator>panesofglass</dc:creator>
				<category><![CDATA[Samples]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Office]]></category>
		<category><![CDATA[Ron de Bruin]]></category>
		<category><![CDATA[VBA]]></category>
		<category><![CDATA[Worksheet Menu Bar]]></category>

		<guid isPermaLink="false">http://panesofglass.com/?p=32</guid>
		<description><![CDATA[More and more companies are slowly rolling out Windows Vista as they bring on new machines. However, if you&#8217;ve tried upgrading an Office 2003 document that uses custom menus, you will have noticed that those custom menus don&#8217;t appear in Office 2007&#8242;s new Ribbon. Well, that&#8217;s not entirely true; they do appear in the Add-Ins [...]]]></description>
			<content:encoded><![CDATA[<p>More and more companies are slowly rolling out Windows Vista as they bring on new machines. However, if you&#8217;ve tried upgrading an Office 2003 document that uses custom menus, you will have noticed that those custom menus don&#8217;t appear in Office 2007&#8242;s new Ribbon. Well, that&#8217;s not entirely true; they do appear in the Add-Ins tab. However, leaving users to figure that out for themselves is not a little unkind.</p>
<p>You might think your only option is to create two files&#8211;one for Office 2003 and previous and another for Office 2007. You would be wrong. Instead, you can use an Excel 2007 Add-In to extend the ribbon when the file is opened in Office 2007 and open the file as normal in Office 2003. <a href="http://www.rondebruin.nl/" onclick="urchinTracker('/outgoing/www.rondebruin.nl/?referer=');">Ron de Bruin</a> explains how to do this in his post &#8220;<a href="http://www.rondebruin.nl/compatiblemenu.htm" onclick="urchinTracker('/outgoing/www.rondebruin.nl/compatiblemenu.htm?referer=');">Dealing with Ribbons and Menus &#8211; Avoiding Two Versions</a>.&#8221;</p>
<p>To make this work, you&#8217;ll need the <a href="http://openxmldeveloper.org/articles/customuieditor.aspx" onclick="urchinTracker('/outgoing/openxmldeveloper.org/articles/customuieditor.aspx?referer=');">CustomUIEditor</a> from <a href="http://openxmldeveloper.org/" onclick="urchinTracker('/outgoing/openxmldeveloper.org/?referer=');">OpenXMLDeveloper.org</a>. Also, I&#8217;ve always been a big fan of class modules, so I have attempted to roll all this up into one class module, following Ron&#8217;s second example.</p>
<p>Here&#8217;s the code for the CustomMenu.cls class:</p>
<pre class="brush: vb">VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  &#039;True
END
Attribute VB_Name = &quot;CustomMenu&quot;
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
&#039;******************************************
&#039; Class:        CustomMenu
&#039;
&#039; Created by:   Ryan Riley (Catapult Systems)
&#039; Created on:   5/22/2008
&#039;
&#039; Description:  This class allows menus and menu items to be added
&#039;               to assist the user with additional reporting options.
&#039;
&#039; Dependencies: None
&#039;******************************************

Option Explicit

&#039;******************************************
&#039; Private Members
&#039;******************************************
Private mstrName As String            &#039; Use an &#039;&amp;amp;&#039; to assign a shortcut key.
Private mstrRibbonxPath As String     &#039; Store the Excel 2007 Add-in path.
Private mstrRibbonxName As String     &#039; Store the Excel 2007 Add-in filename.
Private mcolMenuItems As Collection
Private mcbc As CommandBarControl

&#039;******************************************
&#039; Class C&#039;tors and D&#039;tors
&#039;******************************************
Private Sub Class_Initialize()
    &#039; Initialize the Name property.
    mstrName = &quot;My Menu&quot;
    mstrRibbonxPath = ThisWorkbook.Path
    mstrRibbonxName = &quot;RibbonxAddin.xlam&quot;
    Set mcolMenuItems = New Collection
End Sub

Private Sub Class_Terminate()
    &#039; Destroy the menu item collection object.
    Set mcolMenuItems = Nothing

    &#039; Destroy the CommandBarControl object.
    Set mcbc = Nothing
End Sub

&#039;******************************************
&#039; Public Properties
&#039;******************************************
Public Property Get Name() As String
    Name = mstrName
End Property

Public Property Let Name(ByVal value As String)
    mstrName = value
End Property

Public Property Get RibbonxPath() As String
    RibbonxPath = mstrRibbonxPath
End Property

Public Property Let RibbonxPath(ByVal value As String)
    mstrRibbonxPath = value
End Property

Public Property Get RibbonxName() As String
    RibbonxName = mstrRibbonxName
End Property

Public Property Let RibbonxName(ByVal value As String)
    mstrRibbonxName = value
End Property

&#039;******************************************
&#039; Public Methods
&#039;******************************************
&#039; Add a menu to the document.
Public Function Attach( _
  Optional Before As Integer = 0) As Boolean
    &#039; Initialize the function return value.
    Attach = False
   
    If Val(Application.Version) &amp;gt; 11 Then
        If (Dir(mstrRibbonxPath &amp;amp; &quot;\&quot; &amp;amp; mstrRibbonxName) &amp;lt;&amp;gt; &quot;&quot;) Then
            Workbooks.Open mstrRibbonxPath &amp;amp; &quot;\&quot; &amp;amp; mstrRibbonxName
        Else
            MsgBox &quot;The RibbonX add-in (&quot; &amp;amp; mstrRibbonxName &amp;amp; &quot;) could not be found.&quot;
        End If
    Else
        AddMenu Before
    End If

    &#039; If all has gone well, return true.
    Attach = True
End Function

Public Function Detach()
    Detach = False

    If Val(Application.Version) &amp;gt; 11 Then
        On Error Resume Next
        Workbooks(mstrRibbonxName).Close False
    Else
        DeleteMenu
    End If

    Detach = True
End Function

&#039; Add menu items to the menu. Call this for each menu
&#039; item you wish to add to the menu.
Public Function AddItem( _
  ByVal Name As String, _
  ByVal Macro As String) As Boolean
    AddItem = False
   
    If (Val(Application.Version) &amp;gt; 11) Then
        &#039; Add the menu item to the private collection.
        mcolMenuItems.Add Macro, Name
    Else
        &#039; Add a menu item to our new menu, give it a caption,
        &#039; and tell it which macro to run (OnAction).
        With mcbc.Controls.Add(Type:=msoControlButton)
            .Caption = Name
            .OnAction = Macro
        End With
    End If

    AddItem = True
End Function

&#039; Define the function for all Ribbon controls to call when executed.
&#039; This method execute the button that matches the format:
&#039; &quot;buttonName&quot; where Name is the Menu Item name.
Public Function OnActionCall(MenuItem As Variant) As Boolean
    Dim RibbonItem As IRibbonControl

    &#039; Initialize the return value.
    OnActionCall = False

    &#039; Set the MenuItem to the RibbonItem as an implementation
    &#039; of the IRibbonControl.
    Set RibbonItem = MenuItem

    &#039; Run the macro that matches that stored in the collection.
    Application.Run mcolMenuItems(Mid(RibbonItem.ID, 7))

    &#039; If all goes well, return true.
    OnActionCall = True
End Function

&#039;******************************************
&#039; Private Methods
&#039;******************************************
Private Function AddMenu( _
  Optional Before As Integer = 0) As Boolean
    Dim cbMainMenu As CommandBar
    Dim intBeforeIndex As Integer

    &#039; Initialize the function return value.
    AddMenu = False

    &#039; Delete any existing menus with the same name.
    &#039; We must use On Error Resume next in case it does not exist.
    On Error Resume Next
    Application.CommandBars(&quot;Worksheet Menu Bar&quot;).Controls(mstrName).Delete
    On Error GoTo 0
   
    &#039; Set a CommandBar variable to use for the Worksheet menu bar
    Set cbMainMenu = Application.CommandBars(&quot;Worksheet Menu Bar&quot;)
   
    &#039; If the Before Index is &amp;gt; 0, set the Before Index to the passed
    &#039; in index; otherwise set it to the index for the last menu.
    If (Before &amp;gt; 0 And Before &amp;lt;= cbMainMenu.Controls.Count) Then
        intBeforeIndex = Before
    Else
        intBeforeIndex = cbMainMenu.Controls.Count
    End If

    &#039; Add &quot;My Menu&quot; to the Main Menu CommandBar, just before the last menu.
    &#039; Set a CommandBarControl variable to it
    Set mcbc = cbMainMenu.Controls.Add( _
        Type:=msoControlPopup, Before:=intBeforeIndex)
                     
    &#039; Set the menu control&#039;s caption to the provided name.
    mcbc.Caption = mstrName

    &#039; If all has gone well, return True
    AddMenu = True
End Function

Private Function DeleteMenu() As Boolean
    On Error Resume Next
   
    &#039; Initialize the function return value.
    DeleteMenu = False
   
    &#039; Remove the menu added earlier.
    Application.CommandBars(&quot;Worksheet Menu Bar&quot;).Controls(mstrName).Delete

    DeleteMenu = True
End Function</pre>
<p>As for the implementation, you could do something like this to get the same affect as Ron&#8217;s Example 2 (above):</p>
<pre class="brush: vb">Option Explicit

Const RibbonxAddin As String = &quot;HasRibbonX.xlam&quot;
Private menu As CustomMenu

Private Sub Workbook_Open()

    Set menu = New CustomMenu

    menu.Name = &quot;Test Tab&quot;
    menu.RibbonxPath = ThisWorkbook.Path    &#039; Default
    menu.RibbonxName = RibbonxAddin
    menu.Attach
    menu.AddItem &quot;A&quot;, &quot;BtnA&quot;
    menu.AddItem &quot;B&quot;, &quot;BtnB&quot;
    menu.AddItem &quot;C&quot;, &quot;BtnC&quot;
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    menu.Detach
   
    Set menu = Nothing
End Sub

Sub OnActionCall(MenuItem As Variant)
    menu.OnActionCall MenuItem
End Sub</pre>
<p>Using this approach, you&#8217;ll be able to add in your custom menu/ribbon for all users of your custom Office development.</p>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/samples/future-proofing-office-2003-custom-menu-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
