Get Property also runs Set?!

  • Thread starter Thread starter Alan Gillott
  • Start date Start date
A

Alan Gillott

While line by line debugging some code I noticed that whenever I run through
Property Set it also subsequently calls Property Set with the having been
Gotten data? Can anyone explain.
A
 
Alan Gillott said:
While line by line debugging some code I noticed that whenever I run
through Property Set it also subsequently calls Property Set with the
having been Gotten data? Can anyone explain.

Post your implementation of the property.

Maybe you are recursively calling the 'Set' accessor of the property?

\\\
Public Property Foo() As Object
Set(ByVal Value As Object)
Me.Foo = Vaue
End Set
...
End Property
///

This won't work. Use the code below instead:

\\\
Private m_Foo As Object

Public Property Foo() As Object
Set(ByVal Value As Object)
m_Foo = Vaue
End Set
...
End Property
///
 
Post your implementation of the property.

Maybe you are recursively calling the 'Set' accessor of the property?

\\\
Public Property Foo() As Object
    Set(ByVal Value As Object)
        Me.Foo = Vaue
    End Set
    ...
End Property
///

This won't work.  Use the code below instead:

\\\
Private m_Foo As Object

Public Property Foo() As Object
    Set(ByVal Value As Object)
        m_Foo = Vaue
    End Set
    ...
End Property
///

Correction against mistype: Change "vaue"s to "value" in Herfried's
post.
 
If that's the most significant contribution you have to a thread then don't
bother.

It was painfully obvious to anyone with half a brain exactly what Herfried
was demonstrating.


Post your implementation of the property.

Maybe you are recursively calling the 'Set' accessor of the property?

\\\
Public Property Foo() As Object
Set(ByVal Value As Object)
Me.Foo = Vaue
End Set
...
End Property
///

This won't work. Use the code below instead:

\\\
Private m_Foo As Object

Public Property Foo() As Object
Set(ByVal Value As Object)
m_Foo = Vaue
End Set
...
End Property
///

Correction against mistype: Change "vaue"s to "value" in Herfried's
post.
 
If that's the most significant contribution you have to a thread then don't
bother.

It was painfully obvious to anyone with half a brain exactly what Herfried
was demonstrating.









Correction against mistype: Change "vaue"s to "value" in Herfried's
post.- Hide quoted text -

- Show quoted text -

I just wanted to pay attention for OP not to get an annoying error.
Saying this of you is completely nonsense and against goodwill that i
just wanted to warned the OP with the purpose of being complementary,
nothing more.
 
That's just sanctimonious claptrap.

As the OP is quite capable of single-stepping through his code, I am
absolutely certain that he is quite capable of resolving a simple typo
without someone attempting to save him from himself.

Such posts do nothing more than clutter up threads, and the newsgroup in
general, and add no value to the discussion at hand whatsoever.


If that's the most significant contribution you have to a thread then
don't
bother.

It was painfully obvious to anyone with half a brain exactly what Herfried
was demonstrating.









Correction against mistype: Change "vaue"s to "value" in Herfried's
post.- Hide quoted text -

- Show quoted text -

I just wanted to pay attention for OP not to get an annoying error.
Saying this of you is completely nonsense and against goodwill that i
just wanted to warned the OP with the purpose of being complementary,
nothing more.
 
Alan said:
While line by line debugging some code I noticed that whenever I run
through Property Get it also subsequently calls Property Set with the
having been Gotten data? Can anyone explain.

Are you by any chance passing calling Property Get as part of a call into a
procedure, passing it in as a ByRef parameter?

If so, when the procedure finishes executing, it transfers the value back to
the property (because you passed it by reference, not by value).

For example, take the following code:

\\\
Private Sub MySub
DoSomething(MyProperty)
End Sub

Public Sub DoSomething(ByRef value As String)
value = "new value"
End Sub

Public Property MyProperty() As String
Get
Debug.Print("Get")
Return "return value"
End Get
Set(ByVal value As String)
Debug.Print("Set: " & value)
End Set
End Property
///

If you call MySub, it'll call MyProperty Get to retrieve the string "return
value". This is then passed By Reference into DoSomething, which changes the
string to "new value". When DoSomething finishes, VB automatically and
implicitly calls MyProperty Set in order for the updated value to be passed
back into the property. You'll see that the "value" variable in the Property
Set now contains the string "new value".

This has confused me a couple of times in the past -- there's no obvious
programmatic reason why the Property Set gets called. But that's why it is.

Does that explain it? Or are you seeing something else?

If this is it, you can stop either stop it by passing the parameter By Value
(which is always the best thing to do unless you really do want
modifications to the value within the called procedure to be changed in the
variable in the calling procedure too), or just accept that this is what it
needs to do in order for the variable in the calling procedure to be
updated.

(Incidentally, this is different to how things worked in the COM world. If
you do the same thing in VB6, you'll see that the updated By Ref parameter
value does not get passed back to the property -- this has confused me in
the past too! I do think the .NET way is the more logical and consistent
behaviour)

HTH,
 
Stephany Young said:
That's just sanctimonious claptrap.

As the OP is quite capable of single-stepping through his code, I am
absolutely certain that he is quite capable of resolving a simple typo
without someone attempting to save him from himself.

Such posts do nothing more than clutter up threads, and the newsgroup in
general, and add no value to the discussion at hand whatsoever.




I just wanted to pay attention for OP not to get an annoying error.
Saying this of you is completely nonsense and against goodwill that i
just wanted to warned the OP with the purpose of being complementary,
nothing more.
Sort of like top poster's?

Galen
 
Stephany said:
That's just sanctimonious claptrap.

Such posts do nothing more than clutter up threads, and the newsgroup
in general, and add no value to the discussion at hand whatsoever.

And your post is somehow adding value? Talk about pots calling kettles black...
 
Code;

Public Property Title() As String
Get
Return c_Title
End Get
Set(ByVal value As String)
c_Title = value
End Set
End Property
End Class

Seems simple enough to me!
A
 
Alan,

Did you write somewhere in that class?

c_Title = Title

Cor

Alan Gillott said:
Code;

Public Property Title() As String
Get
Return c_Title
End Get
Set(ByVal value As String)
c_Title = value
End Set
End Property
End Class

Seems simple enough to me!
A
 
No. That was my first error in my first ever .net class: changing title to
value actually made the class work properly.

Cor Ligthert said:
Alan,

Did you write somewhere in that class?

c_Title = Title

Cor
 
Back
Top