From 83b2c5f6eac07ea63aab2b46755676f2cb9706fc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 2 Sep 2025 21:33:32 +0000 Subject: [PATCH 1/5] Initial plan From 991213a8180df80acba2a4e219252f0980e38412 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 2 Sep 2025 21:40:29 +0000 Subject: [PATCH 2/5] Fix misleading documentation about structure assignments in With...End With statements Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../statements/with-end-with-statement.md | 10 ++++- .../vb/mainwindow.xaml.vb | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/docs/visual-basic/language-reference/statements/with-end-with-statement.md b/docs/visual-basic/language-reference/statements/with-end-with-statement.md index 2e1defd199c21..f35409ea2a16f 100644 --- a/docs/visual-basic/language-reference/statements/with-end-with-statement.md +++ b/docs/visual-basic/language-reference/statements/with-end-with-statement.md @@ -19,7 +19,7 @@ ms.assetid: 340d5fbb-4f43-48ec-a024-80843c137817 --- # With...End With Statement (Visual Basic) -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. +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. ## Syntax @@ -49,7 +49,7 @@ If your code accesses the same object in multiple statements, you gain the follo - You make your code more readable by eliminating repetitive qualifying expressions. -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. +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 (e.g., `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. The `objectExpression` is evaluated once, upon entry into the block. You can't reassign the `objectExpression` from within the `With` block. @@ -78,6 +78,12 @@ The following example nests `With…End With` statements. Within the nested `Wit [!code-vb[VbVbalrWithStatement#1](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb#1)] +## Example 3 + +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. + +[!code-vb[VbVbalrWithStatement#3](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb#3)] + ## See also - diff --git a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb index 13409d329f7c0..ef85a5dc5c7b5 100644 --- a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb +++ b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/mainwindow.xaml.vb @@ -51,5 +51,46 @@ End Class ' + ' + Private Sub DemonstrateStructureWithStatement() + ' Create an array of structures - this is referenceable + Dim points(2) As Point + + ' Valid: Array elements are referenceable, so assignments work + With points(0) + .X = 10 + .Y = 20 + End With + + ' Create a single structure variable - this is also referenceable + Dim singlePoint As Point + With singlePoint + .X = 30 + .Y = 40 + End With + + ' Invalid: Using parentheses cuts reference ties + ' With (points(0)) + ' .X = 50 ' This would cause BC30068 error + ' .Y = 60 + ' End With + + ' Invalid: Function returns by value, not referenceable + ' With GetPoint() + ' .X = 70 ' This would cause BC30068 error + ' .Y = 80 + ' End With + End Sub + + Private Function GetPoint() As Point + Return New Point With {.X = 1, .Y = 2} + End Function + + Private Structure Point + Public X As Integer + Public Y As Integer + End Structure + ' + End Class From 2f7db4bca4f2abee227289e56bf7d79be62b08f1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:08:10 +0000 Subject: [PATCH 3/5] Add snippets.5000.json file to fix sample build configuration Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../vbvbalrwithstatement/vb/snippets.5000.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/snippets.5000.json diff --git a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/snippets.5000.json b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/snippets.5000.json new file mode 100644 index 0000000000000..9493e733615c7 --- /dev/null +++ b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/snippets.5000.json @@ -0,0 +1,3 @@ +{ + "host": "visualstudio" +} \ No newline at end of file From 69af60f4ffbb27c3f7a890b44af810c6ba6255cb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:18:41 +0000 Subject: [PATCH 4/5] Update vbproj to target .NET Framework 4.8 Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com> --- .../vbvbalrwithstatement/vb/vbwpfapp.vbproj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/vbwpfapp.vbproj b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/vbwpfapp.vbproj index f2a83367e66fa..5ea423e4e12de 100644 --- a/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/vbwpfapp.vbproj +++ b/samples/snippets/visualbasic/VS_Snippets_VBCSharp/vbvbalrwithstatement/vb/vbwpfapp.vbproj @@ -8,8 +8,7 @@ WinExe VBWPFApp VBWPFApp - v4.0 - Client + v4.8 Custom From 9a5c00f143074a9cda03eb96f5702dfd7dd293f1 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 8 Sep 2025 13:45:12 -0400 Subject: [PATCH 5/5] Update docs/visual-basic/language-reference/statements/with-end-with-statement.md Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- .../language-reference/statements/with-end-with-statement.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/visual-basic/language-reference/statements/with-end-with-statement.md b/docs/visual-basic/language-reference/statements/with-end-with-statement.md index f35409ea2a16f..3e9f3a0908103 100644 --- a/docs/visual-basic/language-reference/statements/with-end-with-statement.md +++ b/docs/visual-basic/language-reference/statements/with-end-with-statement.md @@ -49,7 +49,7 @@ If your code accesses the same object in multiple statements, you gain the follo - You make your code more readable by eliminating repetitive qualifying expressions. -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 (e.g., `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. +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. The `objectExpression` is evaluated once, upon entry into the block. You can't reassign the `objectExpression` from within the `With` block.