Requesting advice: shared properties vs other class interactions.

M

mgoold2002

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.
 
M

Mike McIntyre

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 PersonWithSharedProperty

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 PersonNonSharedProperty

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 PersonWithSharedProperty()

x.Name = "Ted"

Debug.Write(x.Name & Environment.NewLine) ' Will write Ted.

Dim y As New PersonWithSharedProperty()

y.Name = "Mary"

Debug.Write(y.Name & Environment.NewLine ' Will write Mary.

Debug.Write(x.Name & Environment.NewLine) ' Will write Mary too because a shared field and property

' were used in the PersonWithSharedProperty class.



' Use a class without shared fields and properties.

Dim a As New PersonNonSharedProperty()

a.Name = "Ted"

Debug.Write(a.Name) ' Will write Ted.

Dim b As New PersonNonSharedProperty()

b.Name = "Mary"

Debug.Write(b.Name) ' Will write Mary.

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

' in the PersonNonSharedProperty class.
 
C

Cor Ligthert [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top