locked
Public Shared Function & Public Function RRS feed

  • Question

  • Can anyone please give me a simple example that explains the difference between public shared function and public function? I have watched a video tutorial and I just could not see the difference. The video said that it is not necessary to create an instance of the class before using a particular method when using a shared method. May I know what does he mean by that? Thanks a lot!


    Monday, January 19, 2009 2:22 PM

Answers

  •  The "Public Shared Function " modifier means that you can access a function without a class instance. Any non-shared members have to be called off of a instance of the class.

    Here's an example:

    The System.Windows.Forms.MessagBox class is very popular in Windows applications. To use it, you just type:
    MessageBox.Show(message, etc.)
    But, you don't have to create an instance of the class to use it first (Dim
    MessageBox as New MessageBox). You can just call the Show method of the
    MessageBox class because the Show method is declared "shared".


    Lastly, the keyword "Public Function" just means that the element will be available outside of the class module it was created in.

    Dim s As New System.Net.WebClient
    s.DownloadData("Address")


    Gaurav Khanna
    • Marked as answer by Yichun Feng Wednesday, January 21, 2009 1:24 AM
    Monday, January 19, 2009 2:48 PM
  • With a Shared method, you don't need an instance of the Class in order to call the method.
    If you're familiar with VB.Net's Modules, then those are a good example of Shared methods. All methods in a Module are Shared.

    It's tough to know how to explain this, because until you need to write a Shared method, you really won't be able to see why anyone would ever need one.

    To give an example, though, you might have some collection-style class which has a method to tell you if one list is longer than another:

    Public Class myCollection(Of T) 
        Inherits System.Collections.Generic.List(Of T) 
        Public Function isLongerThan(ByVal b As myCollection(Of T)) As Boolean 
            If Me.Count > b.Count Then 
                Return True 
            End If 
            Return False 
        End Function 
    End Class 

    If you wanted a method which took two myCollections and returned the longer collection, though, you would need to call that function from an existing instance of the object:

     
        Public Function Longer(ByVal a As myCollection(Of T), ByVal b As myCollection(Of T)) As myCollection(Of T) 
            If a.Count > b.Count Then 
                Return a 
            End If 
            Return b 
        End Function 
     
    'Calling code: 
             a.Longer(a,b) 

    It would be more sensible in this case to create a Shared function in the class itself, so that you don't need an instance of the class in order to call the function:
        Public Shared Function Longer(ByVal a As myCollection(Of T), ByVal b As myCollection(Of T)) As myCollection(Of T) 
            If a.Count > b.Count Then 
                Return a 
            End If 
            Return b 
        End Function 
     
    ' Calling code: 
            Longer(a,b) 

    • Marked as answer by Yichun Feng Wednesday, January 21, 2009 1:24 AM
    Monday, January 19, 2009 2:53 PM

All replies

  •  The "Public Shared Function " modifier means that you can access a function without a class instance. Any non-shared members have to be called off of a instance of the class.

    Here's an example:

    The System.Windows.Forms.MessagBox class is very popular in Windows applications. To use it, you just type:
    MessageBox.Show(message, etc.)
    But, you don't have to create an instance of the class to use it first (Dim
    MessageBox as New MessageBox). You can just call the Show method of the
    MessageBox class because the Show method is declared "shared".


    Lastly, the keyword "Public Function" just means that the element will be available outside of the class module it was created in.

    Dim s As New System.Net.WebClient
    s.DownloadData("Address")


    Gaurav Khanna
    • Marked as answer by Yichun Feng Wednesday, January 21, 2009 1:24 AM
    Monday, January 19, 2009 2:48 PM
  • With a Shared method, you don't need an instance of the Class in order to call the method.
    If you're familiar with VB.Net's Modules, then those are a good example of Shared methods. All methods in a Module are Shared.

    It's tough to know how to explain this, because until you need to write a Shared method, you really won't be able to see why anyone would ever need one.

    To give an example, though, you might have some collection-style class which has a method to tell you if one list is longer than another:

    Public Class myCollection(Of T) 
        Inherits System.Collections.Generic.List(Of T) 
        Public Function isLongerThan(ByVal b As myCollection(Of T)) As Boolean 
            If Me.Count > b.Count Then 
                Return True 
            End If 
            Return False 
        End Function 
    End Class 

    If you wanted a method which took two myCollections and returned the longer collection, though, you would need to call that function from an existing instance of the object:

     
        Public Function Longer(ByVal a As myCollection(Of T), ByVal b As myCollection(Of T)) As myCollection(Of T) 
            If a.Count > b.Count Then 
                Return a 
            End If 
            Return b 
        End Function 
     
    'Calling code: 
             a.Longer(a,b) 

    It would be more sensible in this case to create a Shared function in the class itself, so that you don't need an instance of the class in order to call the function:
        Public Shared Function Longer(ByVal a As myCollection(Of T), ByVal b As myCollection(Of T)) As myCollection(Of T) 
            If a.Count > b.Count Then 
                Return a 
            End If 
            Return b 
        End Function 
     
    ' Calling code: 
            Longer(a,b) 

    • Marked as answer by Yichun Feng Wednesday, January 21, 2009 1:24 AM
    Monday, January 19, 2009 2:53 PM
  • Thanks guys for all your help. I think I will stop trying to understand this shared function for a while because I am still trying to understand class+methods. Thanks again!
    Tuesday, January 20, 2009 8:02 AM