S h o r t S t o r i e s

// Tales from software development

Archive for January 2011

Using delegates for properties in C# 2.0

leave a comment »

Occasionally I stumble across a scenario where the best solution for a problem would be to pass a property as an argument to a method.

I don’t mean the value of the property, I mean the property itself, i.e. a reference to the property Get and Set accessors. The way to pass a method as an argument is to use a delegate but if you try creating a delegate for a property in .NET 2.0 you’ll get an error message indicating that this is not supported. I’ve always assumed that this was the end of the story as far as .NET 2.0 is concerned.

I’ve just finished a project that implemented a generic interface between my company’s clinical data system and any ODBC data source. The need to have a property passed as an argument came up again and this time I spent a few hours investigating whether I’d overlooked a means to do this.

It turns out there is a way to do what I wanted. It’s not pretty but it does what I need and it enables me to write a common routine for handling a number of data items that I’d otherwise have had to have implemented as several almost identical blocks of code.

The solution is to pass the property accessor using an anonymous delegate.

As an example, consider a method that processes a database date/time value depending on its value might, or might not, set the value of a property. Typically, it would set the property value only if the database date/time value is not null. You’d want to call the method like this, with the third argument being the property:

this.SetDateTimeField(dataReader, columnIndex, person.DeceasedDate)

 
The SetDateTimeField method would check whether the specified column value is null and the process the value accordingly, for example:

if (dataReader.IsDBNull(columnIndex))
{
    person.DeceasedDate = dataReader.GetDateTime(columnIndex);
}

 
The problem is how to pass a delegate for the property. The C# compiler will not permit you define the method with delegate argument and pass a delegate for the property:

Error: 'Person.DeceasedDate' is a 'property' but is used like a 'method'

 
There is a way to do this but, as mentioned at the start of this post, it isn’t pretty. The solution is to use an anonymous method to set the property value. The method needs to define a delegate argument and the easiest way to do this is to make use of the System.Action<T> delegate like this:

private void SetDateTimeFieldValue(DataReader reader, int columnIndex, Action<DateTime> dateTimeProperty)

 
The call is made like this:

this.SetDateTimeFieldValue(reader, columnIndex, delegate(DateTime value) { person.DeceasedDate = value; });

 
Not a perfect solution but better than the alternatives.

Written by Sea Monkey

January 27, 2011 at 8:00 pm

Posted in Development

Tagged with

.NET configuration settings configSource attribute

leave a comment »

Periodically I need to include a config settings file in another config settings file and I always forget how to do it so I’m blogging about it so I know where to find the information the next time I forget and need to remember.

It’s the configSource attribute that allows you to include an external file in an application Settings config section. Here’s a quick example. The first file is the config file without an external file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Vitality.Import.Processor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <Vitality.Import.Processor.Properties.Settings>
            <setting name="ConnectionString" serializeAs="String">
                <value>Database=clinical;Data Source=neptune;User Id=vitality;Password=vitality</value>
            </setting>
        </Vitality.Import.Processor.Properties.Settings>
    </applicationSettings>
</configuration>

 
To move the Vitality.Import.Processor.Properties.Settings to an external file called ‘external.config’ you need to do this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Vitality.Import.Processor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <Vitality.Import.Processor.Properties.Settings configSource="external.config" />
    </applicationSettings>
</configuration>

 
And the external config will have the <Vitality.Import.Processor.Properties.Settings> element as its root:

<?xml version="1.0" encoding="utf-8" ?>
<Vitality.Import.Processor.Properties.Settings>
 <setting name="ConnectionString" serializeAs="String">
  <value>Database=clinical;Data Source=neptune;User Id=vitality;Password=vitality</value>
 </setting>
</Vitality.Import.Processor.Properties.Settings>

 
Note that the configSource attribute cannot be used to mix internal and external settings values, i.e. if the configSource attribute is specified then the *.Settings element cannot contain any child elements and all the settings will be read from the external config file.

Written by Sea Monkey

January 25, 2011 at 8:00 pm

Posted in Development

Tagged with

Why doesn’t Ubuntu Update Manager show an upgrade ?

leave a comment »

Having recently followed a series of upgrades in Ubuntu from 8.10 to 9.04 to 9.10 and finally to 10.04, I couldn’t understand why the upgrade to 10.10 didn’t show up in the Update Manager.

Ubuntu 10.10 was released nearly three months ago so why wasn’t it showing up when I ran the Update Manager ? I also wondered why the 10.04 release has the suffix ‘LTS’. It turns out the queries are related.

LTS stands for Long Term Support and guarantees that this release will be supported for three years. Because it’s a stable long term release, the default settings in the Software Sources configuration that control which upgrades are shown in the Update Manager are different from a non-LTS release and only LTS releases will be shown.

It’s easy enough to change this by starting the Software Sources app (from the Administration menu), clicking on the Updates tab and then changing the ‘Show new distribution releases’ setting from ‘Long Term Support (LTS) release only’ to ‘Normal releases’.

