Establishment and destruction of VB.net objects: The life cycle of objects

Source: Internet
Author: User
Tags constructor integer

When an object is destroyed, the Sub destruct method is automatically invoked by the system, but cannot be explicitly invoked. NET Framework can destroy an object when it thinks it has no use value. But remember, unlike class+terminate and Sub New, the system will call sub destruct this method after the last reference to an object.
1. The constructor of the sub New with parameters
Users can implement the constructor of a class by defining the Sub New procedure anywhere in the definition of a class. The first line of the constructor must be the constructor of the base class that called it, or another constructor of the current class. The user must confirm that the base class was initialized before the initialization of the other class. But surprisingly, even if you create a basic class, the user must call the constructor MyBase.New, because all classes are eventually inherited from a class named object. After invoking the constructor of its parent class, the user can add some additional initialization code to the Sub New procedure. Sub New can support constructors with parameters that are passed in from the procedure by calling the constructor, such as
Dim T as truck=new truck
2. Use sub destruct as destructor
To create a destructor for a class, a user can establish a procedure named sub destruct, and can enter any place in the definition of a class where code written in a destructor can be used to free other objects, close a file, or do other end work. The following example shows how to create a class with a constructor with parameters and initialize an attribute.

Imports System
Namespace trucknamespace
Module Module1
Class truck
  Private iwheels as Integer
     Sub New (initialwheels As Integer)
        mybase.new
        iwheels=initialwheels End
     sub
     sub-destruct ()
        ' place Cleanup code-here-end Sub-property wheels as Integer get wheels=iwheels end Get
     Set
        Iwheels=value
     End Set End
Class
Shared Sub Main ()
    Dim t as truck=new truck
    Console.WriteLine ("The truck has" &_
    t.wheels& "wheels when initialized") End Sub-end
Module

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.