Friday, February 13, 2009

Ghost Doc

As we were talking about autocode documenting, you should try out GhostDoc which is a very simple and easy to use add-in for Visual Studio. 

Installation

Just download the ZIP file from the website

Run the installer MSI file.

image

Check Next

image

Agree and click Next.

image

Choose the default location or change it as required.  Click Next and Next Again

image

After a few seconds you should see the following to say Installation was successful.  click Close.

Using it within Visual Studio

Start visual Studio 2008 for the first time after installation you’ll see the following message.

image

Take the default option of <CTRL><SHIFT>D and click Assign.

image

Create a new default configuration for the rules (we can create one later if the developers feel like it’s a good product to use).  click Create and then Click Finish, Visual studio should then start as normal.

From within any application source code you can add a new comment to a function by placing your cursor on the first line.

image

In the above example the cursor is on line 19 for the button1_click.  Hold down <CTRL><SHIFT> and D, or whatever you assigned above.

image

The additional comment information is automatically added.

Why use GhostDoc

So what’s so different about Ghost Doc over the built-in functionality of just typing ‘///’? 

Well, if you provide good variable names it will actually come up with reasonably good comments automatically, for example above it knew “Click” was an Event, which would not happen in the built-in options.

Also you have a whole load of functionality around customising the language used or rules that can be applied.

image

To access the configuration area choose Tools/GhostDoc/Configure GhostDoc.

image

I’m not even going to try and set out the number of options you have here.

If nothing else it’s worth a look.

Monday, February 9, 2009

Getting to grips with LINQ

According to Microsoft LINQ is “a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities”.  What they actually mean is that its a very interesting way of doing Functional programming using C#.  I make the BIG distinction here between LINQ and LINQ/SQL (i.e. LINQ for SQL databases) as the latter is probably not something that will remain in the Framework long term.  It is probably going to be replaced with the ADO.NET Entity Framework

As an introduction you should download LinqPad which is an excellent tool for the beginner or experience functional programmer.  Not only does it provide a number of samples for a quick start, it also provides a test bed where you can run your queries directly against your data sources.

Installation Instructions

Download to your local machine the LinqPad.exe file from the website.

Double click the LinqPad icon and you should be presented with the application desktop.

image

Testing your queries

The easiest way to test your queries is to do it against a data source, so click the “Add Connection” on the left of the screen.

image

Use the options to connect to your database server and click OK.

Next you select the Database using the “Database” dropdown in the top right and then all you need to do is type your LINQ queries into the work area below. 

Simple where example

   from c in Companies
   where c.Comp_name.Contains("Iona Tech")
   select c

Pressing the play button will execute the query.

image

 

This example is against a SQL database but that is only for convenience there is no issue at all running queries against array or other collections and that is where the power of this technology can be seen.

Example of Parsing strings

    from word in "The quick brown fox jumps over the lazy dog".Split()
    orderby word.Length
    select word

Example working with arrays

    from n in new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }
    where n < 5
   select n

   - or simply -  

    (new[] {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 } ).Where (n => n < 5)

LINQ has a future for the .Net Framework, you only need to look at the developments with PLINQ and the Entity Framework to know that the syntax will start to move into day-to-day C# code.  Also I’d recommend having a review of the F# Development Centre, this is an extension for Visual Studio for developers who really, really, really like their functional programming.

Interesting examples

Here are some links to resources which will come in useful.

Thursday, February 5, 2009

PowerShell for hard core scripting

Microsoft have (finally) released a new scripting language for XP, Vista and its server Products called PowerShell.  This is a vast improvement on the bad old days of writing everything in DOS.  It will be installed by default in Windows 2008, however for earlier OS products you can get it from the Microsoft Site.

Installation

Here are the instructions for getting a copy.  Download and run the EXE from the Microsoft site and follow the online prompts.  Just choose the defaults for all the settings and agree the Licence Agreement.

image

This will add a new item to your Start menu, selecting the Windows Power Shell item will bring you into the PowerShell environment, which is much the same as the standard Command (CMD.EXE) window except you had access to a whole set of new functions and facilities.

image

Creating a basic Power Shell Script

You can create a Powershell script using notepad, Visual Studio or any text editor, however this is a little limited.  I’d recommend the free Power Shell GUI application which can be downloaded and installed.  This gives you IntelliSense some debugging facilities.  Just download and install selecting all the standard default.

image

When you start PowerGUI you get this friendly and basically useless console window.  Just ignore this until you get to the stage of wanting to add your own scripts to the tree.  Your main editor is available from the Tools/PowerGUI Script Editor option on the menu.

image

From here on you can just type your PowerShell scripts and and press the Play button to see it in action.

Power Shell Script Resources

There are numerous examples of good PowerShell scripts on the Internet here are a few I use:

Wednesday, February 4, 2009

Ajax ASP.NET Controls

A developer pointed me at an excellent set of .Net Ajax Controls which are available on CodePlex . Now all I need to do is find an excuse to use it.

Ever get sick of the waning messages from IE?

This message is very annoying on the servers when you just want to download some software.

image

Here are the instructions for removing it.

image

Go to Control Panel/Add Remove Programs and select Windows Components.  When the dialogue appears find the “Internet Explorer Enhanced Security Configuration” entry and uncheck the box and click Next.

image

Once this is completed you should click Finish.

The message should now not be displayed every time you attempt to access a website.

Monday, February 2, 2009

Visual Studio 2008 - Code Snippits

