For complex operation Design Solutions, we must first determine the objects that make up this system, in VB, we often combine the properties and operations of each object into a class, the definition of the class is as follows:
Class ClassName
' Properties and actions
End Class
A complete example:
Class Classname
Dim A As String
Dim b As Integer
Dim c As String
Sub Op ()
End Sub
Sub Cl ()
Endsub
Endclass
The definition of a class does not produce a variable of its own, and after definition you can use the new operator to produce a variable
Dim variable As ClassName
You can use the dot operator to refer to a variable definition
Variable.a = ' Test '
When creating a class, you should design the class as a "black box", just like a TV. We do not need to understand the composition and principle of the television, directly use it, you can hide the class members and methods of private classes,
Modifiers used to control access to class members
Friend: Available only in the current project
Private: Only available in this class
Protected: Members available in this class and derived classes in this class
Protected Friend: Available in the current project and in derived classes of this class
Public: Code other than a class can also access
The default value is: Public
Initialization of a class member:
When you create an object of a class, you usually assign a value immediately, and in order to simplify the assignment, you can put a specialized subroutine, the constructor, which is called New in any class definition, for example:
Class Book
Public title As String
Public Publisher As String
Sub New ()
Me.title=title
Me.publisher=publisher
End Sub
End Class
If you want to simplify references to object members, you can use the WITH operator
When you create a class, you may often need to limit the assignment of variables to a class, for example, age, in order to limit the assignment of a class variable, you can use private when the variable is defined, for example
Module Module1
Class Book
Public title as String
Public author as String
Private Price as Double
Public Property Bookprice () as Double
Get
return price
End Get
Set (ByVal Value as Double)
If (value >= 0) and (value <=) Then
Price = Value
Else
MsgBox ("error")
Price = 0
End If
End Set
End Property
Public Sub New (ByVal title as String, ByVal author as String)
Me.title = Title
Me.author = author
End Sub
End Class
Sub Main ()
Dim p as New book ("OS AP", "Hans")
P.bookprice = 490
Console.ReadLine ()
End Sub
The method corresponding to the constructor is a destructor, which is a finalize subroutine
Here is a detailed explanation for this in MSDN:
Whenever an instance of a class is created, if a procedure named New is present in the object, the common language runtime (CLR) attempts to execute it. New is a procedure called a constructor that initializes a new object before any other code in the object executes. The New constructor can be used to open a file, connect to a database, initialize a variable, and handle any other tasks that need to be completed before the object can be used.
When an instance of a derived class is created, the Sub New constructor of the base class executes first and then executes the constructor in the derived class. This is because the first line of code in the Sub New constructor uses the syntax MyBase.New () to invoke the constructor of the class itself in the class hierarchy. The Sub New constructor of each class in the class hierarchy is then invoked until the constructor of the base class is reached. At this point, the code in the base class constructor executes, then executes the code of each constructor in all derived classes, and finally executes the code in the most closely derived class.
When an object is no longer needed, the CLR invokes the object's Finalize method and then releases its memory. The Finalize method is called a destructor because it performs cleanup tasks, such as saving state information, closing files and connecting to the database, and performing other tasks that must be done before the object is disposed.
When you call a sub-finalize method of a derived class, you first perform any required cleanup tasks, and then use the syntax Mybase.finalize () to explicitly call the sub Finalize method of its base class. Therefore, the Sub Finalize method first runs from the closest derived class and finally executes the code in the base class.
Once the Finalize method is involved, the garbage collector cannot fail to mention that in the. NET environment, the garbage collector manages memory, and in vb.net, the garbage collector is told that your program does not need to use an object by setting the object to nothing, as follows:
Once the Finalize method is involved, the garbage collector cannot fail to mention that in the. NET environment, the garbage collector manages memory, and in vb.net, the garbage collector is told that your program does not need to use an object by setting the object to nothing, as follows:
Employee=nothing
Before the garbage collector releases an object, it first calls the Finalize method of the object, which, depending on how it is handled, can sometimes take a long time between the execution of an object and the Finalize method of an object, in which case a specific method of disposing is placed in the class. This method is used to perform processing of discarded objects, when the contents of an object are not needed, the Dispose method is invoked, the program immediately executes the object's cleanup, carefully observes the window code generated by VS, and discovers the code for the Dispose method, as follows:
Public Sub New ()
MyBase.New ()
' This call is required by the Windows Forms Designer.
InitializeComponent ()
' Add any initialization after the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub
All we need to do is call the Dispose: Someclass.dispose