Summary of VB. NET New Method

Source: Internet
Author: User

Here is a summary of the VB. NET new method. Let's take a look. VB. NET
The new method is like sub.
Main, class_initialize is called before other VB6 classes run code. In addition, it is called before error processing, so debugging becomes very difficult, and errors
The error is displayed on the client to instantiate the object. In addition, class_initialize does not require a parameter, which means that there is no way in VB6 to use the number when the object is created.
Data.

 

 

VB. NET removes class_initialize and adopts the complete constructor method. This constructor provides complete error handling capabilities and can receive parameters. Therefore, we can initialize objects when creating them, which is a very important feature of VB. NET. In VB. NET, the constructor method is sub new.

 

  1. Public class theclass
  2. Public sub new ()
  3. (Initialize the object here)
  4. End sub
  5. End Class

Using this type of constructor, you can create a class instance as follows:

 

  1. Dim OBJ as new theclass ()

This example is similar to creating a VB6 code in class_initialize. However, we often use data to initialize objects when creating objects. We can load some data from the database, or we can directly provide data for the object. No matter what method is used, we want to provide some data for the object when it is created.

To do this, you can add a parameter list to the new method:

 

  1. Public class theclass
  2. Public sub new (byval ID as integer)
  3. (Here we use the id value to initialize the object)
  4. End sub
  5. End Class

Now let's create an instance of the class and provide data for the object. The Code is as follows:

 

  1. Dim OBJ as new theclass (42)

To increase the flexibility, we can receive optional parameter values. To achieve this, there are two methods: declare an optional parameter by using the optional keyword, Or reload the new method. To use the optional keyword, we simply declare the optional parameters. The Code is as follows:

 

  1. Public sub new (optional byval ID as integer =-1)
  2. If Id =-1 then
  3. (The object can be initialized here)
  4. Else
  5. (The id value can be used to initialize the object)
  6. End if
  7. End sub

This method is too idealistic, but since we have to check whether the parameter is (not) provided, and then decide how to initialize the object. The new method can be implemented in two more ways. The first is for each behavior type, which can be implemented through overloading:

 

  1. Public overloads sub new ()
  2. (The object can be initialized here)
  3. End sub
  4. Public overloads sub new (byval ID as integer)
  5. (The id value can be used to initialize the object)
  6. End sub

This method not only avoids conditional checks and simplifies code, but also changes the use of client code objects.
To be clearer. This overload VB. NET
The new method can use or not use parameters, providing greater flexibility. In fact, through overloading, we can create many different constructors, or use many different methods to initialize our
Object. In VB. NET, the constructor method is optional. But there is only one exception, that is, when we use inheritance, the parent class only has one constructor that requires parameters.

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.