Archive for April, 2009

Did you know? – Sharepoint Extensions for VS 2008 can be installed in XP

Did you know that “You can install Sharepoint Extensions for VS 2008 in Windows XP?”

Solution: In registry under

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions

Create a key called 12.0 and add a string value called “Sharepoint” with a value of “Installed” in it.

How can it be helpful: This is a hack which enables the sharepoint extension for VS 2008 to get installed in XP.

I got this hack from Janne Mattila’s blog. Thanks a bunch 🙂

Hope this helps.

Happy Programming!!!

Cheers,

Raja

Advertisements

Did You Know?

I was just wondering how it would be to have a “Did You Know” section in our blog. The objective of this section would be to have a max of 4 lines of text where in the first line would be the Title (duh), second line would be  Solution, third line would be how it could be useful and finally a simple example. Let me know what you think.

Let me start of by giving the DYK series # 1

Did You Know that “It is possible to have default Binding in WPF?”

Solution : Just specify the FallbackValue

How can it be helpful: Helps designers to see some value before the control gets bound.

Example :

<TextBox Margin="4" Text="{Binding Path=FirstName, FallbackValue=First name goes here}" FontStyle="Italic" Grid.Column="1"/>

Hope this helps.

Happy Programming!!!

Cheers,

Raja

Easy Dirty Checking with LINQ to SQL

dirtyDog

What is dirty checking?

Dirty checking is simply a way to check an object to see if it contains changes. This can be used to prevent unnecessary database updates. Properly implemented it should also account for the object changing back to its original state.

For example:

We change the spelling of a dog’s name “Spottie” to “Spotty". If we were to test the object it should return dirty.

Then if we decide the original spelling was correct and change it back to “Spottie” the object should no longer return dirty.

How we implemented it:

We wanted to extend our LINQtoSQL generated classes to implement dirty checking. All we had to do was extend the OnLoaded() method to call our copy constructor so that we had a private copy of the original object. Then we can simply check that private object against the current object state property by property to determine if the object was dirty.

partial class Dog
{
    private Dog _Original; 

    // Copy constructor.
    public Dog(Dog previous)
    {
        Name = previous.Name;
        Breed = previous.Breed;
        Age = previous.Age;
        Sex = previous.Sex;
        Weight = previous.Weight;
        Color = previous.Color; 
    } 

    //Here we copy the original object inside of itself for dirty checking
    partial void OnLoaded()
    {
        _Original = new Dog(this);
    } 

    //dirty checking using _Original from copy constructor
    public bool IsDirty
    {
        get
        {
            return
            !(
                this.Name == _Original.Name &&
                this.Breed == _Original.Breed &&
                this.Age == _Original.Age &&
                this.Sex == _Original.Sex &&
                this.Weight == _Original.Weight &&
                this.Color == _Original.Color 
            );
        }
    } 

}

-ctrlShiftBryan

Partial Methods

We all know about Partial Classes (in case you dont know read here) but I was intriqued when I look at LINQ to SQL generated designer.cs. I saw methods like

partial void OnLoaded();

partial  void OnCreated();

After a bit of researching I came to know that these are called as Partial Methods and their implementation is pretty neat. Check out this blog to get an overview about partial method implementation Or check out this blog for a detailed explanation about Partial methods.

Happy Programming!!!

Cheers,

Raja

Method Overloading in JQuery

This is a code script which I did while I was helping out a friend in his project. This is a pretty neat implementation of having overloaded methods in JQuery. For this to work I utilized a sweet method which John Resig wrote as given below:


// addMethod - By John Resig (MIT Licensed)
function addMethod(object, name, fn){
    var old = object[ name ];
    object[ name ] = function(){
        if ( fn.length == arguments.length )
            return fn.apply( this, arguments );
        else if ( typeof old == 'function' )
            return old.apply( this, arguments );
    };
}

Given below is how I utilized the addMethod of Johns to have a overloaded method implementation:


