Requesting advice: shared properties vs other class interactions.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mgoold2002@hotmail.com

    #1

    Requesting advice: shared properties vs other class interactions.

    Hello. I've just begun programming in VB .NET, and I'm trying to turn
    all my modules into classes. In order to retrieve/exchange values from
    one class to another, I initiated New instances of the classes in each
    class where I needed to retrieve a property or method. I discovered
    that this reciprocal loading of class instances led to stackoverflow
    exceptions. I am now remedying this problem by sharing properties
    among the classes. I ask you though: what's the best way to share
    methods and properties reciprocally among classes? Any advice is
    greatly appreciated.

  • Mike McIntyre

    #2
    Re: Requesting advice: shared properties vs other class interactions.

    If you use a public shared field with a public shared property, all instances of the class will point to the same memory location.

    If you use a non-shared field with a not shared property, each instance of the class will store a value unique to the instance.

    Here is a class example using Shared and one without:

    ' Class using Public, Shared fields and properties.

    Public Class PersonWithShare dProperty

    Private Shared mName As String

    Public Shared Property Name() As String

    Get

    Return mName

    End Get

    Set(ByVal Value As String)

    mName = Value

    End Set

    End Property

    End Class

    ----------------------------------------------------------------------------

    ' Class using Public, non-Shared fields and properties.

    Public Class PersonNonShared Property

    Private mName As String

    Public Property Name() As String

    Get

    Return mName

    End Get

    Set(ByVal Value As String)

    mName = Value

    End Set

    End Property

    End Class

    ------------------------------------------------------------------------------------------------------

    Here is some code which uses each class and explains what happens with each.



    ' Use a class with shared fields and properties.

    Dim x As New PersonWithShare dProperty()

    x.Name = "Ted"

    Debug.Write(x.N ame & Environment.New Line) ' Will write Ted.

    Dim y As New PersonWithShare dProperty()

    y.Name = "Mary"

    Debug.Write(y.N ame & Environment.New Line ' Will write Mary.

    Debug.Write(x.N ame & Environment.New Line) ' Will write Mary too because a shared field and property

    ' were used in the PersonWithShare dProperty class.



    ' Use a class without shared fields and properties.

    Dim a As New PersonNonShared Property()

    a.Name = "Ted"

    Debug.Write(a.N ame) ' Will write Ted.

    Dim b As New PersonNonShared Property()

    b.Name = "Mary"

    Debug.Write(b.N ame) ' Will write Mary.

    Debug.Write(a.N ame) ' Will still write Ted because a shared filed and poperty were not used

    ' in the PersonNonShared Property class.



    <mgoold2002@hot mail.com> wrote in message news:1149557671 .829013.266550@ u72g2000cwu.goo glegroups.com.. .[color=blue]
    > Hello. I've just begun programming in VB .NET, and I'm trying to turn
    > all my modules into classes. In order to retrieve/exchange values from
    > one class to another, I initiated New instances of the classes in each
    > class where I needed to retrieve a property or method. I discovered
    > that this reciprocal loading of class instances led to stackoverflow
    > exceptions. I am now remedying this problem by sharing properties
    > among the classes. I ask you though: what's the best way to share
    > methods and properties reciprocally among classes? Any advice is
    > greatly appreciated.
    >[/color]

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Requesting advice: shared properties vs other class interactions.

      MGoold,

      The best method that you ask for is: look everytime how you can avoid shared
      properties and methods. (As public members in a module are); And if you
      really need them, which is seldom, try than to set them in seperate classes.
      How you make such a class you see in the message from Mike.

      You than definitly will see after a while the advantages of OOP

      Cor

      <mgoold2002@hot mail.com> schreef in bericht
      news:1149557671 .829013.266550@ u72g2000cwu.goo glegroups.com.. .[color=blue]
      > Hello. I've just begun programming in VB .NET, and I'm trying to turn
      > all my modules into classes. In order to retrieve/exchange values from
      > one class to another, I initiated New instances of the classes in each
      > class where I needed to retrieve a property or method. I discovered
      > that this reciprocal loading of class instances led to stackoverflow
      > exceptions. I am now remedying this problem by sharing properties
      > among the classes. I ask you though: what's the best way to share
      > methods and properties reciprocally among classes? Any advice is
      > greatly appreciated.
      >[/color]


      Comment

      Working...