What are the differences between Shared and Static?

前端 未结 2 1844
梦如初夏
梦如初夏 2021-01-11 10:15

I\'m a C# developer but I\'ve inherited a legacy VB app today with 0 documentation what so ever. I\'ve been starting to read through the code and reference the list of VB ke

相关标签:
2条回答
  • 2021-01-11 10:55

    For VB.Net you use shared exactly like Static is used in C#, but VB.Net has a static keyword as well and it is used to retain a variable value even after the method call has ended. So the next time you call a method it will have that previous value. MSDN has a more detailed explanation here - http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx

    From the link there are some interesting behaviors:

    When you declare a static variable in a Shared procedure, only one copy of the static variable is available for the whole application. You call a Shared procedure by using the class name, not a variable that points to an instance of the class.

    When you declare a static variable in a procedure that isn't Shared, only one copy of the variable is available for each instance of the class. You call a non-shared procedure by using a variable that points to a specific instance of the class.

    0 讨论(0)
  • 2021-01-11 10:57

    The equivalent of the C# Static method modifier is Shared in VB.net

    The closest equivalent of the C# Static class modifier in VB.Net is a Module

    The Static keyword in VB.NET defines a local variable that exists for the lifetime of the process. There is no equivalent of this in C#.

    For a great reference of comparison between the two see this link: https://www.harding.edu/fmccown/vbnet_csharp_comparison.html

    0 讨论(0)
提交回复
热议问题