|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How do I set a property which is an object?Public Class myObj Dim x as myOtherObj Public Property otherObject() As myOtherObj Get otherObject = x End Get Set(ByVal Value As myOtherObj) ' x is initially nothing x = Value ' x is still nothing even though Value is not! End Set End Property End Class When I step thru Set() with a debugger, x starts out as nothing and remains that way, even though value is not nothing. Why is this? And what's the 'proper' way to fix Set() ? Thanks in advance for your help, Hal Heinrich VP Technology Aralan Solutions Inc. Read through the following code and you'll get a pretty clear idea on how
objects can contain references or instances of other objects. In my example "MyClassA" is the the object that is subscribing to "MyClassB". Your problem was that you were NOT instantiating a second object to be contained in the first object. Instead what you were doing was returning a reference to the object -- with no actual object present in the variable. My example will probably throw a Object Type/Cast exception, but it's for illustrative purposes. In any case, you can see how to affect properties of one object in another object. The other thing I thought I'd point out is that the syntax you were using for the Object accessor in your class is from VB classic, and although it is supported, it makes it much more confusing to read through your code. Use the "Return" keyword and reference the actual property itself. FYI: a good practice is to create object properties with the "Private" keyword for security, and create a public accessor that can read or write from/to the property. I hope this helps. Public Class MyClassA 'property to contain objectPrivate _ObjectContainer As Object 'get/set property methods Public Property ObjectContainer() As Object Get Return _ObjectContainer End Get Set(ByVal Value As Object) _ObjectContainer = Value End Set End Property 'constructor Public Sub New() 'instantiate a new object and 'assign it to the container property ObjectContainer = New MyClassB() 'useage: Console.WriteLine("My name is: " & ObjectContainer.MyName()) Console.WriteLine("My gender is: " & ObjectContainer.MyGender()) ObjectContainer.MyName = "Jane Smith" ObjectContainer.MyGender = "Female" Console.WriteLine("My new name is: " & ObjectContainer.MyName()) Console.WriteLine("My new gender is: " & ObjectContainer.MyGender()) End Sub End Class Public Class MyClassB Private _MyName As String = "John Smith" Private _MyGender As String = "Male" 'create get/set property methods Public Property MyName() As String Get Return _MyName End Get Set(ByVal Value As String) _MyName = Value End Set End Property Public Property MyGender() As String Get Return _MyGender End Get Set(ByVal Value As String) _MyGender = Value End Set End Property 'constructor Public Sub New() 'instantiate a new End Sub Public Sub End Class The following should work:
dim obj as new myObj dim otherobj as new myOtherObj (don't know what this but can be a class, etc.) obj.otherObject = otherobj Public Class myObj Private x as myOtherObj Public Property otherObject() As myOtherObj Get otherObject = x End Get Set(ByVal Value As myOtherObj) ' x is initially nothing x = Value ' x is still nothing even though Value is not! End Set End Property End Class Show quoteHide quote "Hal Heinrich" wrote: > I have the following class: > > Public Class myObj > > Dim x as myOtherObj > > Public Property otherObject() As myOtherObj > Get > otherObject = x > End Get > Set(ByVal Value As myOtherObj) > ' x is initially nothing > x = Value > ' x is still nothing even though Value is not! > End Set > End Property > > End Class > > When I step thru Set() with a debugger, x starts out as nothing and remains > that way, even though value is not nothing. > > Why is this? > > And what's the 'proper' way to fix Set() ? > > Thanks in advance for your help, > Hal Heinrich > VP Technology > Aralan Solutions Inc. I wrote some code, shwon below, to state my problem more explicitly.
When I tested it, it ran fine - my problem went away. When the code below reaches the messageBox statement, "I'm contained -858" is displayed - the effect I want. I'll post this problem again after some more research. Thanks for your responses, Hal Heinrich Public Class containedClass Private _a As String Private _b As Integer Public Property a() As String Get Return _a End Get Set(ByVal Value As String) _a = Value End Set End Property Public Property b() As Integer Get Return _b End Get Set(ByVal Value As Integer) _b = Value End Set End Property End Class Public Class containerClass Private _x As String Private _y As Integer Private _cc As containedClass.containedClass Public Property cc() As containedClass.containedClass Get Return _cc End Get Set(ByVal Value As containedClass.containedClass) _cc = Value End Set End Property Public Property x() As String Get Return _x End Get Set(ByVal Value As String) _x = Value End Set End Property Public Property y() As Integer Get Return _y End Get Set(ByVal Value As Integer) _y = Value End Set End Property End Class Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim innerClass As New containedClass.containedClass, outerClass As New containerClass.containerClass innerClass.a = "I'm contained" innerClass.b = -858 outerClass.x = "I contain objects" outerClass.y = 57 outerClass.cc = innerClass innerClass = Nothing MessageBox.Show(outerClass.cc.a & " " & outerClass.cc.b.ToString) End Sub
2D array of string
Equivalent to Mid function but read from right of the string Opening a browser from vb.net from need clarification from M S Herfried K. Wagner 2GB memory limit Bitmap and MemoryStream Command Line Compiling with license file Binding combo box How to set the default value of a date picker to nothing (blank) DateTimePicker |
|||||||||||||||||||||||