Discussion on the use of attributes in .NET (1)
Codeprince http: //tangyong.net@163.com
I. VB.NET in the properties
When using the object-oriented idea design class, especially when using .NET development, we often want to display data on customer code, while ensuring that data is encapsulated in the class.
In this case, attribute is an ideal solution. The behavior of the property is like a data block in the class. The user can directly access the name of the attribute. Effect is equivalent to this class
Contains a data item with this name. In the specific implementation, the get attribute process acquires the attribute value; the set property process sets the value of the attribute. Public language specification in .NET Framework
(CLS) Support two different properties:
● Scalar properties
Scalar properties represent a single value. This value can be a simple value, such as char or string, or more complex values such as user-defined type objects such as DateTime.
● Index properties
Index properties represent a collection of values. Customer code uses the array syntax to access the value in the collection.
These two properties will be discussed in detail later. But remember: The property is a standard feature in .NET Framework. This means using any language developed with CLS-compatible language
The order code can be used in the properties defined in the class of VB.NET.
A actual example:
The following shows a simple property of the City class CityName.
Class City
Private mcityname as string
Public property cityname () AS STRING
Get
Return MCITYNAME
END GET
Set (byval value as string)
MCITYNAME = Value
End set
End Property
END CLASS
2. Scalar attribute
An example of the above shows the scalar attribute. Pay attention to the following points in the examples:
(1) The City class has a field MCITYNAME, which saves the city name. This field is called Private (private) to prevent customer code access. Object-oriented development
The purpose is to keep the encapsulation of the class. In other words: Do not say the public field.
(2) The city class has a property cityname to get and set up the city name. This property is a packaging of MCITYNAME. The cityname property is called PUBLIC, so it
Can be used in the customer's code. Most attributes tend to public, as attributes are essentially a convenient public interface for classes. However, private may also appear and
The protected property of the Protected property.
(3) When defining the cityName property, the specified cityname's properties type is displayed by adding a syntax AS String at the end of the Porperty statement. This is a good programming.
Habits; but in actual programming (such as me) Press the Enter key after entering cityName (), the system will also automatically add a grammatical segment of GET and SET, but the system is
The AS String will not be added at the end of the Property statement. That is, if you omit the return type, the default return type is Object (this is the most basic class of the system, all
Class (including user-defined classes) should be inherited in Object). The set property process needs to use a parameter to specify a new property value. The type of parameters must be the same as the properties. Should
The parameters must be defined as ByVal, which means that the value of the parameters passes through the value instead of passing the pass reference.
⑷ The cityName property is defined when defined, but there are many properties (including city names) in practice, but we need to access new values, then we
You can omit the set attribute, and use the readonly keyword for Property, just as shown below: Public Readonly Property CityName () AS STRING
Get
Return MCITYNAME
END GET
End Property
Of course, sometimes we can use the Writeonly keyword, then this property is writable. But only write properties are relatively few in the actual design.
⑸ When defining attributes, we also pay attention to the naming method of the property (although this is not the most important in the design process), a good naming method enables you to enjoy fast during the design process.
Le, but define the naming method of the attribute is more difficult than the case-sensitive language (such as C # et al.), I personally like to set MXXX (such as MCITYNAME) for the field, set the attribute to xxx.
(Such as CityName), choose what is not important, depending on the preferences of individuals.