I have observed behavior in VB.net where property definition tools are called more often than it seems necessary, combined with calls to the sister setter method.
Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Console.WriteLine("Calling WorkReferenceTypeByReference") WorkReferenceTypeByReference(ReferenceTypeData) Console.WriteLine("Called WorkReferenceTypeByReference") Console.WriteLine("Calling WorkReferenceTypeByValue") WorkReferenceTypeByValue(ReferenceTypeData) Console.WriteLine("Called WorkReferenceTypeByValue") End Sub Public Sub WorkReferenceTypeByReference(ByRef ref As Point) Dim b As Point = New Point(4, 4) + ref Console.WriteLine(" adding (4,4) to " & ref.ToString) End Sub Public Sub WorkReferenceTypeByValue(ByVal ref As Point) Dim b As Point = New Point(4, 4) + ref Console.WriteLine(" adding (4,4) to " & ref.ToString) End Sub Private m_ReferenceType As Point = New Point(0, 0) Public Property ReferenceTypeData As Point Get Console.WriteLine(" Calling ReferenceTypeData getter") Console.WriteLine(" returning: " & m_ReferenceType.ToString) Return m_ReferenceType End Get Set(ByVal value As Point) Console.WriteLine(" Calling ReferenceTypeData setter") Console.WriteLine(" value = " & value.ToString) m_ReferenceType = value End Set End Property End Class
The previous code returns the following output to the console
Calling WorkReferenceTypeByReference Calling ReferenceTypeData getter returning: {X=0,Y=0} adding (4,4) to {X=0,Y=0} Calling ReferenceTypeData setter value = {X=0,Y=0} Called WorkReferenceTypeByReference Calling WorkReferenceTypeByValue Calling ReferenceTypeData getter returning: {X=0,Y=0} adding (4,4) to {X=0,Y=0} Called WorkReferenceTypeByValue
Note the fake call of the property setting tool after the method is executed. I assume that this behavior is produced as a security measure against unintentional changes to the underlying property, despite the fact that this is potentially an intention.
This behavior when using ByRef vs ByVal can be easily solved by choosing the appropriate ByVal keyword, but the latter recently noticed a more insidious behavior that caused the callback stack to overflow, because the setter call updated the value, which is called only the recipient.
Public Sub DoSomething() Dim a As New CustomObject(anotherObject.AProperty(getterArgument)) End Sub Public Class AnotherObject Public Property AProperty as SomeType Get ' Get value End Get Set ' Set value, call DoSomething End Set End Property End Class
In the previous example, calling DoSomething () starts the getproperty AProperty method, but then after this use it starts the setter method, which programmatically calls DoSomething () again. This is an automatic setter call that puzzles me.