-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ByRef Me for Structure #611
Comments
I like this. |
I would argue that the underlying behavior is broken (or the documentation is, at the very least, misleading)... Module ByrefTest
Structure PDQ
Public P As Integer
Public D As Decimal
Public Q As Long
Sub FooBar()
ByrefTest.FooBar(Me)
End Sub
End Structure
Sub FooBar(ByRef dst As PDQ)
dst.P = 123
End Sub
Sub Main()
Dim o As PDQ = Nothing
o.FooBar()
' Prints 0... expecting it to be 123.
Console.WriteLine($"{o.P}")
End Sub
End Module I slightly modified the original code to reflect the point that, according to the documentation found at: It states that the I don't see the need to have to specify |
To further illustrate why I believe this is a bug... Module ByrefTest
Structure PDQ
Public P As Integer
Public D As Decimal
Public Q As Long
Sub FooBar()
ByrefTest.FooBar(Me)
End Sub
End Structure
Sub FooBar(ByRef dst As PDQ)
dst.P = 123
End Sub
Sub Main1()
Dim o As PDQ = Nothing
o.FooBar() ' Call FooBar within the structure...
Console.WriteLine($"{o.P}") ' Output: 0
FooBar(o) ' Call FooBar outside of the structure...
Console.WriteLine($"{o.P}") ' Output: 123
End Sub
End Module If I modify the sample so that FooBar is called slightly differently, I get two different results... event though I'm essentially executing the same code. The only difference is the usage of |
Per https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass : "
Me
behaves like either an object variable or a structure variable referring to the current instance."However, the
Me
of a VBStructure
will always be a copy of the instance when it's a parameter to a call, and never the instance itself, including where the call is for aByRef
parameter.This proposal is to allow the expression
ByRef Me
to indicate when a reference to the instance is desired instead of its copy when targeting aByRef
parameter.For example, this VB code, utilizing
ByRef Me
:Would then work the same as
ref this
in this C# code:The text was updated successfully, but these errors were encountered: