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

How to use VB.NET shared variables

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

Share

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

This article mainly introduces how to use VB.NET shared variables, the article is very detailed, has a certain reference value, interested friends must read it!

We can also create another kind of shared member. Sometimes all instances of a class need to share a numerical value, and sometimes each particular type of object needs to share the same variable, which can be achieved by using shared variables.

A shared variable can be declared using the Shared keyword, much like the declaration of a shared method:

Public Class MyCounter

Private Shared

MintCount As Integer

End Class

Like the sharing method, we can also set the scope of shared variables as needed. The two are different by default, where the sharing method is Public and the shared variable is Private.

All in all, we should form a good habit of defining the scope of methods and variables instead of using default values to avoid confusion.

One of the important things about shared variables is that they are common to all instances of classes. We can enhance our class by typing the following code:

Public Class MyCounter

Private Shared mintCount

As Integer

Public Sub New ()

MintCount + = 1

End Sub

Public ReadOnly Property

Count () As Integer

Get

Return mintCount

End Get

End Property

End Class

Take a closer look at the above code: when we create an instance for the class, the counter is incremented by one. The + = operator is new in VB.NET.

We can fetch the count value at any time through the Count attribute. In this way, if we run the following customer code, we can get a result of 3.

Protected Sub Button4_Click

(ByVal sender As Object, _

ByVal e As System.EventArgs)

Dim obj As MyCounter

Obj = New MyCounter ()

Obj = New MyCounter ()

Obj = New MyCounter ()

MsgBox (obj.Count, MsgBoxStyle.

Information,counter)

End Sub

If we run this program again, we will get 6, 9, and so on. As long as our application continues to run the counter will remain valid, that is, once we finish the application counter will no longer work.

This count is useful for the server's processing because it can be counted easily and constantly. This value is reset only when the process restarts.

Global variable

Another common application for shared variables is to provide the type of global variable. Given a shared variable with a Public scope:

Public Class TheClass

Public Shared

MyGlobal As Integer

End Class

We can use this variable in customer code.

TheClass.MyGlobal + = 5

This variable will be valid anywhere in our application, and it provides a good mechanism for sharing values among components, classes, modules, and so on.

The above is all the content of the article "how to use VB.NET shared variables". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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