William Bartholomew

Musings on software engineering, technology and Aspergers Syndrome.

Archive for the ‘programming’ Category

Generating Option Strict On Using CodeDOM

with one comment

I was writing a VB.NET code generator this morning using System.CodeDOM and the default code that is generated includes “Option Strict Off” while I wanted “Option Strict On”. After some investigation I discovered that you can set this using:

outCodeCompileUnit.UserData("AllowLateBound") = False

Written by wbarthol

August 6, 2009 at 9:35 am

Posted in programming

Quick Excel Macro: Remove All Pictures

without comments

I pasted some content into Excel, and while I did want the formatting, I didn’t want the pictures it included. This meant I couldn’t use Paste Special.

Solution? A quick macro to remove all shapes from the sheet after I pasted:

Public Sub RemoveAllShapes()
    For Each e In Shapes
        e.Delete
    Next
End Sub

Written by wbarthol

July 31, 2009 at 10:32 am

Posted in programming

Snagit Accessory Installer

without comments

I’ve used Snagit from TechSmith for a year or so now and despite not being free it is without a doubt the best screen capture software I’ve used and I recommend it to anyone that asks me about screen capture software.This post actually isn’t about any of it’s screen capture capabilities but part of their installation. Snagit allows you to install what are called “accessories” (aka plugins) to add additional capabilities to the software. In fact, I’m writing this post with their WordPress.com accessory.

Plugins are certainly not a new concept by any stretch of the imagination, but what impressed me about TechSmith’s implementation is the installation process. Plugins aren’t normally a user-friendly concept because they usually involve putting files in special locations or doing special configuration.

TechSmith take away this complexity by registering a file extension (*.snagacc). These files are effectively a Zip file containing the DLLs and any dependencies of the plugin as well as a “manifest” containing information about the plugin. This file extension is registered to Snagit and the result is that simply double-clicking the *.snagacc file does all of the copying and configuration necessary for that plugin.

I’m impressed by the simplicity (for both the user and the plugin author) of this technique. The use of a renamed Zip file is now a common one and is used by Office 2007 for *.docx, *.pptx, *.xlsx, etc. and this is just another example of the usefulness of this technique.

Written by wbarthol

June 20, 2009 at 9:51 am

Posted in programming, utility

VSTO Deployment Error: The required version of the .NET Framework is not installed on this computer

without comments

I’ve been going crazy to try and solve a problem with a VSTO package I was publishing. Users were receiving the error “The required version of the .NET Framework is not installed on this computer” even though they had the correct .NET Framework version. It turns out that there is a file missing from the .NET Framework 3.5 installation in Windows 7 Beta/RC that causes any packages published from a Windows 7 Beta/RC machine to display this error.

For more details and the workaround read:

http://blogs.msdn.com/vsto/archive/2009/05/07/issues-with-installing-vsto-projects-that-were-published-from-visual-studio-2008-on-windows-7-rc-saurabh-bhatia.aspx#comments

Written by wbarthol

June 11, 2009 at 4:03 pm

Posted in dotnet, programming

Arrowhead Anti-Pattern

without comments

It is a common belief that methods should have a single entry and exit point. While this is a noble goal you need to be very aware that you can detract from the readability and comprehensibility of your code simply to uphold that belief. While a single exit point is not an anti-pattern striving to achieve a single exit point can result in deeply nested “arrowhead” code which is an anti-pattern.

My basic advice to you is that you should only strive for a single exit point from your method when it aids in the readability and comprehensibility of that method, if it doesn’t then don’t get caught up in having a single exit point.

http://www.lostechies.com/blogs/chrismissal/archive/2009/05/27/anti-patterns-and-worst-practices-the-arrowhead-anti-pattern.aspx

Written by wbarthol

May 31, 2009 at 7:54 pm

Posted in dotnet, programming

Validating XML files against an XML Schema (XSD)

without comments

UPDATE: You must set the ValidationFlags property to XmlSchemaValidationFlags.ReportValidationWarnings and you must add the event handler before calling Create on XmlReader, otherwise the event won’t be raised.

