views:

42

answers:

1

Consider the following:

Public Module Extensions
    <Extension()> _
    Public Sub Initialize(ByRef Target as SomeClass, ByVal SomeParam as Something )
        ...
        Target  = SomethingElse
    end Sub
End Module

Class SomeClass
...

    sub New(ByVal SomeParam as Something )
       Me.Initialize(SomeParam)
    end sub

    sub New()

    end sub

End Class


'Case 1: Doesnt Work...why????:

    Dim foo as new SomeClass(SomeParam) 'foo remains uninitialized


'Case 2: Does Work:
    Dim foo as new SomeClass()
    foo.Initialize(SomeParam) 'foo is initialized

Question: Why is Case 1 failing to initialize the object as expected?

+3  A: 

The problem here is that VB.Net supports multiple ways of using ByRef parameters. I did a detailed explanation of the types in a recent blog entry

What's happening here is that Me is not an assignable value. So instead of passing Me as a byRef parameter, the VB compiler will instead pass a temporary. This is loosely known as "Copy Back ByRef". It effectively generates the following code

Dim temp = Me
Initialize(temp, SomeParam)

There is no way to work around this in the case of Me because it's not assignable. In the case of the local variable foo though this works as expected because foo is a valid ByRef value.

JaredPar
WOW...that edge case took about 30 minutes of my life. Thanks for the detailed explaination.
Achilles