473,508 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Jun 6 '06 #1
2 1453
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3523
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
11
3336
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
4
3332
by: Rubbrecht Philippe | last post by:
Hi there, I would like to develop an interface that when implemented in a class requires a number of shared properties or methods to be available. It seems a Shared Member can not be used as...
4
1015
by: Hylton | last post by:
Hi Long Post - Please read to end to understand my question. I am writing a telnet server in VB.net (code samples lower down) I connect two hyperterm sessions to the code. (2 telnet...
5
1568
by: Simon | last post by:
Hi all, We have an ASP.NET 1.1 application running on IIS6 on Server 2003. Most of the base objects we are using in this application are taken from a windows application also written by us. We...
8
1510
by: george.leithead | last post by:
Hi all, I'm looking for some advice on how best to achitect the following requirement. I'm basically writing a Fantasy Football (FF) Web site, and would like to have it fully OO and have it...
8
2000
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class:...
6
1488
by: wingphil | last post by:
Hi there, I have a shared method in a base class, and I need to know which subclass it has been called from. So for example Public Mustinherit Class BaseClass Public Shared Sub SharedMethod...
3
1617
by: shapper | last post by:
Hello, I created a simple class as follows: Public Class HelloWorld Public Function SayMessage() As String Return "Hello World!" End Function
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.