Well I'm back in Ireland and running amok again. Decided to note some old information as I'd forgotten most of these steps since the last time I'd used Visual Studio 2008 Code Snippits.

Creating a Code snippit is very easy as it's just an XML file using a spcific schema. Unfortunetly that schema is a little hard to remember, which is why I'm posting this information in the first place. Below is a simple example which should be saved as an XML file with the extension ".snippit" (e.g. "test.snippit"):

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Test Snippit</Title>
<Shortcut>snipTest</Shortcut>
<Author>Richard Greene</Author>
<Description>Test Code</Description>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>snipTest</ID>
<ToolTip>Add a hello world message</ToolTip>
<Default>Test</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[MessageBox.Show("Hello World");]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

To place this into your Visual Studio 2008 environment, you use the Code Snippit Manager from the Tools menu or the quick keys <CTRL>K <CTRL>B.



Click the Import button and browse to the the location you saved the XML file.

Select the location within the Manager where you want the snippit to appear, I've chosen the standard "My Code Snippets" folder and click Finish.


You should now be able to see the chosen snippet within the Manager.

To make use of this snippit simply within type the shortcut name (in this case "snipTest") within any C# file and Intelisence should display your snippet. Clicking TAB after selecting the snippet will insert the code block you are looking for.

This feature can be expanded greatly, with lots of flexibility for making areas read-only inserting default values etc., however I'll leave that up to you.

Tuesday, January 6, 2009

Creating a User Configurations on the iPhone

Reviewing some applications for the iPhone I noticed that some were making use of the Settings area to hold some User Configuration preference, which looked fantastic. I decided to investigate how this could be done in my own application and discovered that it is relatively east to create.

First within the XCode project we create a Settings.bundle file, by selecting File/New from the menu and selecting the Settings section on the left hand side of the dialogue.


This will create two new files in your project Root.strings and Root.plist. Open the Root.plist file by double clicking and you can see the editor dialogue appear with four default settings already entered under the 'PreferenceSpecifiers" section. You can modify or delete these as required.



There are a number of difference options open to the developer:
PSGroupSpecifier
This has only the one properties defined which is Title. This is used to group a number of related fields together.

PSTitleValueSpecifier
This displays a "Read Only" setting it cannot accept input from the user but can be used to display settings changed elsewhere.
Valid properties are: "Key", which is a string is used as a key to map to the value that a user will access the control. "DefaultValue" Another String property which stores the text that will be displayed in the specifier.

PSTextFieldSpecifier
Displays User enterable field. Properties include "Key" the name used to access the setting, "DefaultValue" a String property which is the initial value. "IsSecure" a boolean which defines if the charecters of the text field will be hidden, used for password fields. "KeyboardType" a String property which can have one of the following values: Alphabet, NumbersAndPunctuati on, NumberPad, URL, EmailAddress. This property specifies which type of keyboard to display when text field is selected. "AutocapitalizationType" a String property which can have one of the following values: None, Sentences, Words, AllCharacter. "AutoCorrectionType" string property which can have one of the following values: Default, No, Yes

PSSliderSpecifier
Displays a slider on a bar which allows the user to select a number in a specified range. "Key", a String property which holds the location of the control. "MinimumValue" and "MaximumValue" are numerical values which corresponds to the slider range. "DefaultValue" a numerical property which defines the first position of the slider.

PSToggleSwitchSpecifier
Displays a toggle switch which can be either On or Off. "Key", the name of the control. "TrueValue" and "FalseValue" defines the value of the control when it is in the On or Off position.". "DefaultValue", boolean property which defines the position of the Toggle button the first time it is loaded.

PSMultiValueSpecifier
Displays a list in a second view and allows the user to select one element from the list. "Key", defines the name of the key. "Values" of type Array which stores sub-elements of type String, each element provides a a possible value for the control to take. "DefaultValue" a String property that defines the initial value of the control, should be one of the elements of the Values list.

PSChildPaneSpecifier
Displays the contents of another Settings file which is useful if you want to break teh configuration up into various related sections. "Key", is the name of the control. "File" is the name of other plist file without the extension.


Testing the values in code
The simple way to read teh values in your application is to use the following
NSString *hostServicey_key = [[NSUserDefaults standardUserDefaults] stringForKey:@"hostService_key"];
Which should look through the settings field and retrieve the value of the property with the "Key" of "hostService_key".

I have however noticed that this value was being set to "nil" if the application was loaded onto the iPhone for the first time. The only way around this was to check the value before using it. If however the User goes into the Settings area first there is no problem.

// The URL of the Webserver
NSString *hostServicey_key = [[NSUserDefaults standardUserDefaults] stringForKey:@"hostService_key"];
//If the first value is nil, then we know that the defaults are not set.
if(hostServicey_key == nil)
{
//Get the bundle path
NSString *bPath = [[NSBundle mainBundle] bundlePath];
NSString *settingsPath = [bPath stringByAppendingPathComponent:@"Settings.bundle"];
NSString *plistFile = [settingsPath stringByAppendingPathComponent:@"Root.plist"];

//Get the Preferences Array from the dictionary
NSDictionary *settingsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFile];
NSArray *preferencesArray = [settingsDictionary objectForKey:@"PreferenceSpecifiers"];

//Loop through the array
NSDictionary *item;
for(item in preferencesArray)
{
//Get the key of the item.
NSString *keyValue = [item objectForKey:@"Key"];

//Get the default value specified in the plist file.
id defaultValue = [item objectForKey:@"DefaultValue"];

if([keyValue isEqualToString:@"hostService_key"])
hostServicey_key = defaultValue;
}
}