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

How do I set a property which is an object?

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 21 '05 #2

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

"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.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.