dotnet / dotnet/vblang

Support null-conditional operator in assignment statements.

Open
#239 6 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 null-conditional operator was an awesome recent addition to VB and I hope it can be extended to the following:

```vb
Public Module Module1
Public Class CTest
Public Value As String
End Class

Public Sub Main()
Dim test As CTest

test?.Value = "foo" '<--- compiler doesn't like this.
End Sub
End Module
```

Compiler says:

![vs_null_conditional_assignment](https://user-images.githubusercontent.com/20465797/34469660-23da27c2-ef35-11e7-98d8-2ded4735566e.png)

Unless I am missing something, couldn't this:

```vb
test?.Value = "foo"
```

...be treated as this:

```vb
If test IsNot Nothing Then
test.Value = "foo"
End If
```

Without this ability today, **if you want to set a member on a possibly-null object reference, you have to write a method that takes a parameter and sets the value, then call that method using the null-conditional operator (which feels like a lot of ceremony for a simple thing like setting a field):**

```vb
Public Module Module1
Public Class CTest
Public Value As String

Public Sub SetValue(ArgValue As String) '<-- must define this.
Me.Value = ArgValue
End Sub
End Class

Public Sub Main()
Dim test As CTest '<--- test is null here.

test?.SetValue("foo") '<--- compiler is OK with this.
test?.Value = "foo" '<--- ...but doesn't like this.
End Sub
End Module
```

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.