|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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. 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. Show quoteHide quote <mgoold2***@hotmail.com> wrote in message news:1149557671.829013.266550@u72g2000cwu.googlegroups.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. > 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 <mgoold2***@hotmail.com> schreef in bericht Show quoteHide quote news:1149557671.829013.266550@u72g2000cwu.googlegroups.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. >
why is dataAdapter.UpdateCommand not updating?
Close form if no user action including mouse move over form. Can't create AutoFiltered Excel worksheet using VB.NET Label problem Accessing inherited variables Textbox Lines Limit How to call BeginInvoke for a sub that has a parameter ? Define an object??? Use a System.Type in Generics? Newbie needs help with datagrid |
|||||||||||||||||||||||