VB.NET With End With Statement

VB.NET With…End With Statement: The With…End Statement and the structure of loop are different to each other. This statement helps in accessing and executing statements in particular objects without mentioning the name of objects together with the each statement. In a With Statement block, one can mention the object member beginning with period(.) for defining multiple statements.

The VB.NET With Statement do not have any new feature or function with respect to the other statements and keywords. It helps in improving the readability of code and also helps in the saving some precious time.

This statement is not completely like looping statements. In this, a complete series of statements referring an object or structure is executed at a single time.

  • The object that is referred in With statement is similar through the whole code in the With…End With block.
  • Thus, the properties and methods of the object is easily used in the code block without referencing the object.
  • If the inner With Statement directs to a sub object or dependent object of the outer With Statement, the With Statement becomes Nested.
  • An object member referred in a With Block omits object name and includes a period and a member name. 

Syntax:

With objExpression  
    [ Statements to be Executed]  
End With  

objExpression: This helps in defining the DataType which could be a class or structure type or a basic data type like Integer. A single execution is allowed in With End With Statement.

Statement: Multiple statements can be defined in the With Block.Statement directs to the object member linked with objectExpression for the execution of With Statement Block.

End With: This is required in ending the With block.

Example: Write a program to understand the uses of With End With statement in VB.NET.

Staff.vb

Public Class Staff  
' definition of global variables  
Public Property name As String  
Public Property age As Integer  
Public Property Occupation As String  
Public Property email As String 
Shared Sub Main()  
' Create an emp object   
Dim emp As New Employee  
' To define the member of an object  
With emp  
.name = " Mr. Rishi"  
.age = 22
.Occupation = "Technical Content Developer"  
.email = "[email protected]"  
End With  
With stf  
' use stf as a reference  
Console.WriteLine(" Name is : {0}", .name)  
Console.WriteLine(" Age is : {0}", .age)  
Console.WriteLine(" Occupation is : {0}", .Occupation)  
Console.WriteLine(" Staff Email is : {0}", .email)  
End With  
Console.WriteLine(" Press any key to exit...")  
Console.ReadKey()  
End Sub  
End Class  

Output:

VB.NET With End With Statement

Example: Write a program to display the worker details using the With End statement in VB.NET.

With_Statement.vb

Imports System  
Module With_statement  
Sub Main()  
' Create a stud object   
Dim wkr As Worker = New Worker()  
' To define the member of an object using With Statement  
With wkr  
.wkr_name = " Mr. Raghav"  
.wkr_role = "Sanitization"  
.wkr_badgeno = "01"  
End With  
With wkr  
' use wkr as a reference  
Console.WriteLine(" Worker Name is : {0}", .wkr_name)  
Console.WriteLine(" Worker Role is : {0}", .wkr_role)  
Console.WriteLine(" Worker Badge No. is : {0}", .wkr_badgeno)  
End With  
Console.WriteLine(" Press any key to exit...")  
Console.ReadKey()  
End Sub  
' Create a Class Worker  
Public Class Worker  
Public wkr_name As String 'Define the variable of a class  
Public wkr_role As String  
Public wkr_badgeno As Integer  
End Class  
End Module  

Output:

VB.NET With End With Statement