Knowledge Share

Knowledge is NOT Power IMPLEMENTATION of knowledge is Power!!!
 
HomePortalGalleryCalendarFAQRegisterUsergroupsLog in

Share | 
 

 Coding Conventions in .net

View previous topic View next topic Go down 
AuthorMessage
Admin
Admin


Posts : 141
Points : 407
Reputation : 0
Join date : 2007-12-29
Location : Chennai

PostSubject: Coding Conventions in .net   Mon Dec 31, 2007 12:06 pm

Field Usage Guidelines
The following rules outline the usage guidelines for fields:
1. Do not use instance fields that are public or protected (Public or Protected in Visual Basic). If you avoid exposing fields directly to the developer, classes can be versioned more easily because a field cannot be changed to a property while maintaining binary compatibility. Consider providing get and set property accessors for fields instead of making them public. The presence of executable code in get and set property accessors allows later improvements, such as creation of an object on demand, upon usage of the property, or upon a property change notification. The following code example illustrates the correct use of private instance fields with get and set property accessors.
[Visual Basic]
Public Structure Point
Private xValue As Integer
Private yValue As Integer

Public Sub New(x As Integer, y As Integer)
Me.xValue = x
Me.yValue = y
End Sub

Public Property X() As Integer
Get
Return xValue
End Get
Set
xValue = value
End Set
End Property
Public Property Y() As Integer
Get
Return yValue
End Get
Set
yValue = value
End Set
End Property
End Structure
[C#]
public struct Point
{
private int xValue;
private int yValue;

public Point(int x, int y)
{
this.xValue = x;
this.yValue = y;
}

public int X
{
get
{
return xValue;
}
set
{
xValue = value;
}
}
public int Y
{
get
{
return yValue;
}
set
{
yValue = value;
}
}
}
Expose a field to a derived class by using a protected property that returns the value of the field. This is illustrated in the following code example.
[Visual Basic]
Public Class Control
Inherits Component
Private handle As Integer

Protected ReadOnly Property Handle() As Integer
Get
Return handle
End Get
End Property
End Class
[C#]
public class Control: Component
{
private int handle;
protected int Handle
{
get
{
return handle;
}
}
}
Use the const (Const in Visual Basic) keyword to declare constant fields that will not change. Language compilers save the values of const fields directly in calling code.
Use public static read-only fields for predefined object instances. If there are predefined instances of an object, declare them as public static read-only fields of the object itself. Use Pascal case because the fields are public. The following code example illustrates the correct use of public static read-only fields.
[Visual Basic]
Public Structure Color
Public Shared Red As New Color(&HFF)
Public Shared Green As New Color(&HFF00)
Public Shared Blue As New Color(&HFF0000)
Public Shared Black As New Color(&H0)
Public Shared White As New Color(&HFFFFFF)

Public Sub New(rgb As Integer)
' Insert code here.
End Sub

Public Sub New(r As Byte, g As Byte, b As Byte)
' Insert code here.
End Sub

Public ReadOnly Property RedValue() As Byte
Get
Return Color
End Get
End Property

Public ReadOnly Property GreenValue() As Byte
Get
Return Color
End Get
End Property

Public ReadOnly Property BlueValue() As Byte
Get
Return Color
End Get
End Property
End Structure
[C#]
public struct Color
{
public static readonly Color Red = new Color(0x0000FF);
public static readonly Color Green = new Color(0x00FF00);
public static readonly Color Blue = new Color(0xFF0000);
public static readonly Color Black = new Color(0x000000);
public static readonly Color White = new Color(0xFFFFFF);

public Color(int rgb)
{ // Insert code here.}
public Color(byte r, byte g, byte b)
{ // Insert code here.}

public byte RedValue
{
get
{
return Color;
}
}
public byte GreenValue
{
get
{
return Color;
}
}
public byte BlueValue
{
get
{
return Color;
}
}
}
2. Spell out all words used in a field name. Use abbreviations only if developers generally understand them. Do not use uppercase letters for field names. The following is an example of correctly named fields.
[Visual Basic]
Class SampleClass
Private url As String
Private destinationUrl As String
End Class
[C#]
class SampleClass
{
string url;
string destinationUrl;
}
3. Do not use Hungarian notation for field names. Good names describe semantics, not type.
4. Do not apply a prefix to field names or static field names. Specifically, do not apply a prefix to a field name to distinguish between static and nonstatic fields. For example, applying a g_ or s_ prefix is incorrect.


Parameter Usage Guidelines
Back to top Go down
http://knowledgeshare.forumotion.com
 
Coding Conventions in .net
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» open every link in a new tab?
» Banner inquiry
» XHTML - Lesson 5/5
» Pagination
» Are emails dying?

Permissions in this forum:You cannot reply to topics in this forum
Knowledge Share :: .Net-
Jump to: