Question

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!

Was it helpful?

Solution 2

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.

OTHER TIPS

The ByRef modifier cannot be used in property setters.

It can only be declared in method'ss and constructor's signatures. There it specifies that the underlying variable of an argument can be modified in the called method.

In the following example the ByRef modifier causes the field named "underlyingVariable" to take the new value. By passing the variable by Value, it would not get modified and therefore would be null:

Private underlyingVariable As Object

Public Sub New()
    MyMethod(underlyingVariable)
End Sub

Public Sub MyMethod(ByRef o As Object)
    o = New Object()
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top