dotnet / dotnet/vblang

Warn when using <Out> without ByRef

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

Description

Since VB 9, using `ByVal` is no longer mandatory, making method declaration syntax quite a bit cleaner. For `ByRef`, a keyword is still required, and unlike with C#, VB doesn't have an out-specific keyword. Instead, you need to use _both_ `ByRef` _and_ apply the `OutAttribute`, for example:

```vb
Private Function TryGetValue(obj As Object, ByRef result as Integer) As Boolean

End Function
```

This makes it easy to introduce a bug* that doesn't become apparent until runtime:

```vb
Private Function TryGetValue(obj As Object, result as Integer) As Boolean

End Function
```

This code looks fine at first glance, But the `` attribute doesn't actually do anything meaningful (best as I can tell), because `result` is actually implicitly `ByVal` now, not `ByRef`.

Thus, I propose that the compiler annotate this as a warning:

>`OutAttribute` used on `ByVal` method parameter. Consider using `ByRef`.

*) Well, this has embarrassingly happened to me, anyway.

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.