But you probably don’t want to do this – why would you want to upgrade a stable release that will be supported until 2013 for a minor upgrade that probably won’t be supported for more than a year or so…
The reason is that the updates shown in the Update Manager are controlled by the settings in the Software Sources app.
Because 10.04 is an Lts (Long Term Support) release, the default setting on the Updates tab of the Software Sources app is to show LTS upgrades only.

Written by Sea Monkey

January 20, 2011 at 8:00 pm

Posted in Environments

Tagged with

Tact filters

leave a comment »

Everyone knows that there are a lot of nerds working in the IT industry and that interactions between nerds and non-nerds are sometimes difficult. I’ve come across several instances where a particular technician was regarded by non-nerds as someone that they simply could not deal with.

Jeff Bigler wrote a brief essay fifteen years ago on why nerds and non-nerds don’t get on. It’s worth reading…

http://www.mit.edu/~jcb/tact.html

(Rob – thanks for the link.)

Written by Sea Monkey

January 18, 2011 at 8:00 pm

Posted in Comment

Tagged with

Dell Vostro 3700: Plugged in, not charging

with 6 comments

Last night I noticed that my Dell Vostro 3700 wasn’t charging. I’d unplugged and reconnected the power supply a few times during the course of the day and now, when I hovered the mouse over the power icon in the system tray, I saw the following message: 6% (plugged in, not charging)

This seemed a bit odd and I wondered why it wasn’t charging. I googled the message and found a number of forum posts describing similar symptoms and courses of action ranging from returning the power supply to Dell for replacement to a convoluted process of powering the laptop down, removing the battery, restarting the laptop and shutting it down, then putting the battery back in and rebooting again.

In my case, the ‘problem’ turned out to be user error. One of the Dell function keys on this particular model controls whether the battery should be charged or not. There’s a blue battery icon on the F2 key and pressing FN+F2 toggles between charging and not charging. This can be confirmed by right-clicking the tray icon and selecting Dell Battery Monitor. On the second tab, ‘Battery Life’, there’s a checkbox labelled ‘Disable battery charging’ which will either be checked or not depending on the current state of the FN-F2 function.

I’m guessing that Dell have added this feature so that you can disable battery charging if the laptop is used as a desktop replacement and always runs from AC power. Disabling the battery charging will probably prolog the life of the battery.

However, it seems a bit odd to have a function like this that is fairly obscure and easily (mis)used inadvertantly. I’m guessing that most users who accidentally put their laptops into a ‘not charging’ state won’t know why or how it happened and will assume that there’s a fault with their laptop or the power supply.

Written by Sea Monkey

January 11, 2011 at 8:00 am

Posted in Hardware

Tagged with

The Mac App Store opens

leave a comment »

The Mac App Store opened today. It could have easily been a disaster but everything seemed to work with only the occasional minor glitch.

The App Store might not seem that big a deal to non-Apple users but what’s really interesting about it is that it’s changing users’ expectations of how software should be bought and how much they should expect to pay for it.

The App Store brings ease of use for end users (e.g. automatic download and installation without administrator prompts, updates for installed applications from the App Store, etc.) , the reduced cost to users (more on this below), and a good return for developers (70% of the price).

The iPhone/iPad App Store has proved that there’s a lot of money to be made in selling a large volume of an application at a low price. Most apps are priced at a few dollars (with many being 99 cents).

Even though Apple doesn’t price it’s own apps this low, it is setting an example in showing that an app sold through the store, i.e. without the margins of a reseller and the costs of distribution, ought to be cheaper to the user.

Apple’s iWork Suite comprises Pages, Numbers, and Keynote and sells for £72 as a boxed retail product. Each app is available in the Mac App Store for £11.99 making a total of just under £36. So, Apple is discounting its own software by 50%. It’s difficult to imagine Microsoft being so generous.

Written by Sea Monkey

January 6, 2011 at 9:00 pm

Posted in Comment

Tagged with

Dell Vostro 3700: Cannot connect to WiFi network on channel 13

with 3 comments

I’ve been having some problems with my SqueezeBox losing its WiFi connection on a regular basis so I had a look at the support forums and followed a recommendation to try a different WiFi channel in case there was interference from other 2.4Ghz devices.

I’ve used Channel 11 on every home WiFi station I’ve ever used as it seems to give a reliable and consistent performance but I followed the suggestion and re-configured my router/modem to use channel 13. The connection problem I’d been having on the SqueezeBox immediately went away but was replaced with a new one – my Dell Vostro 3700 laptop could no longer see the WiFi network.

After a bit of poking around I think I’ve found the answer and it’s not good. The driver for the built-in WiFi card (DW 1520 half-mini card) pre-installed when I bought the laptop in August 2010 has a ‘Location’ parameter that allows one value only: USA

Channel 13 is valid in Europe but not in the USA so this is likely to be why my WiFi network is no loger detected. I assumed that the USA-only driver was an early release and that an updated driver would be available from the Dell web site but the latest driver available is the same release: 5.60.18.8.

Written by Sea Monkey

January 5, 2011 at 8:00 am

Posted in Hardware, Uncategorized

Tagged with