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.
- Public class theclass
- Public sub new ()
- (Initialize the object here)
- End sub
- End Class
Using this type of constructor, you can create a class instance as follows:
- 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:
- Public class theclass
- Public sub new (byval ID as integer)
- (Here we use the id value to initialize the object)
- End sub
- End Class
Now let's create an instance of the class and provide data for the object. The Code is as follows:
- 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:
- Public sub new (optional byval ID as integer =-1)
- If Id =-1 then
- (The object can be initialized here)
- Else
- (The id value can be used to initialize the object)
- End if
- 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:
- Public overloads sub new ()
- (The object can be initialized here)
- End sub
- Public overloads sub new (byval ID as integer)
- (The id value can be used to initialize the object)
- 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.