Declaração With...End With (Visual Basic)
Executes a series of statements making repeated reference to a single object or structure.
With object
[ statements ]
End With
Parts
Term |
Definition |
object |
Required. Variable or expression. Can evaluate to any data type, including elementary types. |
statements |
Optional. One or more statements between With and End With that run on object. |
End With |
Required. Terminates the definition of the With block. |
Comentários
With...End With allows you to perform a series of statements on a specified object without requalifying the name of the object. If the qualification path to the object is long, using With...End With can improve your performance. A With block also reduces repetitive typing of the qualification path and the risk of mistyping one of its elements.
For example, to change a number of different properties on a single object, place the property assignment statements inside the With...End With, referring to the object only once instead of for each property assignment.
Rules
Tipos de dados. O tipo de dados de object pode ser qualquer classe ou estrutura tipo ou até mesmo um tipo elementar do Visual Basic como Integer. The .NET Framework supports all elementary types with classes or structures, which have members you can access inside a With block.
Declaração. Você deve declarar object antes de inserir o With bloco. You cannot declare it in the With statement.
Number of Iterations. A With bloco é não uma iterativa construção. Unless there is a loop inside the block, the statements run only once.
Nesting Structures. Você pode aninhar With...End With estruturas, colocando uma estrutura dentro de outro. For an example, see Como: Executar várias ações em um objeto (Visual Basic).
However, because members of outer statements are masked inside the inner statements, you must provide a fully qualified object reference in an inner With block to any member of an object in an outer With block.
You can also nest different kinds of control structures within one another. For more information, see Estruturas de controle aninhado (Visual Basic).
Transferring Out of the Structure. Visual Basic não oferece suporte a Declaração Saída (Visual Basic) para transferir controle fora de um With bloco. If you need to exit before all the statements have been executed, put a label on the End With statement and use the Instrução GoTo to branch to it. For more information, see Como: Rotular instruções (Visual Basic).
You cannot transfer control either from outside a With block to inside it, or from inside it to the outside. You can call a procedure from inside the block, but control returns to the following statement.
Accessing Other Objects. Depois que você inseriu um With bloco, você não pode reatribuir object até que você tiver passado a End With demonstrativo. Therefore, 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.
Exemplo
The following example uses a With block to execute a series of statements on a single object. The example assumes that object testObject has already been created and exposes the referenced properties.
With testObject
.Height = 100
.Text = "Hello, World"
.ForeColor = System.Drawing.Color.Green
.Font = New System.Drawing.Font(.Font,
System.Drawing.FontStyle.Bold)
End With
With Keyword in Object Initializers
Visual Basic 2008apresenta um novo uso do With em inicializadores de objeto . For more information and examples, see Inicializadores de objeto: Tipos nomeados e anônimos (Visual Basic) and Tipos anônimos (Visual Basic).
Consulte também
Tarefas
Como: Executar várias ações em um objeto (Visual Basic)
Como: Rotular instruções (Visual Basic)
Referência
Declaração Saída (Visual Basic)
Conceitos
Estruturas de controle aninhado (Visual Basic)
Inicializadores de objeto: Tipos nomeados e anônimos (Visual Basic)