Udostępnij za pośrednictwem


With...End With Statement (Visual Basic)

Executes a series of statements that repeatedly refers to a single object or structure.

With object
    [ statements ]
End With

Parts

Term

Definition

expression

Required. An expression that evaluates to an object. The expression may be arbitrarily complex and is evaluated only once. The expression can evaluate to any data type, including elementary types.

statements

Optional. One or more statements between With and End With that may refer to members of an object that's produced by the evaluation of expression.

End With

Required. Terminates the definition of the With block.

Remarks

By using With...End With, you can perform a series of statements on a specified object without specifying the name of the object multiple times. Within a With statement block, you can specify a member of the object starting with a period, as if the With statement object preceded it.

For example, to change multiple properties on a single object, place the property assignment statements inside the With...End With block, referring to the object only once instead of once for each property assignment.

If your code accesses the same object in multiple statements, you gain the following benefits by using the With statement:

  • You don't need to evaluate a complex expression multiple times or assign the result to a temporary variable to refer to its members multiple times.

  • You make your code more readable by eliminating repetitive qualifying expressions.

The data type of expression can be any class or structure type or even a Visual Basic elementary type such as Integer.

The expression is evaluated once, upon entry into the block. You can't reassign the expression from within the With block.

Within a With block, you can access the methods and properties of only the specified object without qualifying them. You can use methods and properties of other objects, but you must qualify them with their object names.

You can place one With...End With statement within another. Nested With...End With statements may be confusing if the objects that are being referred to aren't clear from context. You must provide a fully qualified reference to an object that's in an outer With block when the object is referenced from within an inner With block.

You can't branch into a With statement block from outside the block.

Unless the block contains a loop, the statements run only once. You can nest different kinds of control structures. For more information, see Nested Control Structures (Visual Basic).

Note

You can use the With keyword in object initializers also. For more information and examples, see Object Initializers: Named and Anonymous Types (Visual Basic) and Anonymous Types (Visual Basic).

If you're using a With block only to initialize the properties or fields of an object that you've just instantiated, consider using an object initializer instead.

Example

In the following example, each With block executes a series of statements on a single object.

Private Sub AddCustomer()
    Dim theCustomer As New Customer

    With theCustomer
        .Name = "Coho Vineyard"
        .URL = "http://www.cohovineyard.com/"
        .City = "Redmond"
    End With

    With theCustomer.Comments
        .Add("First comment.")
        .Add("Second comment.")
    End With
End Sub

Public Class Customer
    Public Property Name As String
    Public Property City As String
    Public Property URL As String

    Public Property Comments As New List(Of String)
End Class

The following example nests With…End With statements. Within the nested With statement, the syntax refers to the inner object.

Dim theWindow As New EntryWindow

With theWindow
    With .InfoLabel
        .Content = "This is a message."
        .Foreground = Brushes.DarkSeaGreen
        .Background = Brushes.LightYellow
    End With

    .Title = "The Form Title"
    .Show()
End With

See Also

Reference

List<T>

Concepts

Nested Control Structures (Visual Basic)

Object Initializers: Named and Anonymous Types (Visual Basic)

Anonymous Types (Visual Basic)

Change History

Date

History

Reason

Revised and reorganized the remarks, and changed the examples.

Customer feedback.