docs icon indicating copy to clipboard operation
docs copied to clipboard

Misleading details about Structure and With statement

Open rskar-git opened this issue 5 years ago • 0 comments

[Enter feedback here] Per article: "When using a structure, you can only read the values of members or invoke methods, and you get an error if you try to assign values to members of a structure used in a With...End With statement."

That is not entirely true. For example, members of a structure used in a With...End With statement most certainly can be assigned:

Structure Xyz
    Dim x As Short
    Dim y As Integer
    Dim z As Long
    Dim u As Single
    Dim v As Double
End Structure

    Dim pdq(2) As Xyz
    With pdq(1)
        .x = 123
        .y = 4321
        .z = 999999
        .u = 1.23456
        .v = 123456789.87654321
    End With

This is behavior carried over from VBA/VB6 - user data types (e.g. Type Xyz in its syntax) could always be assigned this way.

However, the read-only behavior mentioned by the article does occur if the value-type in question cannot be regarded as something reference-able, e.g.

    With (pdq(1))  ' Note the parentheses - in VB.NET, this cuts all ties to the original reference-able source
        .x = 123  ' BC30068
       ' etc. ...
    End With

Hence, it's not so much "when using a structure", but "when using a structure returned by value from a function or property or operator".


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

rskar-git avatar Sep 05 '20 13:09 rskar-git