cool hit counter Exploring Shared and Static in VB.net_Intefrankly

Exploring Shared and Static in VB.net


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/28230345

After a brief look at C#, it's soon time to start refactoring the server room charging system, and some people have chosen to implement it in C#, but still want to get a good understanding of some VB.net and use it.

With VB and an object-oriented foundation, learning VB.NET is extremely easy to get started. Having learned several languages we all have an experience that a programming language is a language, a tool through which certain functions are implemented. If we have learned C++, C# and any other OO language before, and we have understood OOP through these languages, now you are asked to learn VB.NET, it will surely be particularly easy, although their syntax is different, but the ideas in them are the same, they all have classes, constructors, properties ......

Comparison of some keywords in VB.net and C#.

C# Keywords

VB.net Keywords

using

Imports

this

Me

void

sub

base

MyBase

abstract

Mustlnherit

sealed

NotOverrideable

virtual

MustOverride

switch

Select

internal

Friend

static

shared

……

……

But has anyone noticed that There are also traces of static in VB.net , so is static in VB.net the same as static in C#? What is the difference between it and SHARED?

Having written a previous Blog about static , which amounts to a little more expansion here.

Let's start with two small examples.

C#:

     public class CircleArea // Calculate the area of a circle
    {
         public double Area(double radius) // Calculate the area of the circle based on the radius value passed in
        {
            return PI.PI * radius * radius;
        }
    }

     public class PI // Define the value of PI
    {
        public static double PI = 3.14;      'It's here., usefulnessstatic trimPI, so that it can be called elsewhere
    }

VB.net:

    Public Class CircleArea   ' Calculate the area of a circle
        Public Function Area(ByVal radius As Double) As Double  ' Calculate the area of a circle from the incoming radius
            Return PI.PI * radius * radius
        End Function
    End Class

    Public Class PI     ' meaningPI values
        Public Shared PI As Double = 3.14       '
    End Class

In the two small examples above, the PI is defined as a static value for the class or other places that need to use the PI to call it, and at this point in the VB.net implementation, Shared in the PI class cannot be replaced with Static, otherwise the following error message will appear.

What are the reasons?

Let's start with Static in VB.net

Still illustrated by an example, creating a VB.net form application.

    Public Class frmTest
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Static Dim temp As Integer = 1   ' Define astatic variabletemp
            temp = temp + temp
            MessageBox.Show("temp=" & temp)
        End Sub
    End Class

When the program is running, clickingButton1, exit from temp=2; Click again.,temp=3, Click again.,temp=4…… furthermore, Here if you putStatic convert toShared, then an error will be reported:

So it can be determined that static in VB.net is mainly used to modify the static variable , with emphasis here on " static (as in electrostatic force) ", the main point is that a variable modified with static exists and retains its latest value for the duration of its life while the program is running, and is not reinitialized on the next call.

Only for local variable Use static. This means that the declaration context of a Static variable must be a block within a procedure, not a source file, namespace, class, structure, or module, and that a Static cannot be used within a structural procedure again.

And then there's the shared in VB.net

In the process of programming, it is often necessary to share some data, such as the "unit price" used in the development of our server room charging system, the string connected to the database, etc., all need to share.

Going back to the VB.net applet that calculated the area of the circle at the beginning of the article.

    Public Class CircleArea   ' Calculate the area of a circle
        Public Function Area(ByVal radius As Double) As Double  ' Calculate the area of a circle from the incoming radius
            Return PI.PI * radius * radius
        End Function
    End Class

    Public Class PI     ' meaningPI values
        Public Shared PI As Double = 3.14       '
    End Class

Here, why is it called "Shared"? Share means "share" in English, and the object modified by Shared here also holds the "same value" and is used to "share" for other objects to call, so I prefer to call it a "shared type". As with PI above, PI values are used when calculating the area of any size circle, so trim it with Shared.

When to use Shared?

It says: "You can only use static for local variables. The declaration context of a Static variable must be a block within a procedure, not a source file, namespace, class, structure, or module, and a Static cannot be used within a structural procedure again. ", Shared is just the opposite, it cannot modify local variables that

If a variable When other program segments are needed to share, Then useShared,Shared It can be used inEvent 、Function 、Property 、Sub In statements such as。

For more on this, see MSDN atShared (Visual Basic) && Shared (Visual Basic)

The rookie learns his craft poorly, and is for reference only, so feel free to share any discrepancies.


Recommended>>
1、Find two numbers whose sum is fixed
2、A concise summary of probability theory ProbabilityCheatsheetv20 the probability quick checklist is so handy A must for home travel
3、Some insights into the unfolding of Cantor
4、Industry Apple to Launch Dedicated Website by Years End to Handle Data Requests from Law Enforcement Agencies Worldwide
5、Duties and prospects of an operations and maintenance engineer

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号