dotnet / dotnet/vblang

Support consuming "ByRef Returns" with the `With` syntax

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

Description

The following code crashes Visual Studio.
```vb
' Important: save your work (Ctrl+Shift+S) or start a new Visual Studio instance
' before trying the code.
With {(K:=1, X:=2, B:=3)}.AsSpan()(0)
Dim Y = .K * .X + .B
Console.WriteLine($"Y = { .K} × { .X} + { .B} = {Y}")
End With
```
The IDE won't crash if the VB compiler supports consuming "ByRef Returns" with the `With` syntax.

Edit:
A real-world example:
```vb
' Original code: https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.bitmap.lockbits

Private Sub LowAllocationSuperFastLockUnlockBitsExample(e As PaintEventArgs)
' Create a new bitmap.
Dim bmp As New Bitmap("c:\fakePhoto.png")
If bmp.PixelFormat <> PixelFormat.Bgra32 Then Throw New NotSupportedException

' Lock the bitmap's bits.
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat)

' Get the span of the back buffer.
' Function GetSpanBgra32WithCSharp(bmpData As BitmapData) As Span(Of Color)
Dim colors = GetSpanBgra32WithCSharp(bmpData)

' Set every R value to 255, G value to 128. A 32bpp image will look orange.
For counter = 0 To colors.Count - 1
With colors(counter)
.R = 255
.G = 128
End With
Next

' Unlock the bits.
bmp.UnlockBits(bmpData)

' Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 150)
End Sub
```

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.