Set doesn't run when accessing a property that is an object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I'm sure this is a simple problem ... for someone!

I have a class which exposes a property which is another object (SmartDate):

Public Property CreateDate() As SmartDate
Get
Return _CreateDate
End Get
Set(ByVal Value As SmartDate) ' this never gets called!
_CreateDate = Value
' raise an event
End Set
End Property

SmartDate is basically a clever Date class - the idea being pinched from
Rocky Lhotkas CSLA!

However, whenever I set the CreateDate it calls the Get and hands me back a
reference to my SmartDate, the properties of the SmartDate are then set and
the Set routine in my object never gets called.

What's the best way round this?

Thanks in advance! Noggin
 
Noggin The Nog said:
I have a class which exposes a property which is another object
(SmartDate):

Public Property CreateDate() As SmartDate
Get
Return _CreateDate
End Get
Set(ByVal Value As SmartDate) ' this never gets called!
_CreateDate = Value
' raise an event
End Set
End Property

SmartDate is basically a clever Date class - the idea being pinched from
Rocky Lhotkas CSLA!

However, whenever I set the CreateDate it calls the Get and hands me back
a
reference to my SmartDate, the properties of the SmartDate are then set
and
the Set routine in my object never gets called.

There must be a problem elsewhere in your code. Can you post the code you
are using to set the 'CreateDate'?
 
The SmartDate object has a Text property:

BusinessObject.CreateDate.Text = Textbox1.Text

The SmartDate then parses the date and attempts to convert it to a date
format, it has other properties like IsDate etc.

When step into the above code it first goes through the CreateDate's Get
function and returns a SmartDate object. The Text property is then set on
this object. Consequently the Set function never runs. It's as though I need
to return a ByVal property rather than a ByRef (which I realise I can't do!)

Thanks!
 
The set will only be called if you assign a new SmartDate object to the
CreateDate property. It will not be called when you change the
properties of the CreateDate.

This code will cause the Set to be called:

BusinessObject.CreateDate = New SmartDate(...)

This code will NOT cause the Set to be called:
BusinessObject.CreateDate.Text = Textbox1.Text

HTH
 
I don't really want to make a new SmartDate each time (in this case it
doesn't, but in others it could involve setting lots of properties on the new
object just to change to one I want to). So I assume the only other way out
of this is to take the SmartDate object out, change what I want, and put it
back:

Dim sdTemp As SmartDate
sdTemp = BusinessObject.CreateDate
sdTemp.Text = Textbox1.Text
BusinessObject.CreateDate = sdTemp

Is that really the most efficient way of doing this? Am I right in saying
what I'm trying to do is have a ByVal property?

Thanks everyone for your help!
 
Noggin The Nog said:
The SmartDate object has a Text property:

BusinessObject.CreateDate.Text = Textbox1.Text

In this code you are not chaning the property' value, which would mean that
you assign a new value to 'CreateDate'. Instead, you are setting a property
/on/ the 'CreateDate''s value. To do that, the value of the 'CreateDate'
property is determined (and thus the 'Get' part of the 'CreateDate' property
is called) and then the property of the 'CreateDate' object is changed.
 
I was merely trying to point out that in the scenario you described,
the Set portion of your property was *not supposed* to run.

As Herfried pointed out, the only time the Set portion runs is when you
assign a *new* SmartDate to the property. Since you're not doing that
here, the Set will not run.

If all you're trying to do is change a property of the
BusinessObject.CreateDate property, then what you originally had is
appropriate.

In other words the following code should be correct:
BusinessObject.CreateDate.Text = Textbox1.Text
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top