Wednesday, 04 November 2009

Getting a string value for an enum value is a common problem and there's a few solutions knocking around the Internet, however a few of these that I've seen end up having a utility class that handles mapping the enum value to the relevant string value, this is fine but I think this is an operation that should be callable on the enum value without having to go off and use another class. The solution I have here provides this functionality using extension methods that are available in .Net 3.5.

The first thing I did was to create a new attribute that will be applied to the enum values when they are declared so a string value can be specified for each enum value such as is shown in the code snippet below:

 

public enum 
{
    [StringValue("Red")]
    Red = 0,
    [StringValue("Blue")]
    Blue = 1,
    [StringValue("Green")]
    Green = 2
} 


The code for the attribute class is:

public class StringValueAttribute : Attribute 
{ 
    public string StringValue { get; private set; }

    public StringValueAttribute(string value) 
    { 
         this.StringValue = value; 
     } 
}

When writing the extension method we can take advantage of the fact that all enums have Enum as their base class. This allows the following extension to be defined.

public static stringGetStringValue(this Enum enumValue) 
{ 
   . . . 
}

This method is now available to be called as follows: 

Colors myColor = Colors.Red; 
Console.WriteLine(myColor.GetStringValue());

 

As the 'GetStringValue' extension method is available on all enums it leaves the possibility that GetStringValue could be called on enums that have not used the 'StringValue' attribute. To protect against this there are two options, option one would be to return an empty string and option two is to use reflection and return the name of the enum value. This was the option I choose, firstly I look for the 'StringValue' attribute if that is present the method returns that as this is the mechanism that allows the developer to take finer control of the string value. So the previous enum could be defined as follows:

 

public enum Colors 
{ 
        [StringValue("#FF0000")] 
        Red = 0, 

        [StringValue("#0000FF")] 
        Blue = 1, 

        [StringValue("#00FF00")] 
        Green = 2 
}

Likewise the enum could be defined as shown below and we would get the values 'Red', 'Green' and 'Blue' when using the 'GetStringValue' method.

public enum Colors 
{ 
        Red = 0, 
        Blue = 1, 
        Green = 2 
}

Here is my implementation of the extension method: 

public static string GetStringValue(this Enum enumValue) 
{ 
    Typetype = enumValue.GetType(); 
    FieldInfo fieldInfo = type.GetField(enumValue.ToString()); 

    StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes( 
        typeof(StringValueAttribute), false) as StringValueAttribute[]; 

    if(attribs.Length > 0) 
    { 
        return attribs[0].StringValue; 
    } 
    else 
    { 
        return fieldInfo.Name;  
     }  
}


I've a prebuilt DLL so all you need to do is download this and reference it in your projects making sure to include the namespace 'Gangleri.Enums' when you wish to use the extension method or the StringValue attribute.

DLL: http://enumstringvalue.googlecode.com/files/Gangleri.Enums.dll.zip
Source Code: http://code.google.com/p/enumstringvalue/source/checkout

