2

Is Shared is a type of scope or a type of data in VB.NET? When is it necessary to use the Shared word before a class, function, or sub in VB.NET? For instance, why would you put Shared in front of the Main method, like this:

Public Shared Sub Main()
    ' ...
End Sub
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
Diego Flores
  • 165
  • 1
  • 7
  • I ussually avoid using the `shared` keyword before entire classes, instead I declare it as a module.[This question](http://stackoverflow.com/questions/881570/classes-vs-modules-in-vb-net) goes much farther into it if you are curious. But I think it breaks down to personal preference. – Mike_OBrien Apr 08 '13 at 20:41

4 Answers4

2

The Shared modifier in VB.NET is the same as the static modifier in C#. It's neither scope (because it can be Private or Public) nor a data type. According to the MSDN:

Specifies that one or more declared programming elements are associated with a class or structure at large, and not with a specific instance of the class or structure.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
George
  • 2,165
  • 2
  • 22
  • 34
2

Shared Main does not make sense (if you build a console application)

Here is an example for shared:

Class WithShared
  Shared Sub A_Method()
  End Sub
End Class

Class WithoutShared
  Sub A_Method()
  End Sub
End Class

now possible code in your Main:

Dim WithSharedInstance as New WithShared()
WithSharedInstance.A_Method() 'does not work
WithShared.A_Method() 'works

Dim WithoutSharedInstance as New WithShared()
WithoutSharedInstance.A_Method() 'works
WithoutShared.A_Method() 'does not work

So if a call does not depend on a specific instance, then use Shared. Since you call Shared Methods through the classdefinition, you cannot access variables and methods, which only exist when the class is instanced.

Amegon
  • 629
  • 6
  • 15
  • Dim WithoutSharedInstance as New WithShared() should be changed to Dim WithoutSharedInstance as New WithoutShared(). – dybzon Sep 18 '17 at 07:50
1

For functions and subs, it means that the function or sub belongs to the class itself, not to an instance of the class. For example, String.IsNullOrEmpty refers to a shared method, whereas myString.TrimEnd() refers to an instance (non-shared) method.

In C# you can make a class static, which means that all of the members will be static. This is not supported in VB (at least not according to this documentation). Modules in VB serve a very similar purpose to static classes in C#.

The only time I can think of when a method needs to be static (shared) is when another static member references it. Otherwise, they're very useful for certain scenarios, such as when a particular method or property doesn't need to retain or access any instance-specific state, but they're not usually necessary.

John M Gant
  • 18,970
  • 18
  • 64
  • 82
1

A good way to look at this is by example:

Dim firstName as String = "Bob"
Dim lastName as String = "Jones"
Dim fullName as String = String.Format("{0} {1}", firstName, lastName.ToUpper())

If you were creating the String.ToUpper() and String.Format() functions you need to differentiate between a function that acts on an instance of a class and one which doesn't.

If you were writing a class like String:

.Format() would be declared Shared, because it doesn't need an object.
.ToUpper() wouldn't because it needs an object instance. The value it returns is related to the object.

Derek Tomes
  • 3,989
  • 3
  • 27
  • 41