Skip to content

Using readonly property as target of ByRef #169

@gafter

Description

@gafter

@jrmoreno1 commented on Fri Aug 26 2016

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? = #1/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

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

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

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

@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

@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

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#.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions