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 Sub Main process

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shares with you the content of a sample analysis of the VB.NET Sub Main process. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

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.

In VB.NET, objects also have the concept of life cycle, but it is very different from what it 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. Object construction is triggered when we create a new instance of the class. You can use the keyword NEW to implement it.

Since VB 6 is based on COM, creating an object triggers a VB.NET 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 VB.NET Sub Main process.

The .NET common language runtime takes a different approach to processing components, as does VB.NET. This means that no VB.NET 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 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 () End Sub End Class Thank you for reading! This is the end of this article on "sample Analysis of the VB.NET Sub Main process". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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