Pages

Thursday, June 12, 2008

WPF Namespace Poster


.NET Coding Guidelines

1. Tabs & Indenting

Avoid tab characters (\0x09) usage in code. 4 Space charactors should be use unsteade.

2 Bracing

Open braces should always be at the beginning of the line after the statement that begins the block. Contents of the brace should be indented by 4 spaces. For example:

if (someCondition)
{
DoSomething();
}
else
{
DoSomethingElse();
}

"case" statements should be indented from the switch statement like this:

switch (someExpression)
{

case 0:
DoSomething();
break;

case 1:
DoSomethingElse();
break;

case 2:
{
int n = 1;
DoAnotherThing(n);
}
break;
}

Braces should never be considered optional. Even for single statement blocks, you should always use braces. This increases code readability and maintainability of your code.

3 Single line statements

Single line statements can have braces that begin and end on the same line.

public class Foo
{
int bar;

public int Bar
{
get { return bar; }
set { bar = value; }
}

}

It is suggested that all control structures (if, while, for, etc.) use braces, but it is not required.

4 Commenting

Comments should be used to describe intention, logical as well as execution flow, algorithmic overview. It would be ideal, if from reading the comments alone, someone other than the author could understand a function's intended behavior and general operation. While there are no minimum comment requirements and certainly some very small routines need no commenting at all, it is hoped that most routines will have comments reflecting the programmer's intent and approach.

Copyright notice

Each file should start with a copyright notice. To avoid errors in doc comment builds, you don't want to use triple-slash doc comments, but using XML makes the comments easy to replace in the future. Final text will vary by product (you should contact legal for the exact text), but should be similar to:

//-----------------------------------------------------------------------
// <copyright file="ContainerControl.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

Documentation Comments

All methods should use XML doc comments. For internal dev comments, the <devdoc> tag should be used.

public class Foo
{

/// <summary>Public stuff about the method</summary>
/// <param name="bar">What a neat parameter!</param>
/// <devdoc>Cool internal stuff!</devdoc>
///
public void MyMethod(int bar) { … }

}

However, it is common that you would want to move the XML documentation to an external file – for that, use the <include> tag.

public class Foo
{

/// <include file='doc\Foo.uex' path='docs/doc[@for="Foo.MyMethod"]/*' />
///
public void MyMethod(int bar) { … }

}

UNDONE§ there is a big doc with all the comment tags we should be using… where is that?

Comment Style

The // (two slashes) style of comment tags should be used in most situations. Where ever possible, place comments above the code instead of beside it. Here are some examples:

// This is required for WebClient to work through the proxy
GlobalProxySelection.Select = new WebProxy("http://itgproxy/");

// Create object to access Internet resources
//
WebClient myClient = new WebClient();

Comments can be placed at the end of a line when space allows:

public class SomethingUseful
{
private int itemHash; // instance member
private static bool hasDoneSomething; // static member
}

5 Spacing

Spaces improve readability by decreasing code density. Here are some guidelines for the use of space characters within code:

  • Do use a single space after a comma between function arguments.
    Right: Console.In.Read(myChar, 0, 1);
    Wrong: Console.In.Read(myChar,0,1);
  • Do not use a space after the parenthesis and function arguments
    Right: CreateFoo(myChar, 0, 1)
    Wrong: CreateFoo( myChar, 0, 1 )
  • Do not use spaces between a function name and parenthesis.
    Right: CreateFoo()
    Wrong: CreateFoo ()
  • Do not use spaces inside brackets.
    Right: x = dataArray[index];
    Wrong: x = dataArray[ index ];
  • Do use a single space before flow control statements
    Right: while (x == y)
    Wrong: while(x==y)
  • Do use a single space before and after comparison operators
    Right: if (x == y)
    Wrong: if (x==y)

6 Naming

Follow all .NET Framework Design Guidelines for both internal and external members. Highlights of these include:

  • Do not use Hungarian notation
  • Do not use a prefix for member variables (_, m_, s_, etc.). If you want to distinguish between local and member variables you should use "this." in C# and "Me." in VB.NET.
  • Do use camelCasing for member variables
  • Do use camelCasing for parameters
  • Do use camelCasing for local variables
  • Do use PascalCasing for function, property, event, and class names
  • Do prefix interfaces names with "I"
  • Do not prefix enums, classes, or delegates with any letter

The reasons to extend the public rules (no Hungarian, no prefix for member variables, etc.) is to produce a consistent source code appearance. In addition a goal is to have clean readable source. Code legibility should be a primary goal.

7 Naming Conventions

Interop Classes

Classes that are there for interop wrappers (DllImport statements) should follow the naming convention below:

  • NativeMethods – No suppress unmanaged code attribute, these are methods that can be used anywhere because a stack walk will be performed.
  • UnsafeNativeMethods – Has suppress unmanaged code attribute. These methods are potentially dangerous and any caller of these methods must do a full security review to ensure that the usage is safe and protected as no stack walk will be performed.
  • SafeNativeMethods – Has suppress unmanaged code attribute. These methods are safe and can be used fairly safely and the caller isn't needed to do full security reviews even though no stack walk will be performed.

class NativeMethods
{
private NativeMethods() {}

[DllImport("user32")]
internal static extern void FormatHardDrive(string driveName);
}

