Property Usage Guide

xiaoxiao2021-03-06  42

Determine is an attribute or method is more suitable for your needs. See Properties and Methods for more information on attributes and methods.

Select the attribute name based on the recommended attribute naming guide.

When using the SET Accessor Access Properties, reserve the value of the property before changing the property. This will ensure that the data will not be lost when the SET accessor is triggered.

Attribute status problem

Allows setting attributes in any order. For other properties, the attribute should be stateless. There is often a case where a particular function of the object is not taken to the developer specifies a particular set of properties or it takes effect until the object has a particular state. The function is not active unless the object is in the correct state. When the object is in the correct state, the function will automatically activate it itself without the need to call. Regardless of the order in which the developer sets the property, no matter how they make the object enter the active state, the semantics is the same.

For example, TextBox controls may have two related properties: DataSource and DataField. DataSource Specify Table Name, DataField Specifies the column name. Once the two properties are specified, the control automatically binds the data from the table to the TEXT property of the control. The following code example explains the properties set in any order.

[Visual Basic]

Dim t as new textbox ()

T. DataSource = "Publishers"

T.Datafield = "authorid"

'The data-binding feature is now active.

[C #]

TextBox T = New TextBox ();

T. DataSource = "Publishers";

T.Datafield = "authorid";

// The data-binding feature is now Active.

You can set the DataSource and DataField properties in any order. Therefore, the above code and the following code are equivalent.

[Visual Basic]

Dim t as new textbox ()

T.Datafield = "authorid"

T. DataSource = "Publishers"

'The data-binding feature is now active.

[C #]

TextBox T = New TextBox ();

T.Datafield = "authorid";

T. DataSource = "Publishers";

// The data-binding feature is now Active.

You can also set the properties to empty (Nothing in Visual Basic) to indicate an unregistered value.

[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 explains how to track the status of the data binding function, and how to automatically activate or deactivate it when appropriate.

[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 the

'Set The Property Value First, In Case Activate Fails.

m_datasource = value

'Update Active State.

SetACTIVE ((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 ((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 the

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.

}

}

The following expression in the above example determines if the object is in the data binding function, can activate its own state.

[Visual Basic]

(NOT (Value is nothing) and not (m_datafield is nothing))

[C #]

Value! = Null && Datafield! = NULL

You can automate the activation, the method is to create a method that determines whether the object can be activated when the object's current state is given, and then activate it as needed.

[Visual Basic]

Sub UpdateActive (M_DataSource As String, M_Datafield As String)

SetACTIVE ((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 implementation

ISupportInitialize interface

. This will allow the designer (or user) to call when set multiple properties.

ISupportInitialize.BeginInit

with

ISupportInitialize.endinit

Methods to enable components to optimize. In the above example, iSupportInitialize prevents unnecessable access to the database before the installation is successfully completed.

The expression that appears in this method indicates a portion that needs to be checked to force these state-converted object models. In this case, the DataSource and DataField properties will be affected. For more information on selecting between properties and methods, see Properties and Methods.

Current attributes have changed events

If the components are notified when their properties are programmed, they will raise the attribute that changes the event. The naming rule that the property has changed event is to add the Changed suffix to the attribute name, such as TextChanged. For example, the TEXTCHANGED event may be triggered when the control changes in its text attribute. The protected helper routine RAISE Changed can be used to raise the event. However, spending system overhead to initiate attributes for the added hash table, may not be worth it. The following code example interprets the implementation of the helper routine when the property has changed the event.

[Visual Basic]

Class Control

Inherits Component

Private M_Text As String

Public property text () AS Stringget

Return M_Text

END GET

Set

If not m_text.equals (value) THEN

m_text = value

RaisextChanged ()

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;

RaisextChanged ();

}

}

}

}

Data Binding Use this mode to allow both bidirectional bindings of the properties. If there is no Changed and Raise Changed events, the data binding will work in one direction; if the database changes, the property will be updated. Each attribute of each trigger CHANGED event should provide metadata to indicate that the property supports data binding.

If the value of the attribute changes due to external mandatory, it is recommended that you trigger a Changing / CHANGED event. These events indicate the value of the developer to the value of the property is being changed or changed by the method of operation instead of calling the object.

The Text attribute of the Edit control is a good example. When the user typed information into the control, the attribute value will be automatically changed. The event is triggered before the value of the attribute changes. It does not pass old values ​​or new values, and developers can cancel this event by triggeting exceptions. The name of the event is composed of the attribute name and composed with suffix. The following code example explains the 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 triggered after the attribute value changes. This event cannot be canceled. The name of the event is composed of the intrigin Changed after name. General PropertyChanged events will also be triggered. The model that triggers both events triggers a specific event from the onpropertychanged method. The following example illustrates the use of onPropertyChanged methods.

[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) ThenOnTextChanged (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 is a case where the base value of the property is not stored as a field, which makes it difficult to track changes. When you trigger a changing event, find all locations that you can change and provide the ability to cancel events. For example, the previous EDIT control example is not fully accurate because the TEXT value is actually stored in the window handle (hwnd). To trigger a TextChanging event, you must check the Windows message to determine when the text may change and allow an exception to cancel the event in onTextChanging to cancel the event. If you can't provide Changing events because it is too difficult, you only support the ChangeD event is reasonable.

Property and method

Class library designers must often decide to be implemented as an attribute or method for class members. Typically, the method represents the operation and the attribute represents data. Use the following guide to help you choose between these options.

Use properties when members are logical data members. In the following member statement, Name is an attribute because it is a logical member of the class. [Visual Basic]

Public Property Name As String

Get

Return M_Name

END GET

Set

m_name = value

End set

End Property [C #]

Public String Name

get

{

Return Name;

}

set

{

Name = value;

} How to use in the following cases:

The operation is conversion, such as Object.toString. Operation can cause a large number of system overheads, so that you want to notify users to consider cache results. Get the property value using the GET Accessor will generate an observable side effect. Continuous calls will have different results. The order in which the execution is very important. Note that you should be able to set and retrieve the properties of the type in any order. Members are static, but returns the value that can be changed. Members return an array. The attributes of the return array may be very easy to misunderstand. It is usually necessary to return a copy of the internal array to make the user unable to change the internal state. This situation is coupled to the user easily assume that it is an index property, which will result in inefficient code. In the following code example, each call for the Methods property creates a copy of the array. As a result, the 2N 1 copy of the array will be created in the following cycle. [Visual Basic]

DIM TYPE As Type = 'get a type.dim i as integer

For i = 0 to Type.Methods.Length - 1

If Type.Methods (i) .name.equals ("text") THEN

'Perform Some Operation.

END IF

Next i [c #]

TYPE TYPE = // Get A Type.

For (int i = 0; i

{

IF (Type.Methods [i] .name.equals ("text"))

{

// Perform Some Operation.

}

}

The following example interprets the correct usage of properties and methods.

[Visual Basic]

Class Connection

'The Following Three Members Should Be Properties

'Because The can be set in any order.

Property DNSName () AS STRING

'Code for Get and Set Accessors Goes Here.

End Property

Property Username () AS String

'Code for Get and Set Accessors Goes Here.

End Property

Property Password () AS String

'Code for Get and Set Accessors Goes Here.

End Property

'The Following Member SHOULD BE A Method

'Because the Order of Execution is import.

'This method cannot be Executed UnTil After the

'Properties Have Been Set.

Function Execute () as boolean

[C #]

Class Connection

{

// The Following Three Members Should Be Properties

// Because the can be set in any order.

String DNSNAME {Get {}; set {};

String username {get {}; set {};

String password {get {}; set {};

// the folload member sheld be a method

// Because the Order of Execution is important.

// this method cannot Be Executed UnTil After the THE

// Properties Have Been Set.

BOOL EXECUTE ();

}

Only read and write attributes

Read-only properties should be used when the user cannot change the logical data member of the attribute. Do not use only write properties.

Index properties

Note that the index property is also known as the indexer.

The following rules outline Guide to use index properties:

When the logical data member of the property is an array, use index properties. Consider using only integer values ​​or strings for index properties. If the design needs to use other types of index properties, reconsider if it represents a logical data member. Otherwise, use the method. Consider only one index. If the design needs to use multiple indexes, reconsider if it represents a logical data member. Otherwise, use the method. Use only an index property only for each class and make it a default index property of this class. This rule is forced to use indexer support in the C # programming language. Do not use non-default index properties. C # does not allow such situations. Name the index attribute Item. For example, see the DataGrid.Item property

. Follow this rule unless there is a more intuitive name for users, such as the CHARS attribute of the String class. In C #, the indexer is always named Item. Do not provide index properties and methods in semantically equivalent to two or more overload methods. In the following code example, the Method property will be changed to the getMethod method. Note that C # does not allow such situations. [Visual Basic]

'Change The MethodInfo Type.method Property to a Method.

Property Type.Method (Name As String) AS MethodInfo

Function Type.getMethod (Name As String, Ignorecase As Boolean) AS MethodInfo [C #]

// Change the methodinfo type.method property to a method.

MethodInfo Type.Method [String Name]

MethodInfo Type.getMethod (String Name, Boolean Ignorecase) [Visual Basic]

'The methodinfo type.method property is change to

'The methodInfo type.getMethod Method.

Function Type.getMethod (Name As String) AS MethodInfo

Function Type.getMethod (Name As String, Ignorecase As Boolean) AS MethodInfo [C #]

// the methodInfo type.method property is change to

// The methodInfo type.getMethod method.

MethodInfo Type.getMethod (String Name)

MethodInfo Type.getMethod (String Name, Boolean Ignorecase)

See

Class Bank Developer Design Guide | Property Name Guide | Class Members User Guide

Send an opinion about this topic

© 2001-2002 Microsoft Corporation. all rights reserved.

转载请注明原文地址:https://www.9cbs.com/read-63200.html

New Post(0)