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,