[SuppressUnmanagedCode]
class UnsafeNativeMethods
{
private UnsafeNativeMethods() {}

[DllImport("user32")]
internal static extern void CreateFile(string fileName);
}

[SuppressUnmanagedCode]
class SafeNativeMethods
{
private SafeNativeMethods() {}

[DllImport("user32")]
internal static extern void MessageBox(string text);
}

All interop classes must be private, and all methods must be internal. In addition a private constructor should be provided to prevent instantiation.

8 File Organization

  • Source files should contain only one public type, although multiple internal classes are allowed
  • Source files should be given the name of the public class in the file
  • Directory names should follow the namespace for the class

For example, I would expect to find the public class "System.Windows.Forms.Control" in "System\Windows\Forms\Control.cs"…

  • Classes member should be alphabetized, and grouped into sections (Fields, Constructors, Properties, Events, Methods, Private interface implementations, Nested types)
  • Using statements should be inside the namespace declaration.

namespace MyNamespace
{

using System;

public class MyClass : IFoo
{

// fields
int foo;

// constructors
public MyClass() { … }

// properties
public int Foo { get { … } set { … } }

// events
public event EventHandler FooChanged { add { … } remove { … } }

// methods
void DoSomething() { … }
void FindSomethind() { … }

//private interface implementations
void IFoo.DoSomething() { DoSomething(); }

// nested types
class NestedType { … }

}

}

Wednesday, June 11, 2008

Selection in Visual Studio 2008 SP1 RTM (Multiple Control Formatting at it's best]

Last week, I have installed Visual Studio 2008 SP1 RTM and I am happy to bring a new feature in your knowledge [i.e. multiple selection and alignment operations in Design]. This feature has been introduced in Visual Studio 2008 SP1 RTM and Visual Web Developer Express 2008 SP1 RTM.

I do understand that designing pages using absolute positioning of elements is a tedious task and some people avoids it. However we have to been relying on the functionality that Visual Studio used to provide since VS 2002 and expected feature to stay in subsequent versions.



But the things are changing rapidly here. In VS 2008 Microsoft Visual Studio Product Group has replaced old IE based designer by a new designer that is shared with Expression Web and is based on former FrontPage technologies. But neither FrontPage nor Expression Web ever had multiple selection functionality in their releases, even Visual Interdev 98 is amongst the same page with these tools, Thus it took us some time to implement the feature in the new code base and it was not yet ready for SP1 Beta release.


Due to time constraints, not all functionality available in VS 2005 will become available in VS 2008 SP1. Here is what will be included:

1. Multiple selection using Ctrl+Click
2. Primary selection is indicated by white tab
3. Position menu with Absolute and Relative sub items
4. Align, Make Same Size and Order menus commands.
5. Ability to apply property to multiple controls in the Properties window
6. Delete multiple controls

Excluded Functionality :

1. Ability to drag multiple controls
2. Cut/Copy/Paste multiple elements

But its an interesting move: look at these pictures.




How to Implement, Transparency, Gradients and 3D Glass Effects on .NET Compect Framework.

Now mobile application developers can create more user friendly UI with .NET Compect Framework!!!

With the release of Microsoft® Windows Vista® and new development technologies such as Microsoft Silverlight™, .NET Micro Framework and Microsoft Windows® Presentation Foundation [WPF], expectations of what constitutes an acceptable application user interface on various Microsoft client platforms have never been higher. Since a long this move was expected from Microsoft as the company is aggresively capturing mobile deveolopment market, the Independent Software Vendor (ISV) creating software for Microsoft Windows Mobile® has always had to compete on features and functionality, but increasingly also faces competition on look and feel. Developers creating LOB (Line of Business) Windows Mobile solutions must now consider going beyond the out-of-the-box Windows Forms controls available in the .NET Compact Framework to create more compelling Windows Mobile–based application user interfaces that match the new graphic metaphors introduced in Windows Mobile 6.

Creating controls for .NET Compact Framework–based applications—which includes transparency, gradients, and three-dimensional glass-like appearance—is well within reach today, using existing Microsoft Windows Mobile development tools. This article will demonstrate how to achieve transparency, gradients, and glass effects by extending existing .NET Compact Framework controls and leveraging some powerful native graphics features available in the Windows Mobile operating system.

Sunday, June 01, 2008

Microsoft releases ASP.NET MVC [Model-View-Controller] Preview - 3

Last week (27th May 2008) , Microsoft released the latest preview version of its model-view-controller (MVC) architecture for Web application frameworks.

The debut of Version 3 was announced Tuesday on the blog of Scott Guthrie, corporate vice president of Microsoft's .NET Developer Division. This version includes many of the changes detailed in a post in April and several new ones, including improvements to the MVC, bug fixes, additions to HTML helper methods and a new URL routing engine that will also "ship in .NET 3.5 SP1 this summer," Guthrie wrote.

Another improvement he highlighted is a richer URL route mapping offering, including support for new characters.

Guthrie also commented on changes planned for upcoming versions. "In future preview releases, you'll start to see more improvements higher up the programming model stack in areas [such as] views (HTML helpers, validation helpers, etc.); AJAX; subcontrollers and site composition; deeper log-in, authentication, authorization and caching integration; as well as data scaffolding support," he wrote.

The preview is available for public download here. The source code is available here.