473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I set a property which is an object?

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
2 1000
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Aaron | last post by:
Hi, I've seen javascript code where a constructor function is passed an argument "document", and inside the function itself the assignment "this.document = document;" is made. This is the code...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
3
by: MattC | last post by:
Hi, I found this code somewhere on the net and need to make some alterations. public class GenericSorter : IComparer { String sortProperty; bool sortOrder; public GenericSorter(String...
10
by: dgk | last post by:
I'm searching for a way to determine if control values have changed. For some it's easy, such as TextBox.Modified. But looking at the radiobutton I come across IsMirrored. Curious, I look to the...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
0
by: Memfis | last post by:
While I was looking through the group's archives I came across a discussion on doing properties (as known from Delphi/C#/etc) in C++. It inspired me to do some experimenting. Here's what I came up...
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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,...

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.