June 2007 Entries
The Decorator Pattern is a simple pattern that allows the developer to add responsibilities to an object dynamically. This pattern is useful for extending classes that are locked by use of the "sealed" keyword. The UML diagram for this pattern looks something like this..  An example implimentation would be similar to... using System;
using System.Collections.Generic;
using System.Text;
namespace garyshort.org.patterns.decorator
{
//GS - Define our decorator
public class DecoratoredDateTime
{
protected DateTime _itemToDecorate;
/// <summary>
/// GS - Construct the decorator passing the object to decorate
/// </summary>
/// <param name="aDateTime">DateTime</param>
public DecoratoredDateTime(DateTime aDateTime)
{
_itemToDecorate = aDateTime;
}
/// <summary>
/// GS - Answer whether or not the decorated DateTime
/// is a holiday or not
/// </summary>
public bool isHoliday
{
get
{
//GS - Simulate a database lookup or similar here
Random rnd = new Random();
return (rnd.Next() % 2) == 0;
}
}
/// <summary>
/// GS - Answer the short date string of the
/// underlying DateTime
/// </summary>
/// <returns>string</returns>
public string ToShortDateString()
{
return _itemToDecorate.ToShortDateString();
}
}
/// <summary>
/// GS - Demonstrate adding the knowledge of whether or not a
/// DateTime is a holiday to the DateTime struct
/// </summary>
class Program
{
static void Main(string[] args)
{
//GS - Create a DateTime
DateTime aDateTime = DateTime.Now;
//GS - Create the decorator so we can add behaviour to
//the DateTime
DecoratoredDateTime aDecoratedDateTime =
new DecoratoredDateTime(aDateTime);
//GS - Ask the decorated DateTime if it is a holiday
//date or not
if (aDecoratedDateTime.isHoliday)
{
Console.WriteLine(aDecoratedDateTime.ToShortDateString() +
" is a holiday");
}
else
{
Console.WriteLine(aDecoratedDateTime.ToShortDateString() +
" is not a holiday");
}
//GS - Wait for the user to press a key before closing
//the DOS box
Console.Read();
}
}
}
Update: Of course the real power of this pattern lies in implementing interfaces so that multiple decorators can be applied to a decorated class. I didn't include that here, for brevity, but there is a good example provided by Greg.
If you are getting this error in VSTO 2005 SE using the 2003 PIAs it's because the Microsoft. Office. Interop. Excel. Worksheet class inherits both an Activate method and an Activate event, from the Microsoft. Office. Interop. Excel. _Worksheet and Microsoft.Office. Interop. Excel. DocEvents _Event interfaces, respectively. To fix this warning you have to make it more clear to the compiler which one you mean by casting the worksheet to the interface, like so... //GS - Go to sheet named SheetName
Worksheet ws = _inputWorkbook.Sheets[SheetName] as Worksheet;
(ws as Microsoft.Office.Interop.Excel._Worksheet).Activate();
Sometimes I really hate strongly typed languages; why can't we all just use Smalltalk? :-)
Recently both Dave Winer and Robert Scoble have have bemoaning the fact that the social networks out there don't allow the user to define how they want to describe a contact, nor do they let the user define the nature of the relationship with the contact, nor do they allow the user to import/export contacts from one network to the other. I've given this a little thought (and I do mean a little thought) and the solution is pretty straight forward actually. The database schema below should allow for the above functionality, all you need to do is to wrap it in some stored procedures, wrap them in some web services and present that in a nice AJAX enabled web site and there you have a pretty good Web 2.0 business. But hey, since I don't have the financial backing to do that (plus put the server infrastructure in place) I'll leave that for someone else to do, just remember me when you become a millionaire. ;-)  Just realised this would make a great SaaS business too. Hey, anyone out there with several million quid they'd like to front me? No? Hmm, didn't think so; sigh, guess I'm just ahead of my time. :-)
I've had a bit of trouble with my feeds (don't know when it started) but it's all fixed now. So, if you've tried to subscribe in the past and had problems, it should all work fine now. Sorry 'bout that. :-)
I see our local Police Chief Constable has been awarded a CBE in the Queen's birthday honours list. For what may I ask? "Mr Vine, who oversaw the security operation at the G8 summit in Gleneagles two years ago, received the award for services to policing." Source: BBC NEWS Um, excuse me for pointing this out, but doesn't he receive his salary for "services to policing"? These awards should be for charity work etc, actions above and beyond your working life for which, ordinarily, you would receive no recompense, not for doing the job you are paid to do.
The website of fellow Charterite William Wishart is currently showing this error... Now that's pretty funny :-) Technorati tags: Web, Error Pages, Humour
Looks like I'll be giving my Grok Talk (10 tools in 10 minutes) at Developer Day as well as my agile methods talk. So, it's shaping up to be a busy day, but I reckon it's going to be a blast; I can't wait!
This just in, via Craig Murphy's blog, the sign up for the Developer Day geek dinner is a here. Add your name to the wiki if you fancy coming along, it should be a good laugh.
If you are on a particular worksheet and you want to programmatically set the active cell to be a cell in another sheet, you may think you could do that by using the following code... ((Globals.ThisAddIn.Application.ActiveWorkbook.Sheets[
"SheetName"] as Worksheet).Cells[iRow, iCol]
as Range).Select();
But you'd be wrong. The above code generates the ultra-helpful "The select method on the Range object failed!". To make a cell on a non-current worksheet active you must first make the non-current worksheet active and then select the cell, like so... //GS - Make the required sheet active
Worksheet ws =
Globals.ThisAddIn.Application.ActiveWorkbook.Sheets[
fd.Sheetname] as Worksheet;
ws.Activate();
//GS - Select the required range in the now active sheet
(ws.Cells[fd.Row, fd.Column] as Range).Select();
Less than a month after launch and Popfly gets it's first set of updates. Check out Aaron's blog for details. Technorati Tags: Microsoft, Popfly
"Brian D. Kelly didn't think he was doing anything illegal when he used his videocamera to record a Carlisle police officer during a traffic stop. Making movies is one of his hobbies, he said, and the stop was just another interesting event to film. Now he's worried about going to prison or being burdened with a criminal record. " Video recording leads to felony charge Technorati Tags: Vlogging, Police, Felony
If, like me, you are lucky enough to have an invite to the alpha release of Popfly you may be interested in this... "Popfly Explorer is a Visual Studio 2005 add-in that enables you create, modify, and share Visual Studio solutions from your Popfly Space. As Popfly is currently in a private alpha, you must be a registered Popfly user to share projects to your Popfly Space." Microsoft Popfly Explorer Technorati Tags: Microsoft, Popfly
"More than 25 years ago, Allen Wirfs-Brock created one of the early implementations of Smalltalk. He was working at Tektronix at the time, as was Ward Cunningham who became the first user of Tektronix Smalltalk. Allen later served as chief scientist of Digitalk-ParcPlace and CTO of Instantiations, then joined Microsoft four years ago. His original charter was to work on future strategies for Visual Studio, but recently -- in light of growing interest in dynamic languages at Microsoft -- he's returning to his roots." Channel 9
"Police blame a woman named Butts for stealing toilet paper from a central Iowa courthouse, and while they're chuckling, the theft charge could put her in prison." Forbes.com You just can't write comedy like that! :-)
The SCRI at Invergowrie and an open day today as part of the LEAF program. Very interesting stuff. These guys, or guys like them, are going to end up saving the planet! How's that? Well did you know that you can use barley fields to extract carbon from the atmosphere? Very cool! You can see some photos here. Technorati tags: SCRI, LEAF
Dave Winer says... "With much of the UserLand mess settled, I’ve been having thoughts about doing more products, but then things pop up that remind me that it’s not as simple as just creating a product and marketing it, there are always people who show up wanting to take what you created for themselves." Source: Scripting News for 6/9/2007 « Scripting News Annex After reading this, I have one word for Dave, RSS.
In C#, if you want to iterate over a collection of something, you can use the following pattern to ensure that each element in the collection is visited once... foreach (object aThing in aCollectionOfThings)
{
aThing.DoWork();
}
However, if you are using VSTO 2005 SE you have to be careful because if you attempt to access a collection which is empty you do not get an empty collection returned; instead a COM error is thrown. This being the case, you should use the following pattern to iterate over a collection... try
{
foreach (Range aRange in aCell.Precedents)
{
//Do work with aRange
}
}
catch { } //Ingnore the COM error
finally
{
//Do stuff that needs to be done regardless of errors or otherwise.
}
Technorati tags: VSTO 2005 SE
Over on his blog Barry Carr has a post about the upcoming meeting of eXtreme Tayside (13/6/07) where he'll be giving a demo of Ruby on Rails development on a Mac. I'm going to be late as my train doesn't get in until 7:30 so I hope I don't miss the best bits ;)
Looks like the idea of having a Scottish based developer day event is gaining support. :-)
Okay so you've created a custom menu in Excel and you've wired up the menu items to your code. You've tested it and it works - first time. Click the menu item again however and nothing happens. Why not? Well, if you've used the code in my previous post (or something similar) then the menu items have gone out of scope after the first click. Arrrrggg! To prevent this, you have to create a class level property to hold your menu items, like so... const string MENU_CAPTION = "MyTestMenu";
Office.CommandBarButton XmlFormLoadermenuItem;
Office.CommandBarButton FormulaWalkerFormmenuItem;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
//GS - Get a hold of the menubar
Office.CommandBarControls cbc =
this.Application.CommandBars[1].Controls;
//GS - Create a new menu for our addin to use
Office.CommandBarPopup newMenu =
(Office.CommandBarPopup)cbc.Add(
Office.MsoControlType.msoControlPopup, Type.Missing,
Type.Missing, cbc.Count, true);
//GS - Give the menu a sensible caption
newMenu.Caption = MENU_CAPTION;
//GS - Add a menu item to open the Xml input loader form
XmlFormLoadermenuItem =
(Office.CommandBarButton)newMenu.Controls.Add(
Office.MsoControlType.msoControlButton,
Type.Missing, Type.Missing, Type.Missing, true);
//GS - Caption the menu item
XmlFormLoadermenuItem.Caption = "Item 1...";
XmlFormLoadermenuItem.Tag = "1";
//GS - Wire up the click event
XmlFormLoadermenuItem.Click += new Office.
_CommandBarButtonEvents_ClickEventHandler(
XmlFormLoadermenuItem_Click);
//GS - Add a menu item to open the formula walker form
FormulaWalkerFormmenuItem =
(Office.CommandBarButton)newMenu.Controls.Add(
Office.MsoControlType.msoControlButton,
Type.Missing, Type.Missing, Type.Missing, true);
//GS - Caption the menu item
FormulaWalkerFormmenuItem.Caption = "Item 2...";
FormulaWalkerFormmenuItem.Tag = "2";
//GS - Wire up the click event
FormulaWalkerFormmenuItem.Click +=
new Office._CommandBarButtonEvents_ClickEventHandler(
FormulaWalkerFormmenuItem_Click);
}
It took me a while to work out how to do this, so I'll post it here to save you the bother. ;-) //GS - Get a hold of the menubar
Office.CommandBarControls cbc =
this.Application.CommandBars[1].Controls;
//GS - Create a new menu for our addin to use
Office.CommandBarPopup newMenu =
(Office.CommandBarPopup)cbc.Add(
Office.MsoControlType.msoControlPopup, Type.Missing,
Type.Missing, cbc.Count, true);
//GS - Give the menu a sensible caption
newMenu.Caption = "&Model Input";
//GS - Add an item to the menu
Office.CommandBarButton menuItem =
(Office.CommandBarButton)newMenu.Controls.Add(
Office.MsoControlType.msoControlButton,
Type.Missing, Type.Missing, Type.Missing, true);
//GS - Caption the menu item
menuItem.Caption = "&Open";
//GS - Wire up the click event
menuItem.Click += new Office.
_CommandBarButtonEvents_ClickEventHandler(menuItem_Click);
A colleague pointed my to this video showing that it is possible to have the best of both worlds ;-)
As you may or may not be aware, every year the Microsoft developer community gets together at Microsoft's UK headquarters at Thames Valley Park in Reading (more information here). Recently someone suggested we may like to organise a similar thing here in Scotland. If you are interested then note your interest on this blog post.
What a great piece of technology, who do I have to kill to get one? Check out Scoble YouTubing with one, fantastic!! Now imagine how much fun a vlogger like me could have at Developer Day with one of those. ;-) Technorati Tags: Nokia N95
Here's some screen shots of my first Popfly mashup. Technorati Tags: Popfly, Microsoft
Technorati Tags: Silverlight, Popfly
Along with two other Charteris guys (Barry Dorrans and Chris Seary) I'll be speaking at this year's Developer Day (on the subject of agile methods). If you want to know what Developer Day is all about, then pop across to Craig Murphy's blog where he has a short trailer video available for download.
I hope Julie is back up soon. :-(
... that well, they operate on a budget. Today we were supposed to be traveling to Birmingham for a Charteris Day company gathering, and by we, I mean the whole of the team (well almost all) from the Edinburgh office. We arrived at Edinburgh airport to discover that our 08:45 flight to Birmingham was delayed until 14:15 due to, wait for it, crew shortages! I took that to mean they had no one to fly the thing. Gee, you mean if you want to run a scheduled route between Edinburgh and Birmingham, you actually need someone there to fly the darn thing? Tut, if only they could have foreseen that one eh? So, needless to say, we didn't hang around until 14:15 to fly to Birmingham for the day. Pity, being the new boy, I was looking forward to meeting the folks from down south. Ah well, there's always the next one eh?
|