Friday, March 14, 2008

Resetting Intellisense in VS 2005

For some reason or another (well possibly due to uninstalling R#) intellisense had stopped working in Visual Studio (2005).

It's fairly easy to rest it. To do so enter the following commands into the console from the \progra files\Microsoft Visual Studio 8\Common7\IDE directory:

1) devenv /setup
2) devenv /resetuserdata
3) devenv /resetsettings CSharp

Restart VS after step 3 just in case.

This will clobber any customised settings such as fonts or colors you may have installed. For me it killed the TextMate theme I had installed. Worth it to have intellisense back!

Thursday, March 13, 2008

Twitter

So I've decided to experiment with Twitter.

I am "KiwiBastard" - www.twitter.com/kiwibastard

Feel free to follow me. Actually please do I only have one at the moment and that's my wife!

For those of you that don't know, Twitter is a service where you can update what you are up to, keep in touch with people all in 140 character posts. Has the power of social networking like Facebook, without the app Spam.

Wednesday, March 12, 2008

iPhone SDK. Initial Impressions

I have been playing with the iPhone SDK over the past few days. BTW they need a better name than that I think, it's not just for the iPhone. I think there will be just as many iPod touch users. Anyway I digress.

After the intial hiccup of the download server being over loaded, I managed to down the SDk a couple of days after the release. It's a whopping 2.1Gb which includes the actual SDK and other tools including an update to XCode to 3.1. That's a post in itself, but the code editor seems to be a lot nicer with inline error messages, code folding (that may have been in since 3.0 not sure) and little things like brace matching animations etc.

The actual SDK is broken in 4 layers. They are: Core OS, Core Services, Media and Cocoa Touch. I won't go into detail here but the main layer is the Cocoa Touch layer which includes the UIKit framework which handles UI and UI events such as gestures etc.

More info HERE

Initial Thoughts
First off, I am a hobbiest Objective-C programmer at best. While I am a very experienced C# developer, Objective-C is a little different. Having said that, I had an iPhone app running pretty quickly. Admittedly, the app only displayed a TableView and then on touch of a cell loaded another view, but it was easy. A few lines of code easy.
The SDK would be an impressive effort on a full blown desktop, but packing all the animation and UI features into a device the size of a phone is nothing short of amazing. The features the iPhone exposes through the SDK is nothing short of game changing. As a long suffering Windows Mobile developer, to say the iPhone is a generation ahead from a developers point of view is an understatement.

I think the clearest thing I can say is that a developer with a good idea isn't limited by the device. Except...

Issues
There are a couple. First off, your iPhone app exists in a "sandbox" mode. It can't access resources from other applications or files from outside it's realm.
Also, only one application can be running at once, and if the phone rings for example, your application will simply quit.
The last issue is syncing. There isn't at this stage inbuilt syncing functionality. There are a couple of options to get over this mostly involving communicating with a network layer or webservice. So your desktop app will have to expose it's data either locally or over the internet via a webservice. This does give the option of having a Windows app serving the data, but it would be nice to have a desktop syncing mechanism ala Active Sync.

The thing to remember is this is a Beta so hopefully some of the issues can be addressed before official release, but even so this is going to be big. I mean think of the games potiential alone. With the motion detection on the devices, it has the potiential to take hand held games to another level.

So as a conclusion to this ramble, I had great hopes for the SDK, and they have been met in almost every way.

Friday, March 07, 2008

iPhone SDK announced

The iPhone SDk has been announced by Apple. It is beta at the moment, with full release in June.

All apps will be published through iTunes with the developer getting 70% of revenues. Pretty fair I think.

This is going to be huge. If you don't believe me, try and download the SDK - the server is overloaded!

Thursday, March 06, 2008

Intoduction toExtension Method

One of the nice features in 3.5 is Extension Methods. Basically a side effect of LINQ, Extension methods offer the ability to create utility methods that are statically available to all classes. I'm not going to go into detail here, but there is plenty of information available on the net.

One utility class that most developers will have in there virtual rucksack is for Encrypting and Decrypting. In most cases encryption/decryption is done at variable level, for example encrypting/decrypting passwords or logins.

Creating an Extension Method to do this is a simple thing to do.

There are a couple of criteria that an EM must adhere to. Out of the Visual Studio help file : "Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive."

To achieve our goal we will create two EM's. One for encrypting some plain text and one for decrypting an already encrypted value back to plain text.

First we must create a new class. Make sure the class is marked static.

Our encryption EM will take a string parameter and return an array of byte as below:

public static byte[] ToEncryptedValue(this String unEncryptedValue)
{
   return ProtectedData.Protect(Encoding.Default.GetBytes
      (unEncryptedValue), null, DataProtectionScope.LocalMachine);
}

All the hard work of encryption is handled by the ProtectedData class. The DataProtection class is a .NEt wrapper for the Data Protection API (DPAPI). The beauty of using the DPAPI class is it illeviates the neccessity of generating and storing an encryption key, which can be an issue when dealing with security. The protect method takes a string, and a protection scope and returns an encrypted array of bytes.

Conversely, our decryption EM will take an array of bytes and return a string as below:

public static string ToUnEncryptedValue(this byte[] encryptedValue)
{
   return Encoding.Default.GetString(ProtectedData.Unprotect
      (encryptedValue, null, DataProtectionScope.LocalMachine));
}

again we use the ProtectedData class. The only caveat here is the data protection scope must be the same as the encryption scope.

Also, the encryption is tied to a machine. You can't take a password encrypt it on one machine and then try and decrypt it on another machine.

Extension Methods are called like any other class method, except they are called by variables themselves. For example to encrypt a variable called password:

byte[] encrypted = password.ToEncryptedValue();

and coversely to decrypt:

string plainText = encryptedArray.ToUnEncryptedValue();

Easy!

Hopefully from this the potential of extension methods are evident.

ASP.NET Tip #2 - Formatting a Caption on a GridView

This one is easy too, but also easy to overlook.

To format the Caption of a GridView, just add CSS to the Caption element of the css of your GridView. For example if you want to set the caption font to bold do the following:

.tipGrid Caption
{
   font-weight:bold;
}

[The above assumes you have set CssClass="tipGrid" on your GridView and you have a class called .tipGrid in your CSS file]

Wednesday, March 05, 2008

ASP.NET Tips #1 - Formatting Bound Columns to short date

I am back doing web development with my current contract, so thought I would post some quick tips as I came across things that may not be obvious.

This is fairly easy, but a good way to start. Say you have a date column that is displaying the time, but you only want to show the short date.

On the column set the DataFormatString to "{0:d}" and set HtmlEncode="false". Job Done.

Example: