Sharing methods, shared variables, and global variables in VB.net

Source: Internet
Author: User

Sharing Method

  • Description: You do not need to create a class instance. Static methods or class methods are called in other programming languages.
  • Instance: A Shared method cannot be used as a common method, but can be accessed directly from a class through an instance of an object. Examples of sharing methods are as follows:
Public Class Math  Shared Function Add(ByVal a As Integer, ByVal b As Integer) As Integer  Return a + b  End Function  End Class 


We can access it without instantiating a math object. The Code is as follows:

Dim result As Integer  result = Math.Add(5, 10) 

  • Analysis:In the preceding example, the class method is used directly. If you use the common method, it will lead to errors, but using the shared method is acceptable. The sharing method can be accessed not only through the common method, but also by providing the access function without creating an object. In fact, when a shared method is called, no object is created, just as a program in the module can be directly called.

 

  • Overload:The VB.net sharing method can also be overloaded. Therefore, you can use the same sharing method to create a series of changing methods. Each method has a list of different parameters.

  • Scope:The default scope of the shared method is public. We can also set the sharing method to friend, protected, or private in the Declaration. In fact, when a method is overloaded, we have different scopes as long as the parameter list is different.

  • Usage:How is the sharing method used? Let's look at an example. When we want to open a text file for the input, we can use a shared code in the file class, as shown below:
Dim infile As StreamReader = File.OpenText(words.txt)  Dim strIn As String  str = infile.ReadLine() 
  • Analysis:No file of any type is created here. Opentext is a sharing method that opens a file and returns a streamreader object.

Shared variable:

 

  • Meaning:We can create another shared Member. Sometimes all instances of the class need to share a value, and sometimes each specific type of object needs to share the same variable, which can be achieved through sharing variables. A vb. NET shared variable can be declared using the shared keyword, which is like the declaration of the shared method:
Public Class MyCounter  Private Shared mintCount As Integer  End Class 

  • Scope:By default, the sharing method is public, and the sharing variable is private. All in all, we need to develop a good habit: defining the scopes of methods and variables without using the default values to avoid confusion. One important thing about shared variables is that they are common in all class instances. We can type the following code to enhance our class.

 

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  

  • Analysis:When we create an instance for the class, the counter is added. + = Operator is a new operator in VB. NET. At any time, we can use the Count attribute to retrieve the Count value. In this way, if we run the following Customer code, we can get 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 can get 6, 9, and so on. As long as our application continues to run the counter, it will remain valid, that is, once we end the application counter, it will no longer work.

This count is very useful for the server's processing process, because it can easily count continuously. This value is reset only when the processing starts again.

Global Variables

  • Another common application of VB. NET shared variables provides the type of global variables. Given a shared variable with a public scope:
Public Class TheClass  Public Shared MyGlobal As Integer  End Class 


We can use this variable in the Customer Code.

TheClass.MyGlobal += 5 


This variable will be effective anywhere in our application. It provides a good mechanism to share values among components, classes, modules, and so on.


I have searched and sorted out some materials from the Internet based on my own needs. I hope readers will criticize and correct me.

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.