Skip to content

Commit 4c0c57f

Browse files
CopilotBillWagnergewarren
authored
Fix misleading documentation about structure assignments in With...End With statements (#48222)
* Initial plan * Fix misleading documentation about structure assignments in With...End With statements Co-authored-by: BillWagner <[email protected]> * Add snippets.5000.json file to fix sample build configuration Co-authored-by: BillWagner <[email protected]> * Update vbproj to target .NET Framework 4.8 Co-authored-by: BillWagner <[email protected]> * Update docs/visual-basic/language-reference/statements/with-end-with-statement.md Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]> Co-authored-by: Genevieve Warren <[email protected]>
1 parent c470eca commit 4c0c57f

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

docs/visual-basic/language-reference/statements/with-end-with-statement.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ms.assetid: 340d5fbb-4f43-48ec-a024-80843c137817
1919
---
2020
# With...End With Statement (Visual Basic)
2121

22-
Executes a series of statements that repeatedly refer to a single object or structure so that the statements can use a simplified syntax when accessing members of the object or structure. 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.
22+
Executes a series of statements that repeatedly refer to a single object or structure so that the statements can use a simplified syntax when accessing members of the object or structure.
2323

2424
## Syntax
2525

@@ -49,7 +49,7 @@ If your code accesses the same object in multiple statements, you gain the follo
4949

5050
- You make your code more readable by eliminating repetitive qualifying expressions.
5151

52-
The data type of `objectExpression` can be any class or structure type or even a Visual Basic elementary type such as `Integer`. If `objectExpression` results in anything other than an object, you can only read the values of its 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. This is the same error you would get if you invoked a method that returned a structure and immediately accessed and assigned a value to a member of the function’s result, such as `GetAPoint().x = 1`. The problem in both cases is that the structure exists only on the call stack, and there is no way a modified structure member in these situations can write to a location such that any other code in the program can observe the change.
52+
The data type of `objectExpression` can be any class or structure type or even a Visual Basic elementary type such as `Integer`. If `objectExpression` is a structure, the ability to assign to its members depends on whether the structure expression is referenceable. You can assign to members of structures that are directly referenceable (such as variables, array elements, or fields), but you get an error if you try to assign values to members of structures that are returned by value from functions, properties, or operators, or when parentheses are used to cut reference ties (for example, `With (structureVariable)`). This is the same error you would get if you invoked a method that returned a structure and immediately accessed and assigned a value to a member of the function's result, such as `GetAPoint().x = 1`. The problem in both cases is that the structure exists only on the call stack, and there is no way a modified structure member in these situations can write to a location such that any other code in the program can observe the change.
5353

5454
The `objectExpression` is evaluated once, upon entry into the block. You can't reassign the `objectExpression` from within the `With` block.
5555

@@ -78,6 +78,12 @@ The following example nests `With…End With` statements. Within the nested `Wit
7878

7979
[!code-vb[VbVbalrWithStatement#1](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb#1)]
8080

81+
## Example 3
82+
83+
The following example demonstrates how `With...End With` statements work with structures. You can assign to members of referenceable structures (like array elements), but not to structures returned by value or when parentheses are used.
84+
85+
[!code-vb[VbVbalrWithStatement#3](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb#3)]
86+
8187
## See also
8288

8389
- <xref:System.Collections.Generic.List%601>

samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,46 @@
5151
End Class
5252
'</snippet2>
5353

54+
'<snippet3>
55+
Private Sub DemonstrateStructureWithStatement()
56+
' Create an array of structures - this is referenceable
57+
Dim points(2) As Point
58+
59+
' Valid: Array elements are referenceable, so assignments work
60+
With points(0)
61+
.X = 10
62+
.Y = 20
63+
End With
64+
65+
' Create a single structure variable - this is also referenceable
66+
Dim singlePoint As Point
67+
With singlePoint
68+
.X = 30
69+
.Y = 40
70+
End With
71+
72+
' Invalid: Using parentheses cuts reference ties
73+
' With (points(0))
74+
' .X = 50 ' This would cause BC30068 error
75+
' .Y = 60
76+
' End With
77+
78+
' Invalid: Function returns by value, not referenceable
79+
' With GetPoint()
80+
' .X = 70 ' This would cause BC30068 error
81+
' .Y = 80
82+
' End With
83+
End Sub
84+
85+
Private Function GetPoint() As Point
86+
Return New Point With {.X = 1, .Y = 2}
87+
End Function
88+
89+
Private Structure Point
90+
Public X As Integer
91+
Public Y As Integer
92+
End Structure
93+
'</snippet3>
94+
5495

5596
End Class
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"host": "visualstudio"
3+
}

samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/vbwpfapp.vbproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
<OutputType>WinExe</OutputType>
99
<RootNamespace>VBWPFApp</RootNamespace>
1010
<AssemblyName>VBWPFApp</AssemblyName>
11-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
12-
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1312
<MyType>Custom</MyType>
1413
</PropertyGroup>
1514
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">

0 commit comments

Comments
 (0)