dotnet / dotnet/vblang

ByRef Me for Structure

Open
#611 3 comments 2 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

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 VB `Structure` 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 a `ByRef` 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 a `ByRef` parameter.

For example, this VB code, utilizing `ByRef Me`:

Module Program

Structure PDQ
Public P As Integer
Public D As Decimal
Public Q As Long
Sub FooBar()
Program.FooBar(ByRef 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 123 if ByRef Me is used; otherwise 0
Console.WriteLine($"{o.P}")
End Sub

End Module

Would then work the same as `ref this` in this C# code:

class Program
{

struct PDQ
{
public int P;
public decimal D;
public long Q;

public void FooBar()
{
Program.FooBar(ref this);
}
}
static void FooBar(ref PDQ dst)
{
dst.P = 123;
}
static void Main(string[] args)
{
PDQ o = default;
o.FooBar();
Console.WriteLine($"{o.P}"); // Prints 123
}
}

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.