Galin Iliev's blog

Software Architecture & Development

C# 3.0 Partial Method Definitions

While I dug into Orcas March CTP Documentation I found another new C# feature (at least for me). It is called Partial Method Definitions and allows you to separate method definition and method implementation in partial classes.

//Definition

partial void partial void onNameChanged();

 

//Implementation

partial void onNameChanged()

{

  //method body

}

 

As I have C++ background this sounds familiar to me and reminded me times when I had to declare methods in .h (header) files and put implementation in .c/.cpp files.

To be honest I am not able to find the advantages of this new feature but I suppose there will be more blog post soon that will discuss this.

As I emphasize above you can separate method definition from implementation only in partial classes. There are number of restrictions stated in Orcas documentation:

  • Partial method declarations must begin with the contextual keyword partial and the method must return void.
  • Partial methods can have ref but not out parameters.
  • Partial methods are implicitly private, and therefore they cannot be virtual.
  • Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.
  • Partial methods can have static and unsafe modifiers.
  • Partial methods can be generic. Constraints are placed on the defining partial method declaration, and may optionally be repeated on the implementing one. Parameter and type parameter names do not have to be the same in the implementing declaration as in the defining one.
  • You can not make a delegate to a partial method.

 

JScript IntelliSense in Visual Studio "Orcas"

JavaScript IntelliSense in Visual Studio "Orcas" March CTP - WOW That's awesome!!!

The guys from VS Web tools team did a great job and employ xml comments to make JS Intellisense possible.

Take a look at the screenshots below

ASP.NET AJAX Concepts in Completion List

IntelliSense from Script Libraries for ASPX Pages

There are much more...

Did I get your interest? See the full post at Web Development Tools Team blog.

C# 3.0 Auto-Implemented Properties

I have downloaded VS Orcas March CTP and I am playing with its features now.

I found one very cool feature called Auto-Implemented Properties. This will allow you to create private field and public property accessor with single C# 3.0 line like this:

public string FullName { get; set; }

instead like in c# 2.0:

private string fullName;

 

public string FullName

{

    get { return fullName; }

    set { fullName = value; }

}

And as with the others cool language sugars the compiler will do the rest :). Actually it doesn't look that good but still it works. For the single line property above I got the following code in Reflector:

public string FullName

{

    [CompilerGenerated]

    get

    {

        return this.<>k__AutomaticallyGeneratedPropertyField0;

    }

    [CompilerGenerated]

    set

    {

        this.<>k__AutomaticallyGeneratedPropertyField0 = value;

    }

}

Nice huh?

Of course this won't work if you have complex getters and setters but still will speed up code writing and leave more time for thinking :)

Prevent multiple clicks on Submit button in ASP.NET

Recently I had to solve very common problem: When users what to submit the page by clicking on Submit button very often they become inpatient and click the button again which results in another button OnClick event firing (and if you save db record) duplicating records in database.

The most obvious solution is to use JavaScript to disable submit button. Unfortunately this easy solution is not the best because of number of reasons - see Andy Smit's blog entry you don't want to disable buttons after they are clicked here for more details.

So I decided to use another approach : I won't disable button with I will hide it and I show div element with text that would calm user down :). Thus buttons are visisble according ASP.NET but parent HTML element hides them :)

this is the aspx code that declares buttons


<div id="divButtons" style="display:inline;">
   <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="buttons" OnClientClick="SubmitButtonClicked();return true;" OnClick="btnSubmit_Click" style="padding-left: 10; padding-right: 10;"></asp:Button>

                   

   <input type="button" value="Cancel" onclick="javascript:top.hidePopWin(false);" style="padding-left: 10; padding-right: 10;"/>

</div>

 

<div id="divWait" style="display:none;">Note is being saved. Please wait respond from server...</div>                    

               


thus asp:Buttons controls fires SubmitButtonClicked(); javascript function before post the form.

Here is the js code:



function SubmitButtonClicked()

{

    if (typeof(ValidatorOnSubmit) == "function"){

        if (Page_ClientValidate() == false) {

            return false; }}

   

    $get("divButtons").style.display = "none";

    $get("divWait").style.display = "";

}


UPDATE: The function above is edited according the link below and all validators works as expected. :) Thanks John!

Although the bug I think it is good starting point.

You can see here for alternative solution - http://aspzone.com/blogs/john/articles/207.aspx