dotnet / dotnet/vblang

Using readonly property as target of ByRef

Open
#169 3 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

@jrmoreno1 commented on [Fri Aug 26 2016](https://github.com/dotnet/roslyn/issues/13403)

Microsoft Visual Studio Enterprise 2015
Version 14.0.25123.00 Update 2
Microsoft .NET Framework
Version 4.6.01590

Visual Basic 2015 00322-90150-04967-AA372
Microsoft Visual Basic 2015

**Steps to Reproduce**:
Compile

Dim nlDate As Date? = #01/01/2015#
If (Date.TryParse("07/04/2016", nlDate.Value)) Then
Console.WriteLine(nlDate)
End If

**Expected Behavior**:
Compiler error ('ReadOnly' Property cannot be the target of an assignment)
**Actual Behavior**:
Compiles, and when run the parse succeeds, but the nullable date is unchanged. Checked with VS 2013, and same behavior, so it's not a change due to Roslyn, but I do think it's an error.

---

@gafter commented on [Fri Aug 26 2016](https://github.com/dotnet/roslyn/issues/13403#issuecomment-242882540)

I believe the VB language spec specifically permits this, instead sending a ref to the temporary value that it has read the value into. @AnthonyDGreen Can you please confirm?

---

@markhurd commented on [Fri Aug 26 2016](https://github.com/dotnet/roslyn/issues/13403#issuecomment-242889627)

Because VB.NET doesn't acknowledge `Out` parameters as more than `ByRef`, this makes sense: as far as VB is concerned you might just be expecting to pass _in_ the property as an argument.

---

@paul1956 commented on [Fri Aug 26 2016](https://github.com/dotnet/roslyn/issues/13403#issuecomment-242890278)

Please someone fix VB so it acknowledge "Out" parameters even if it is only for new code. Currently the "Out" attribute is completely ignored. Lack of this makes code less clear and for readonly variables being passed to Out parameters subtle bugs.

---

@jrmoreno1 commented on [Sun Aug 28 2016](https://github.com/dotnet/roslyn/issues/13403#issuecomment-242971693)

@markhurd : ref or out, it doesn't make a difference. Either way you are potentially making an assigment to a readonly value. If you only need the value, then the argument should be ByVal.

---

@jrmoreno1 commented on [Sun Aug 28 2016](https://github.com/dotnet/roslyn/issues/13403#issuecomment-242973361)

@gafter: VB.NET does allow using properties for ByRef arguments, but it shouldn't allow doing so for ReadOnly properties. Your comment prompted me to check readonly fields, and it has the same behavior. I also looked at the IL and there is no attempt to assign to the Property (or the readonly field).

---

@jrmoreno1 commented on [Fri Sep 02 2016](https://github.com/dotnet/roslyn/issues/13403#issuecomment-244418212)

I don't know if VB is scheduled to get readonly locals, but if so it's going to get even more confusing if readonly local variables can't be passed to as a ref argument but readonly field/properties can. And if readonly locals are allowed, that's going to cause a lot of confusion between vb and C#.

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.