Knowledge Share
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Knowledge Share

Knowledge is NOT Power IMPLEMENTATION of knowledge is Power!!!
 
HomePortalGalleryLatest imagesRegisterLog in

 

 Coding Conventions in .net

Go down 
AuthorMessage
Admin
Admin



Posts : 142
Points : 59554454
Reputation : 0
Join date : 2007-12-29
Location : Chennai

Coding Conventions in .net Empty
PostSubject: Coding Conventions in .net   Coding Conventions in .net Icon_minitimeMon Dec 31, 2007 12:04 pm

[C#]
TextBox t = new TextBox();
t.DataField = "AuthorID";
t.DataSource = "Publishers";
// The data-binding feature is now active.
You can also set a property to null (Nothing in Visual Basic) to indicate that the value is not specified.
[Visual Basic]
Dim t As New TextBox()
t.DataField = "AuthorID"
t.DataSource = "Publishers"
' The data-binding feature is now active.
t.DataSource = Nothing
' The data-binding feature is now inactive.
[C#]
TextBox t = new TextBox();
t.DataField = "AuthorID";
t.DataSource = "Publishers";
// The data-binding feature is now active.
t.DataSource = null;
// The data-binding feature is now inactive.
The following code example illustrates how to track the state of the data binding feature and automatically activate or deactivate it at the appropriate times.
[Visual Basic]
Public Class TextBox
Private m_dataSource As String
Private m_dataField As String
Private m_active As Boolean

Public Property DataSource() As String
Get
Return m_dataSource
End Get
Set
If value <> m_dataSource Then
' Set the property value first, in case activate fails.
m_dataSource = value
' Update active state.
SetActive(( Not (m_dataSource Is Nothing) And Not (m_dataField Is Nothing)))
End If
End Set
End Property
Public Property DataField() As String
Get
Return m_dataField
End Get
Set
If value <> m_dataField Then
' Set the property value first, in case activate fails.
m_dataField = value
' Update active state.
SetActive(( Not (m_dataSource Is Nothing) And Not (m_dataField Is Nothing)))
End If
End Set
End Property
Sub SetActive(m_value As Boolean)
If value <> m_active Then
If m_value Then
Activate()
Text = dataBase.Value(m_dataField)
Else
Deactivate()
Text = ""
End If
' Set active only if successful.
m_active = value
End If
End Sub
Sub Activate()
' Open database.
End Sub

Sub Deactivate()
' Close database.
End Sub
End Class
[C#]
public class TextBox
{
string dataSource;
string dataField;
bool active;

public string DataSource
{
get
{
return dataSource;
}
set
{
if (value != dataSource)
{
// Update active state.
SetActive(value != null && dataField != null);
dataSource = value;
}
}
}

public string DataField
{
get
{
return dataField;
}
set
{
if (value != dataField)
{
// Update active state.
SetActive(dataSource != null && dataField != null);
dataField = value;
}
}
}
void SetActive(Boolean value)
{
if (value != active)
{
if (value)
{
Activate();
Text = dataBase.Value(dataField);
}
else
{
Deactivate();
Text = "";
}
// Set active only if successful.
active = value;
}
}
void Activate()
{
// Open database.
}

void Deactivate()
{
// Close database.
}
}
In the preceding example, the following expression determines whether the object is in a state in which the data-binding feature can activate itself.
[Visual Basic]
(Not (value Is Nothing) And Not (m_dataField Is Nothing))
[C#]
value != null && dataField != null
You make activation automatic by creating a method that determines whether the object can be activated given its current state, and then activates it as necessary.
[Visual Basic]
Sub UpdateActive(m_dataSource As String, m_dataField As String)
SetActive(( Not (m_dataSource Is Nothing) And Not (m_dataField Is Nothing)))
End Sub
[C#]
void UpdateActive(string dataSource, string dataField)
{
SetActive(dataSource != null && dataField != null);
}
If you do have related properties, such as DataSource and DataMember, you should consider implementing the ISupportInitialize Interface. This will allow the designer (or user) to call the ISupportInitialize.BeginInit and ISupportInitialize.EndInit methods when setting multiple properties to allow the component to provide optimizations. In the above example, ISupportInitialize could prevent unnecessary attempts to access the database until setup is correctly completed.
The expression that appears in this method indicates the parts of the object model that need to be examined in order to enforce these state transitions. In this case, the DataSource and DataField properties are affected. For more information on choosing between properties and methods, see Properties vs. Methods.
Raising Property-Changed Events
Components should raise property-changed events if they want to notify consumers when the component's property changes programmatically. The naming convention for a property-changed event is to add the Changed suffix to the property name, such as TextChanged. For example, a control might raise a TextChanged event when its text property changes. You can use a protected helper routine Raise<Property>Changed, to raise this event. However, it is probably not worth the overhead to raise a property-changed event for a hash table item addition. The following code example illustrates the implementation of a helper routine on a property-changed event.
[Visual Basic]
Class Control
Inherits Component
Private m_text As String
Public Property Text() As String
Get
Return m_text
End Get
Set
If Not m_text.Equals(value) Then
m_text = value
RaiseTextChanged()
End If
End Set
End Property
End Class
[C#]
class Control: Component
{
string text;
public string Text
{
get
{
return text;
}
set
{
if (!text.Equals(value))
{
text = value;
RaiseTextChanged();
}
}
}
}
Data binding uses this pattern to allow two-way binding of the property. Without <Property>Changed and Raise<Property>Changed events, data binding works in one direction; if the database changes, the property is updated. Each property that raises the <Property>Changed event should provide metadata to indicate that the property supports data binding.
It is recommended that you raise changing/changed events if the value of a property changes as a result of external forces. These events indicate to the developer that the value of a property is changing or has changed as a result of an operation, rather than by calling methods on the object.
A good example is the Text property of an Edit control. As a user types information into the control, the property value automatically changes. An event is raised before the value of the property has changed. It does not pass the old or new value, and the developer can cancel the event by throwing an exception. The name of the event is the name of the property followed by the suffix Changing. The following code example illustrates a changing event.
[Visual Basic]
Class Edit
Inherits Control

Public Property Text() As String
Get
Return m_text
End Get
Set
If m_text <> value Then
OnTextChanging(Event.Empty)
m_text = value
End If
End Set
End Property
End Class
[C#]
class Edit : Control
{
public string Text
{
get
{
return text;
}
set
{
if (text != value)
{
OnTextChanging(Event.Empty);
text = value;
}
}
}
}
An event is also raised after the value of the property has changed. This event cannot be canceled. The name of the event is the name of the property followed by the suffix Changed. The generic PropertyChanged event should also be raised. The pattern for raising both of these events is to raise the specific event from the OnPropertyChanged method. The following example illustrates the use of the OnPropertyChanged method.
[Visual Basic]
Class Edit
Inherits Control
Public Property Text() As String
Get
Return m_text
End Get
Set
If m_text <> value Then
OnTextChanging(Event.Empty)
m_text = value
RaisePropertyChangedEvent(Edit.ClassInfo. m_text)
End If
End Set
End Property
Protected Sub OnPropertyChanged(e As PropertyChangedEventArgs)
If e.PropertyChanged.Equals(Edit.ClassInfo. m_text) Then
OnTextChanged(Event.Empty)
End If
If Not (onPropertyChangedHandler Is Nothing) Then
onPropertyChangedHandler(Me, e)
End If
End Sub
End Class
[C#]
class Edit : Control
{
public string Text
{
get
{
return text;
}
set
{
if (text != value)
{
OnTextChanging(Event.Empty);
text = value;
RaisePropertyChangedEvent(Edit.ClassInfo.text);
}
}
}

protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (e.PropertyChanged.Equals(Edit.ClassInfo.text))
OnTextChanged(Event.Empty);
if (onPropertyChangedHandler != null)
onPropertyChangedHandler(this, e);
}
}
There are cases when the underlying value of a property is not stored as a field, making it difficult to track changes to the value. When raising the changing event, find all the places that the property value can change and provide the ability to cancel the event. For example, the previous Edit control example is not entirely accurate because the Text value is actually stored in the window handle (HWND). In order to raise the TextChanging event, you must examine Windows messages to determine when the text might change, and allow for an exception thrown in OnTextChanging to cancel the event. If it is too difficult to provide a changing event, it is reasonable to support only the changed event.
Back to top Go down
https://knowledgeshare.forumotion.com
 
Coding Conventions in .net
Back to top 
Page 1 of 1
 Similar topics
-
» Coding Conventions in .net
» Coding Conventions in .net
» Coding Conventions in .net
» Coding Conventions in .net
» Coding Conventions in .net

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