Differences and usage of class fields and attributes in VB

Source: Internet
Author: User

A class consists of fields, attributes, methods, and events. Fields and attributes indicate the information contained in the object. Fields are similar to variables because they can be directly read or set. For example, if there is an object named car, you can store its color in the field named color. The retrieval and setting methods of attributes are similar to those of fields, but attributes are implemented using the property get and property set processes. These processes provide more control over how to set or return values. The indirect layer between the stored value and the process using this value helps isolate data and enables you to verify these values before they are assigned or retrieved.

Add fields to a class
Declare a public variable in the class definition, as shown below:CodeAs shown in:

Class Thisclass
Public Thisfield As   String  
End Class  

Add attributes to a class
Declare a local variable in the class to store the attribute value. This step is required because the attribute does not allocate any storage area on its own. To protect their values from being directly modified, the variables used to store attribute values should be declared as private.
Start with a modifier (such as public and shared) as needed. Use the property keyword to declare the property name and the Data Type stored and returned by the property.
Define the get and set attributes in the attribute definition. The get attribute process is used to return the property value, which is basically equivalent to a function in the syntax. They do not accept parameters and can be used to return the value of a private local variable declared in the class for storing attribute values. The set attribute process is used to set the attribute values, which have parameters (usually called values). The data type of this parameter is the same as that of the attribute. Each time the attribute value is changed, the value is passed to the set attribute process. During this process, you can verify it and store it in a local variable.
Use the end get and end set statements to terminate the get and set attribute processes as needed.
Use the end property statement to terminate the attribute block.
Note: If you are working in the integrated development environment (IDE) of Visual Studio, You can instruct it to remove the empty GET and set attribute processes. Type property propname as datatype (where propname is the property name and datatype is a specific data type, such as integer). The corresponding attribute process will appear in the code editor.
The following example declares an attribute in the class:

Class Thisclass
Private M_propval As   String  
Public   Property One () As   String  
Get  
Return M_propval ' Return the value stored in the local variable.
' Optionally, you can use the syntax one = propval to return
' The property value.
End   Get  
Set ( Byval Value As   String )
M_propval = Value ' Store the value in a local variable.
End   Set  
End Property  
End Class  

When you create an instance of thisclass and set the value of the one attribute, the set attribute process is called and the value is passed in the value parameter. this parameter is stored in a local variable named m_propval. When this property value is retrieved, the get property procedure will be called like a function and the value stored in the local variable m_propval will be returned.

Attribute and attribute Process
Attributes and fields can be used to store information in objects. The attribute uses the attribute process to control how to set or return values, while the field is only a public variable. The attribute process is a code block declared in the attribute definition. This allows you to execute code when setting or retrieving attribute values. Visual Basic. Net has two types of attribute processes: Get attribute process is used to retrieve attribute values; set attribute process is used to assign values to attributes. For example, the attributes for storing bank account balances may use code during the get attribute process to charge interest and check service fees before returning the available balance. The set attribute process for available balances provides verification code to prevent the balance from being updated incorrectly. In short, the property process allows objects to protect and verify their own data.

Read-only and write-only attributes
Most attributes include the get and set attributes so that you can read and modify the stored values at the same time. However, you can use readonly or writeonly modifiers to restrict reading or modifying attributes. Read-Only attributes cannot have the set attribute process. They can be used for items that need to be made public but cannot be modified. For example, you can use the read-only attribute to provide the speed of the computer processor. Only write properties do not have get attribute procedures. They can be used for data that needs to be stored but is not made public to other objects. For example, write-only attributes can be used to store passwords.

Note:When an object is assigned to a property, earlier versions of Visual Basic Support the use of the let property process. Visual Basic. Net eliminates the need for the let attribute process, because it can process object allocation like any other type of allocation.

Attribute process and Field
Attributes and fields can be stored and retrieved in objects. Their similarity makes it difficult to determine which programming option is better under a given circumstances.

Use the attribute process in the following situations:
When you need to control the time and method of setting or retrieving values.
When the attribute has a well-defined set of values that need to be verified.
When a value is set, some noticeable changes (such as a visible property) occur in the object state.
When you set properties, other internal variables or values of other properties are changed.
You must perform a set of steps before you can set or retrieve attributes.

Use fields in the following scenarios:
When the value is of the self-verification type. For example, if a value other than true or false is assigned to a Boolean variable, an error or automatic data conversion occurs.
Any value within the range supported by the data type takes effect. This is the case for many single or double attributes.
The attribute is of the string data type and has no constraints on the size or value of the string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.