Validating XML files against an XML Schema (XSD) has changed slightly in .NET Framework 3.5 because the XmlValidatingReader has been obsoleted as have XmlReader’s constructors. The correct way to validate an XML file now is to:

  1. Create an XmlReaderSettings class, set the ValidationType to ValidationType.Schema, set the validation flags to report warnings, and add your schema to the Schemas collection.
  2. Add a handler for the ValidationEventHandler event, unusually this event is raised from the XmlReaderSettings class not the XmlReader class.
  3. Use the Create factory method on XmlReader passing in the path to the XML file and the XmlReaderSettings object you created above.
  4. Iterate through all of the nodes in the XmlReader (validation is done as the reader processes each node).
  5. Close the reader.
  6. Remove the event handler.

Here is an example:

Dim settings As New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas.Add("http://tempuri.org/MySchema.xsd", "MySchema.xsd"))
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings

Try
    AddHandler settings.ValidationEventHandler, AddressOf ValidationEventHandler 

    Using reader As XmlReader = XmlReader.Create("MyInstance.xml", settings)
        While reader.Read()
        End While

        reader.Close()
    End Using
Finally
     RemoveHandler settings.ValidationEventHandler, AddressOf ValidationEventHandler
End Try
...
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
    ...
End Sub

Written by wbarthol

May 31, 2009 at 6:27 pm

Posted in dotnet, programming

Unity 1.2 Hands-on Labs and Advertising Injectable Dependencies

without comments

Hand-on Labs for Unity 1.2 are now available (via J.D. Meier). Unity is a very simple and lightweight dependency injection container.

One of the features of Unity is the ability to define type aliases that allow you to define the full type name once and then use an alias to refer to it elsewhere in the configuration. For example:

<unity>
    <typeAliases>
        <typeAlias alias="IClipboard"
            type="MyApplication.Services.Clipboard.IClipboard, MyApplication" />
    </typeAliases>
</unity>

While working through the examples in the hands-on labs it occurred to me that this is a good way of advertising the different dependencies that can be injected. By defining a type alias for each of the dependencies that can be injected (both mandatory and optional) then developers wanting to extend your application can easily see the available dependencies.

For example:

<unity>
    <typeAliases>
        <!--Mandatory-->
        <typeAlias alias="IClipboard"
            type="MyApplication.Services.Clipboard.IClipboard, MyApplication" />
        <typeAlias alias="IStore"
            type="MyApplication.Services.Store.IStore, MyApplication" />

        <!--Optional-->
        <typeAlias alias="ILogger"
            type="MyApplication.Services.Logger.ILogger, MyApplication" />
    </typeAliases>
</unity>

Written by wbarthol

April 5, 2009 at 7:49 pm

Posted in dotnet, programming

Inside the Microsoft® Build Engine: Using MSBuild and Team Foundation Build

with one comment

I’m proud to say that after many months of work Sayed Ibrahim Hashimi and I have completed work on Inside the Microsoft® Build Engine: Using MSBuild and Team Foundation Build (PRO-Developer). It is being published by Microsoft Press and is due out on 7th January 2009.

Written by wbarthol

December 1, 2008 at 10:29 pm

Documentation Is There For A Reason

without comments

Michael Kaplan’s post “y? Because it’s documented that way!” raises a good point about undocumented behaviour. Basically, just because it works doesn’t mean you should, unless it’s documented that is. If you rely on undocumented behaviour you are exposing yourself to a higher level of risk when the API you use is revised.

Whether one looks at the Custom Date and Time Format Strings used by things like DateTime.ToString(string) or the strings in GetDateFormat or the strings in GetTimeFormat, it is clear that some characters (like H and M) have specific meaning attached to the cased variants.

In my book, since only the lowercase y is used here, the uppercase behavior is undefined — if it works then have fun but you really shouldn’t; if it fails then you kind of asked for that failure….

And more importantly, if the meaning changes in the future then that is also something requested in the improper use. and the nature of H vs. h and M vs. m kind of underscores this.

Written by wbarthol

November 25, 2008 at 10:28 pm

Posted in dotnet, programming

Installing SQL Server 2005 On Windows Vista/IIS 7

with one comment

While doing a SQL Server 2005 installation on Windows Vista I received a warning during the System Configuration Check stating that IIS was required although I already had IIS installed.

image5 

After some searching I found the following knowledgebase article http://support.microsoft.com/kb/920201 which describes how to resolve the warning.

Written by wbarthol

October 17, 2008 at 12:46 pm