Q re Shared Methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Woods

    #1

    Q re Shared Methods

    Hi

    I'm pretty new to vb.net and have a question because I'm not sure when to
    use Shared as part of the method declaration. I understand, from my reading,
    that this applies the method to the class rather than each object created
    from that class, but I don't understand the applicability of this.

    So for example, I have a business object working in the middle tier. I have
    a collection class, MyBusinessObjec ts, which has a Function GetData and
    returns a MyBusinessObjec t.

    e.g.

    Public Class MyBusinessObjec ts

    Public Function GetData(KeyList () as String) as MyBusinessObjec t
    ....

    or

    Public Shared Function GetData(KeyList () as String) as
    MyBusinessObjec t
    ...
    End Class

    So when I'm using this collection in my code, I could

    Dim _MyBusinessObje cts as New MyBusinessObjec ts
    For Each _MyBusinessObje ct as MyBusinessObjec t in
    _MyBusinessObje cts.GetData(Key list())
    ...
    Next

    or using the Shared declaration I could

    For each _MyBusinessObje ct as MyBusinessObjec t in
    MyBusinessObjec ts.GetData(Keyl ist())
    ...
    Next

    Perhaps my example isn't a very good one, but when should I use Shared and
    when not?

    Thanks

    Simon


  • Mattias Sjögren

    #2
    Re: Q re Shared Methods

    >Perhaps my example isn't a very good one, but when should I use Shared and
    >when not?
    You can use Shared whenever the method doesn't depend on any instance
    data or method.


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Phill W.

      #3
      Re: Q re Shared Methods

      Simon Woods wrote:
      I'm pretty new to vb.net and have a question because I'm not sure when to
      use Shared as part of the method declaration. I understand, from my reading,
      that this applies the method to the class rather than each object created
      from that class, but I don't understand the applicability of this.
      Consider the String class.

      Dim s As String = "a,b,c,d,e"
      Dim array() as String _
      = s.Split( "," )

      Here, you're using an Instance method (Split) that relies on the data
      held in the /specific/ String object that it's called on.

      s = String.Format( "{0},{1},{2 }" _
      array(0), array(1), array(2) )

      This is using the Shared method, Format. Format doesn't operate on any
      specific String, but it's "related" to other String-like operations so
      the logical place to put it is in the String class.

      HTH,
      Phill W.

      Comment

      • Simon Woods

        #4
        Re: Q re Shared Methods

        Thanks Phill and Mattias

        Phill W. wrote:
        Simon Woods wrote:
        >
        >I'm pretty new to vb.net and have a question because I'm not sure
        >when to use Shared as part of the method declaration. I understand,
        >from my reading, that this applies the method to the class rather
        >than each object created from that class, but I don't understand the
        >applicabilit y of this.
        >
        Consider the String class.
        >
        Dim s As String = "a,b,c,d,e"
        Dim array() as String _
        = s.Split( "," )
        >
        Here, you're using an Instance method (Split) that relies on the data
        held in the /specific/ String object that it's called on.
        >
        s = String.Format( "{0},{1},{2 }" _
        array(0), array(1), array(2) )
        >
        This is using the Shared method, Format. Format doesn't operate on
        any specific String, but it's "related" to other String-like
        operations so the logical place to put it is in the String class.
        >
        HTH,
        Phill W.

        Comment

        Working...