Optional ByRef - Wrong in VB.NET?

Public Function Foo(ByRef a As AClass, _
                    Optional ByRef b As BClass = Nothing, _ 
                    Optional ByRef c As CClass = Nothing) As XClass

Error: optional parameters must specify a default value.

Was there a das?

+3
source share
2 answers

This compiler is great for me with empty classes. It seems, I think, that you have an error in another line or definition of parameter classes.

Public Class Class1

    Public Function Foo(ByRef a As aclass, _
                        Optional ByRef b As bclass = Nothing, _
                        Optional ByRef c As cclass = Nothing) As xclass
        Return Nothing
    End Function

End Class

Public Class aclass    
End Class

Public Class bclass    
End Class

Public Class cclass    
End Class

Public Class xclass
End Class
+3
source

If this is the template you want to use, I would recommend creating an overload that does not require these parameters:

Public Function Foo(ByRef a As AClass) As XClass
    Return Foo(a, Nothing, Nothing)
End Function
Public Function Foo(ByRef a As AClass, ByRef b As BClass, ByRef c As CClass) As XClass
    '//Return XClass here
End Function
+1
source

Source: https://habr.com/ru/post/1786931/