[Proposal] Dim ByRef to allow references to Structure's
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
In VB today, there are two ways to make a reference to a Structure (value type):
1. via ByRef parameter of Function or Sub
2. as the objectExpression of a With statement
Signficant performance benefits are possible as demonstrated by the With statement (see "Avoiding Requalification" at https://msdn.microsoft.com/en-us/library/aa289513(v=vs.71).aspx). Of course today one could use Class instead of Structure, or perhaps an Interface trick, and get some performance benefit those ways, but really that's of no use if the Structure in question is somebody else's (outside of one's control).
My suggestion is to extend the Dim statement:
` Dim ByRef myRef = objectExpression
`
This would be only as an automatic variable in a Sub or Function, never as a Field in a Class or Module. The semantics would seem the same as a ByRef parameter to a Sub/Function. Behind the scenes, it would work about the same as a With ... End With, except that what is the _objectExpression_ of the With now has an indentifier (e.g. "myRef"), and the End With is implied by the scope and lifetime of that identifier. During its lifetime, it can only reference its original _objectExpression_. I'm thinking that this Dim ByRef idea can be real handy when dealing with arrays of Structure.
Hence something like this:
```
For i As Integer = 0 to n - 1
Dim ByRef item_i = A(i)
item_i.member1 = expression
item_i.member2 = expression
Next
```
Would amount to the same as:
```
For i As Integer = 0 to n - 1
With A(i)
.member1 = expression
.member2 = expression
End With
Next
```
But here's the so what - what if one is working with two or more sets of Structure types? With statement is limited - only one Structure at a time! Nested With's are of no help. Dim ByRef would allow for things such as this:
```
For i As Integer = 0 to n - 1
Dim ByRef item_A = A(i)
Dim ByRef item_B = B(i + 1)
With item_A
.member1 = item_B.member1
.member2 = item_B.member2
End With
Next
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.