Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6697355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:27:01+00:00 2026-05-26T06:27:01+00:00

I have observed a behaviour in VB.net where property setters get called more often

  • 0

I have observed a behaviour in VB.net where property setters get called more often than seems necessary, in conjunction 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 to the console the following output

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 spurious call to the property setter following the method execution. I am supposing this behaviour is produced as a safety measure against inadvertently modifying the underlying property, despite this potentially being the intent.

This behaviour in the case of ByRef vs ByVal usage is easily solved by choosing appropriate ByVal keyword, however hase recently noticed a more insidious behaviour, one that has caused a stack overflow of repeated calls, since the setter call would update a value that called the getter only.

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() would fire the AProperty getter method, but then after that usage, would fire the setter method, which by program logic calls DoSomething() again. It is the automatic calling of the setter that puzzles me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:27:02+00:00Added an answer on May 26, 2026 at 6:27 am

    This is, in fact, a feature of VB.Net. In your code, you are passing a property, not a variable, by reference. Strictly speaking, passing a property ByRef is not possible, because ByRef needs a reference to a variable. However, the compiler automatically creates a temporary local on your behalf and passes it to the method for you. Because the method may change the ByRef parameter, which is now the compiler-generated temporary and not your property, the compiler then inserts a call to the setter. Essentially, something like this happens:

    Dim temp = Me.ReferenceTypeData
    Me.WorkReferenceTypeByReference(temp)
    Me.ReferenceTypeData = temp
    

    Other languages, such as C#, do not allow passing a property by reference (rightly so from the strict definition of parameter passing) and instead require you to write the equivalent of the above code yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have observed that windowWillClose is called when the user closes a window but
I have observed some unexpected or at least not-perfectly-matching-my-needs behaviour of textboxes bound to
Does Java 6 consume more memory than you expect for largish applications? I have
I have a view (actually, it's a table valued function, but the observed behavior
I have observed that, very infrequently, Internet Explorer (7 or 8, it does not
Let's have a model public class Model { public int Number { get; set;
I have noticed an undesirable behavior with .net winforms applications. I have a wide
I have a build script (.bat) that invokes MSBUILD ( C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\msbuild.exe ) that used
We have an ASP.NET application that queues some of its long-running operations (generating reports,
I'm seeing some quirky behaviour with Cocoa's KVC/KVO and bindings. I have an NSArrayController

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.