VB.net class and Object basics

Source: Internet
Author: User
Tags garbage collection inheritance new features requires valid versions

OOp (object-oriented-programming) is relative to the structural programming (Structure programming), which means that the object-oriented thinking is used for software programming. It is the most popular programming mode today. Object-oriented technology pursues the direct simulation of software system to the real world, and realizes the real world directly mapping to the solution space of software system.

Objects are entities that handle attributes (properties), Fields (Fileds), Methods (methods), events (event) as a separate data type. Programming also requires objects to meet and support three of the following features: encapsulation, inheritance, and polymorphism. Object allows a user to declare a number of variables and methods first, and then invoke one of its objects for reuse when it needs to be used.
In earlier versions of Visual Basic (1.0-3.0), which did not include object-oriented features, starting with VB 4.0, users could create a new class as a new form, and take it as a new object, Classes are a very important part of vb.net, and almost all formal programs include one or several classes, in vb.net, the difference between a class module and a form is no longer there, and almost all programs are made up of classes, and in order to better use them, they are divided into functional libraries in different functional categories.

VB.net in the Declaration, construction and execution class than VB 6.0 has a lot of changes, but also in the processing of objects there are no small changes. These changes affect methods that define an object, reference and Dereference objects, and use bundled techniques.
VB.net did not use the CreateObject statement to create the object. CreateObject is a product of Visual Basic and COM-related. Because vb.net no longer uses COM, CreateObject is no longer used from vb.net. VB.net uses the new statement to create the object. Users can use new at any point in the code. Here is an instance of creating a variable and creating an object in an instance of the class:
Dim obj as Theclass
Obj=new Theclass ()
You can simplify the above statement:
Dim obj as New theclass ()
In Visual Basic 6.0, there are some problems with the above two paragraphs, but there is no difference between the two paragraphs in the vb.net, but the second paragraph is shortened. If a user declares a variable in a module, that variable is valid only in that module. In many cases, the user wants to declare a variable in the scope of the method, or want to be in a module (such as a Try ...). End or loop loop structure), create an instance of the class. In such cases, it is not appropriate to combine the declarations with instantiation.
The above statement defines a variable for the user and instantiates a class. This statement may be more useful when dealing with inheritance or multiple interfaces. The user can declare the variable to be one of these types and instantiate the object based on the class to perform the interface:
Dim obj as Myinterface=new theclass ()
You can also use complex statements at the same time. If you now have a method that requires an object reference, you can instantiate the object like this:
DoSomething (New theclass ())
The above statement is a new instance of calling the DoSomething method and passing theclass as an argument. This new object is only present in the invocation event of this method, that is, when the method completes, the object is automatically dereference.
It should also be noted here that canceling a reference to an object does not mean terminating the object at once. This point has been explained in the previous introduction. object is only in the. NET garbage collection handler to clear them out of memory.
The following example may be more complex. Instead of using an object reference, the method here requires a string. A user can instantiate an object and invoke a method from one method by supplying a string value to an object:
DoSomething (New theclass (). Getstringdata ())
Obviously, the user might need to look at the readability of the statement. It is important to note that the compression of statements tends to degrade readability.
When working with objects, the SET statement is not used here. In VB 6.0, when working with object references, users have to use the SET command to process objects from other data types. In VB.net, objects from other data types are handled in different ways, and you can use direct arguments to work with objects, like working with integer or character data types. This set command is no longer valid in vb.net.
In VB 6.0, a user can dereference an object by setting the object reference to Nothing (empty). This is the same as the approach in vb.net.
Dim obj as Theclass
Obj=new Theclass ()
Obj=nothing
However, this statement has different effects in vb.net. Because vb.net does not use reference computing to terminate objects, it relies on garbage collection mechanisms. In VB 6.0, this object is terminated when no variable accesses the object's reference. In vb.net, the following view is wrong: When the garbage collection handler discovers that the object is not referenced, it is terminated. The correct thing to do is to purge the object from memory after the last reference has been removed for a period of time. However, this does not clear the value of the object being canceled. If the user has a long-running algorithm, it is best to explicitly dereference the object in the handler so that it can be purged by the garbage collector if possible. As long as the code retains a reference to the object, the object remains in memory and is not purged by the garbage collector.
vb.net can create processing code that runs on the. NET Framework. All management code can interact with other types of processing code, regardless of the programming language in which the user creates the components. This means that a user can create a class on one programming language and then apply it to another programming language, including, of course, inheritance. This is the hybrid programming mechanism of cross language mixed inheritance. In fact, there are many program designers who are already applying this technology. Many of them. NET System class libraries are written on C #, and users can inherit these classes as base classes when they write programs on vb.net.
For example, create a class library project in vb.net named Vblib, and add a simple class to parent, the code is as follows:
Public Class Parent
Public Sub dosomething ()
MsgBox (Parent dosomething,msgboxstyle.information.parent)
End Sub
End Class
Many program designers may not yet know that vb.net supports visual inheritance of Windows Forms. Visual inheritance, which means that users can create a Windows Form and then inherit the form so that other forms have the same layout, controls, and behavior as the form. Users can also use inheritance to create their own windows controls. For example, a user can create a textbox (text box) with improved performance to achieve a special validation of data entry. By inheriting, create a subclass that inherits from the original text box control class, and make the appropriate modifications to improve the performance of the text box to enable validation of data entry. This is the same as Web Form controls, where Web Form controls can create a subclass of a Web Form control that already exists. A user's subclass can overload an existing function or add some new functions.
VB.net not only provides inheritance, but it also provides many other important new features. VB.net improves the way you create and process multiple interfaces, making it easier for users to use them. In addition, vb.net supports events as part of the interface so that all elements can now be expressed on the interface: Methods, properties, events. In addition, terminating an object in vb.net is not implemented by reference counting, but instead uses the garbage collection program to purge objects from memory. All in all, vb.net has greatly increased its object-oriented capabilities compared to previous versions of Visual Basic, and vb.net retains most of the features of a lower version.
For a detailed theory of object-oriented, see the following chapters: VB.net's OOP architecture.

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.