function UploadFileWithData() {
    addMethod(this, "upload", function(oframe, FileDescription, List){
       if (document.getElementById(oframe) != null) {

            ifUpload = document.getElementById(oframe);
            ifUpload.contentWindow.document.getElementById("hiddenDescription").value = FileDescription;
            ifUpload.contentWindow.document.getElementById("hiddenFilesList").value = List;
            ifUpload.contentWindow.document.getElementById("hiddenIsEditRequired").value = "0";
            ifUpload.contentWindow.document.getElementById("btnSubmit").click();
        }
        var oSection = document.getElementById("hSection");

        if (oSection != null) {

            oSection.value = List;
        }
    }
    );
    addMethod(this, "upload", function(oframe, FileDescription, List, AddEdit,Id, IsFileDeleted){
        if (document.getElementById(oframe) != null)
        {

        ifUpload = document.getElementById(oframe);
        ifUpload.contentWindow.document.getElementById("hiddenDescription").value = FileDescription;
        ifUpload.contentWindow.document.getElementById("hiddenFilesList").value = List;
        ifUpload.contentWindow.document.getElementById("hiddenIsEditRequired").value = "1";
        ifUpload.contentWindow.document.getElementById("hiddenSId").value = Id;
        ifUpload.contentWindow.document.getElementById("hiddenIsFileDeleted").value = IsFileDeleted;
        ifUpload.contentWindow.document.getElementById("btnSubmit").click();

        }
        var oSection = document.getElementById("hSection");

        if (oSection != null) {

            oSection.value = List;
        }
    }
    );
}

Hope it helps.

Happy Programming!!!

Cheers,

Raja

Top 5 Favorite Visual Studio Features

i-heart-vs

Visual Studio is such a powerful tool. I believe it is one of the major reasons why I enjoy .Net development over other platforms.

I thought I would share my top 10 favorite Visual Studio features with their keyboard shortcuts. The ones I couldn’t live without. Then I realized I could only come up with 5. This made me think I need to spend some time exploring the application I use more than any other application (Firefox not included) in my toolbox. So here are my top 5. Check back later for my “Top 5 Newly Discovered Features” post.

In no particular order…

-Find All (Ctrl+Shift+F)

Highlight a search term and you can search across the current document, all open documents, an entire solution or an entire project. Your results window will pop up and you’ll be able to view the line that contains the search term. Double click the line to go to it in code. I don’t know why everyone uses the classic find and next when this is just as easy but much more powerful.

-Comment\Uncomment selected lines (Ctrl+K, Ctrl+C), (Ctrl+K, Ctrl+U)

This has to be one of the most used features ever. It is so basic I wouldn’t consider an IDE without it an actual IDE. Without this you might as well be developing in notepad.

-Collapse\Open code (Ctrl+M, Ctrl+O), (Ctrl+M, Ctrl+L)

This one is really great. Assuming you aren’t using regions (which really can interfere with the usability\readability here) you can enter this shortcut chord (Ctrl+M, Ctrl+O) to collapse all properties and method details for a nice clean view of your class. Pressing (Ctrl+M, Ctrl+L) will open the whole class again or you can open individual section using the (+/-) toggle to the left.

-Refactor Rename (Ctrl+R, Ctrl+R)

Naming is huge in writing good code. Sometimes it just isn’t easy to find that perfect name when you first create something or a better name becomes apparent as you add additional items that have similar names. With this feature renaming a variable, method, property or class becomes 2 seconds of work. A lot of people may use search and replace but this actually uses the compiler to ensure it only renames what it should.

-Go to Definition (F12)

I remember when I first saw another developer use this and I couldn’t believe I had been working in VS for almost 6 months without knowing about it. Again it is so basic some may forget it is actually a feature but I’m sure if I captured my keyboard shortcuts it would be up at the top of the list of most used.

-ctrlShiftBryan