Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of VB.NET object

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the VB.NET object example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The life cycle of an object

In VB 6, objects have a clear definition and an easy-to-understand concept of lifecycle, which is defined by the following events.

Event description

When Sub Main runs, it is loaded as a component, and before the object is created.

Class_Initialize it runs before other code in the object runs. When an object is created, it is called by the running program.

Class_Terminate is run after other code in the object is run. Called by the running program when the object is unloaded.

VB.NET objects also have the concept of life cycle, but they are very different from what they used to be. In particular, we no longer have the same concept of component-level Sub Main (which is loaded as a DLL), and the Class_Terminate event has been changed, while the Class_Initialize event has been replaced by a mature constructor method. It is worth pointing out that this constructor method can accept parameters.

Now in VB.NET, all we need to define a life cycle is a New event that runs before other code in the object and is called when the object is created.

It is true that there is a big change from VB 6 to VB.NET, which we will discuss in detail below.

Construction

The VB.NET object construction is triggered when we create a new instance of the class. You can use the keyword NEW to implement it.

Sub Main

Since VB 6 is based on COM, creating an object triggers a Sub Main process to run. This happens when an object is created from a given component (usually DLL). Before creating the object, the VB 6 runner loads the DLL (dynamic Link Library) and runs the Sub Main process.

The .NET common language runtime takes a different approach to processing components, as does VB.NET. This means that no Sub Main procedure is called when the component is loaded. In fact, Sub Main is only used when an application starts. When another component is loaded by the application, only the code in the class is called.

In fact, it is unwise to rely on Sub Main in VB6, because the code will be run before all wrong actions. Bugs in Sub Main is difficult to debug in VB6. If we have to use code that adheres to the concept of Sub Main to initialize, then we need to execute a workspace in VB.NET.

It is easy to call a method from a constructor method in each class. For example, we can create valid code in a module:

Public Module CentralCode Private blnHasRun As Boolean Public Sub Initialize () If Not blnHasRun Then blnHasRun = True (initialize here) End If End Sub End Module

This program is designed to run only once, no matter how it is called. We can use this method from every constructor in the class. For example:

Public Class TheClass Public Sub New () CentralCode.Initialize () (add additional work here) End Sub End Class

Although the above code does some extra work, it achieves the same effect as a Sub Main program that uses the VB6 type.

New method

Just like Sub Main,Class_Initialize is called before the code in other VB6 classes runs. In addition, it is called before error handling, which makes debugging difficult, while errors are displayed on the client as general errors to instantiate objects. In addition, Class_Initialize takes no parameters, which means that there is no method in VB6 that can be initialized with data when the VB.NET object is created.

VB.NET removes Class_Initialize and uses a complete constructor method. This constructor has full error handling and can accept parameters. So we can initialize objects when they are created, which is a very important feature of VB.NET. The constructor method in VB.NET is Sub New.

Public Class TheClass Public Sub New () (initialize the object here) End Sub End Class

With this type of constructor, you can create an instance of the class as follows:

Dim obj As New TheClass ()

This example is similar to creating a VB6 code in Class_Initialize.

However, often, when we create VB.NET objects, we often initialize them with data. We can load some data from the database, or we can provide data directly to the object. Either way, we want to provide some data for the object when it is created.

To do this, add a parameter list to the New method:

Public Class TheClass

Public Sub New (ByVal

ID As Integer)

(use the ID value here to initialize the object)

End Sub

End Class

Now let's create an instance of the class and provide data for the object, as follows:

Dim obj As New TheClass (42)

In order to increase flexibility, we can receive optional parameter values. There are two ways to do this: by declaring an optional parameter using the Optional keyword, or by overloading the New method. To use the Optional keyword, we simply declare the optional parameters as follows:

Public Sub New (Optional

ByVal ID As Integer =-1)

If ID =-1 Then

(you can initialize the object here)

Else

(here you can use ID values to initialize objects.)

End If

End Sub

This approach is too idealistic, but since we have to check whether the parameter is (or not) provided, and then decide how to initialize the object. The New method can be implemented in two more ways. * is for each type of behavior, and it can be overloaded:

Public Overloads Sub New ()

(you can initialize the object here)

End Sub

Public Overloads Sub

New (ByVal ID As Integer)

(here you can use ID values to initialize objects.)

End Sub

This approach not only avoids conditional checking and simplifies code, but it also makes the use of client code VB.NET objects clearer. This overloaded New method has more flexibility with or without parameters.

In fact, through overloading, we can create many different constructors, and we can initialize our VB.NET objects in many different ways.

Thank you for reading this article carefully. I hope the article "sample Analysis of VB.NET objects" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report