posted on Wednesday, 04 November 2009 21:13:16 (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [1]

If you've got an account that uses Google mail you have got to try out the Gmail Notifier Plus app as it looks so good on Windows 7. Just look at the Jump Menu for the app. At a glance you can see how many new items are in your inbox and gmailNotifierPlusJumpMenu most recent 10 have their subjects listed on the Jump menu so that you can go directly to any of them with a single click. Also from the tasks section you have the option to compose a new mail or go to your inbox, both of these options will open a browser with the selected page displayed.

Another very nice feature of the app occurs when you mouse over the icon on the Windows 7 task bar. You get a small preview window that allows you to step through your mail and read the first few lines from each and for convenience you have the ability to open the full email in your browser.gmailNotifierPlusMouseOver Trust me if you have a Google mail account you want to check this little app out as it makes it so easy to work with your gmail.

posted on Wednesday, 04 November 2009 01:52:55 (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [1]
 Friday, 30 October 2009

windowsDiscImageBurner ImgBurn has always been a great solution for burning iso images on the Windows platform but with the final release of Windows 7 we now have "Windows Disc Image Burner". This allows you to simply right click on an iso file and select "Burn disc Image". This is a very simple program that does one thing and one thing only, burn your iso to a CD or DVD. ImgBurn still has the advantage of providing more features but if all your looking to do is burn an iso image to disc the built in functionality is more that sufficient.

posted on Friday, 30 October 2009 20:37:57 (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [0]
 Thursday, 29 October 2009

I've been doing some work with the Unity IoC container recently and had to configure some generic types so I thought I'd post some of the examples here so that I'd have some to refer back to more than anything else a blog post tends to be less likely to get lost that an old post-it note.

Xml config to configure mapping for a specific generic type, note the use of [[]] to define the generic type. Inside the square brackets you specify type name and the assembly containing the type. In both example `1 signifies the generic parameter:

<type type="Data.Repository.IRepository`1[[Poco.Address, Poco]], Data.Repository"
      mapTo="Data.Repository.LinqRepository`1[[Poco.Address, Poco]], Data.Repository">
  <lifetime type="singleton"></lifetime>
</type>

Xml config to configure open generic type mapping:

<typeAlias alias="IRepository`1"            
           type="Questern.ActiveOrderManagementSystem.Data.Repository.IRepository`1, 
Questern.ActiveOrderManagementSystem.Data.Repository
" /> <type type="IRepository`1" mapTo="Questern.ActiveOrderManagementSystem.Data.Repository.LinqRepository`1,
Questern.ActiveOrderManagementSystem.Data.Repository
"> <lifetime type="singleton" /> </type>

 

Thanks to Marceli for helping out with this!!

posted on Thursday, 29 October 2009 19:52:26 (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [1]
 Tuesday, 27 October 2009

SyncToy Logo Despite having a laptop I still find I'm often carrying documents between various computers on a USB pen so I and needed a convenient way to sync the changes made on the USB pen back onto my hard drive. To solve this problem I found SyncToy from Microsoft. This offers a really convenient way to keep my files in sync thanks to a simple and intuitive interface.

You can see just how clean the interface is from the screenshots. SyncToy 2.0 Main Screen All you need to do is click the "Create New Folder Pair" button. This produces another simple dialog that allows you to select the two folders you wish to keep in sync. Setup new folder pairing

Click then next button and you will be presented the the second of the three dialogs that setup the folder pairing. Here you have three radio buttons, "Synchronise", "Echo" and "Contribute". What I really found useful was the label to the right of the radio buttons that gave a clear and helpful description of what each of the radio buttons means. For example the explanation for the synchronize option reads "New and updated files are copied both ways. Renames and deletes on either side are repeated on the other side." As I will be working with code and will have new files, updates to existing files and possibly some deletes that I want kept in sync between my main computer and my pen drive this is the option I choose. However if you only want a one way synchronization choose "Echo". Or if you don't want deletes synchronized between the two folders select the "Contribute" option.

Specify actions for pairing

The last screen gives the option to enter a name for the pairing this is just a useful tag that will allow you to identify the pairing in the list of pairings you've created. Click finish and you will be presented with a summary screen that gives you all the details of what you have just created. Summary of pairing On this screen you should notice two actions that you can click on. The first "Change action." allows you to change what you are doing e.g. change from synchronize to echo. The second action link "Change Options." gives you the ability to take finer control of the synchronization. Here you can specify specific file types to include in the synchronization or files types that should not be copied as part of the synchronization.  Along with controlling the file types you have the option of excluding certain file types from the synchronization by clicking the "select subfolder" action link. The best thing about this screen is the level of help provided if required. Simply mouse over any of the controls and you'll get a clear and concise help message describing what the control is for and in the case of the free text entry fields used to control the file types the help gives examples of how to format what you enter into these fields. Options for the pairing

The last two buttons I'll take about are the "Run" and "Preview" these are clear cases of doing what it says on the tin. Run will run the synchronization and depending on the actions you have selected it will delete, copy, update and add the files and folder in one location to the other. However the preview is really nice as it gives you the ability to see what will happen as a result of clicking the "Run" button. You can see from the screenshot the results of running a preview on one of my pairings. Preview results showing what will happen when Run is clicked

This is a tool that I recommend anyone who works with USB pen drives go and checkout, it's easy to use, works well and so far has done all I want of it.

You can download SynToy free from Microsoft, at the time of writing 2.0 is the latest version.

 

 

http://www.microsoft.com/Downloads/details.aspx?familyid=C26EFA36-98E0-4EE9-A7C5-98D0592D8C52&displaylang=en

posted on Tuesday, 27 October 2009 18:54:52 (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [0]