docs
docs copied to clipboard
Misleading details about Structure and With statement
[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.
- ID: d4099464-c3a0-f9f0-ac92-04f15512e8ea
- Version Independent ID: 0fd42897-47a8-cb07-e8b1-458131eaf78d
- Content: With...End With Statement - Visual Basic
- Content Source: docs/visual-basic/language-reference/statements/with-end-with-statement.md
- Product: dotnet-visualbasic
- GitHub Login: @KathleenDollard
- Microsoft Alias: kdollard