C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
However: We must instantiate Widget, with New Widget(), to call TestB(). The TestB() function is not Shared.
Note: Fields, such as the _value field above, are also Shared or not Shared.
And: In a program, Shared fields can be accessed by Shared Functions. Regular Functions can access both Shared fields and also regular fields.
Thus: This means that Shared functions have additional restrictions on what they can do.
VB.NET program that uses Shared function
Module Module1
Class Widget
Public Shared Function Test() As Integer
' Cannot access regular fields.
Return Integer.Parse("1")
End Function
Dim _value As String = "2"
Public Function TestB() As Integer
' Can access regular fields.
Return Integer.Parse(_value)
End Function
End Class
Sub Main()
' Use Shared Function.
Console.WriteLine(Widget.Test())
' Use Non-Shared Function.
Dim w As Widget = New Widget()
Console.WriteLine(w.TestB())
End Sub
End Module
Output
1
2
Also: In many implementations, instance functions are passed a reference to the instance as the first argument.
But: You can determine whether a function should be shared based on whether it acts upon specific instances or not.