Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 (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.

Expand Down Expand Up @@ -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

- <xref:System.Collections.Generic.List%601>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,46 @@
End Class
'</snippet2>

'<snippet3>
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
'</snippet3>


End Class
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"host": "visualstudio"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
<OutputType>WinExe</OutputType>
<RootNamespace>VBWPFApp</RootNamespace>
<AssemblyName>VBWPFApp</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<MyType>Custom</MyType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
Expand Down