By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,205 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,205 IT Pros & Developers. It's quick & easy.

Requesting advice: shared properties vs other class interactions.

P: n/a
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.

Jun 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

<mg********@hotmail.com> wrote in message news:11**********************@u72g2000cwu.googlegr oups.com...
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.

Jun 6 '06 #2

P: n/a
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

<mg********@hotmail.com> schreef in bericht
news:11**********************@u72g2000cwu.googlegr oups.com...
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.

Jun 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.