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
--
Message posted via
http://www.dotnetmonster.com