Vb. NET in the class

Source: Internet
Author: User
Tags constructor inheritance variables
As I mentioned in my previous article: The advent of vb.net makes VB6 a great change in the concept and operation of the class. Now let's go into these changes for a more in-depth discussion. First let's look at what the class really means.

Suppose I take you back to college, and now you walk into a classroom and I ask you to explain what class is, you might say, "a group of students form a class." "Oh, if this answer, you are really fluke: yes, here, the group is the most critical word." In terms of object-oriented programming, a class is a way to store data and give a group of related code a coordinated function. Classes are the core of object-oriented programming.

. NET gives Class A new face
VB6 and vb.net have a major difference in the use of classes, which is that the latter no longer uses set keywords. In addition, the method of defining classes in code has changed. These changes will not only directly affect the code you write, but will also change the way you organize your source files.

Under VB6, each class must be added and defined within a single source file. This is not the case under vb.net: A class is defined as a block of code, as follows:

Public Class MyClass
' Write the member code for the class here!
End Class

In the face of such revolutionary changes, some people may be impatient for the mouth to bubble, and most people may not care or do not understand the use of code blocks to define what the class is. To tell you the truth, it all depends on your taste. Some people like and want to define only one class in a file, while others are more likely to logically organize their classes. For example, if you have a product class and you have a corresponding products collection class, you can simply put them in the same file to better organize your source files.

The construction and deconstruction of class--a new facelift
When your code creates an instance of a class, it calls a special method of the class: the constructor (constructor). Similarly, the destructor (destructor) method is invoked when the class is corrupted. In most object-oriented languages, the constructor and destructor of a class are usually taken with the above two general terms rather than the member names when they are specifically encoded. I like to use the term "structors" to refer to constructs and destructors. In the VB6 language, the specific "structors" is Class_Initialize and Class_Terminate. Under VB.net, the Class_Initialize method is renamed to New, and Class_Terminate is finalize.

The programmer is not allowed to add parameters to the Class builder according to the VB6 implementation restrictions. However, VB. NET, the constructor method is provided with parameter support. Under VB.net, you can now arbitrarily add parameters to this method, or even overload the method.

How does a parameterized constructor create a class flexibly? For example, suppose you have an ADO connection class. In VB6 language, you first instantiate the class and then define the connection in the code with the ConnectionString attribute. To use vb.net, the constructor for this class is parameterized and overloaded so that the programmer can pass a connection string to the object when it is instantiated, so you don't have to worry about how to set up subsequent connections within the code. Of course, you can do it the old-fashioned ways: what parameters are not passed and later set the connection string in code.

Fields and properties
A lot of people in the understanding of the attribute has produced some confusion concept. In fact, the property of this thing is not as complex as people think. Let's go back to the most basic concept level to discuss attributes: The so-called attribute is actually a piece of data that is contained within the class containing the read/write interface. By this definition, you might think that you can declare a public variable in a class and call it an attribute. Of course, this assumption is not without a point, but the technical term for a class's public variable should be field (field). The key difference between a domain and a property is the inclusion nature of the interface. Now for the moment not to make a specific explanation of this difference we first understand its grammatical use can be.

In VB6, properties are defined and manipulated by let, get, and set programs. And in the vb.net, the grammar changes. An attribute is organized as a single property code block, where a child code block is set for both the write (set) and read (get) operations of the property. This program acts as an abstraction layer above the private variables of the class. Most people are accustomed to naming these private variables in a way that is similar to public variables, preceded by a lowercase prefix letter m or an underscore that distinguishes the associated property (the M prefix is inherited from the VB6, meaning "module-level variables"). Give an example:

Private _contactname as String
Public Property ContactName ()
Get
Return _contactname
End Get
Set (ByVal Value)
_contactname = Value
End Set
End Property

I created an attribute for a hypothetical class ContactName. To support this property, I need to set a variable in the class to store its actual value. ContactName a private variable is the complete function. As soon as I get or set this property, I use the value indirectly.

So why do you do this better than using a domain? The main reason is that if you adopt a domain, you cannot control the data that is written to the variable. For example, some people have to place a string in an integer field or assign a value to a read-only field. In the case of attributes, you can add logic to the Set program to guarantee the validity of the assignment. You can even completely omit the set code block to set the property to read-only.

Property that's amazing
At last. NET also provides us with what is called a property. If a class is marked as an attribute, it is given some particularly interesting special features. For example, the following serializable attribute.

<serializable () >
Public Class MyClass
' This class can be serialized!
End Class

After you mark the above class as a property, I can get an instance of it and write out the disk regardless of its state. At any time, I can serialize the class as if it were always stored in memory. NET Framework provides a large number of properties that you can even use vb.net to create your own properties.

Inherited
As you can see, VB. NET class has changed significantly in terms of concept and work VB6.0. In the next article, I will continue to discuss these changes, mainly to explain the inheritance of classes. Most VB programmers are likely to understand inheritance, but they almost never use it. I will explain in detail the importance of inheritance and under what circumstances inheritance


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.