Search code examples
vb.netpropertiesbyref

WriteOnly Property ByRef


I don't understand how I can pass an argument byref in VB.NET.

I tried this:

Private m_Form As frmMain

Public WriteOnly Property MyForm() As Form

    Set(ByRef value As Form)
        m_Form = value
    End Set

End Property

But VB.NET does not like the "Byref" argument. Can somebody help?

Thank you!


Solution

  • You cannot pass things by reference with setters. It must be ByVal. From the VB.NET Specification:

    §9.7.2 If a parameter list is specified, it must have one member, that member must have no modifiers except ByVal, and its type must be the same as the type of the property.

    I don't think it particularly makes sense to use ByRef in a property setter. Using ByRef implies that you may want to change the reference of what invokes the setter.

    Form is a reference type (class), so you want to pass it by value. Otherwise you are passing a reference of a reference type.