Home »
        VB.Net »
        VB.Net Programs
    
    VB.Net program to demonstrate the Clear() method of Array class
    
    
    
    
	    
		    By Nidhi Last Updated : November 11, 2024
	    
    
    
    Array.Clear() Method in VB.Net
    The Clear() method is used to clear the value of elements in the specified array from the specified index.
    Syntax
Sub Clear(ByVal arr() as object, ByVal index as integer, Byval length as integer) 
    
    Parameter(s)
    
        - Arr: It is the specified array.
- Index: Specified index to clear the value of items.
- Length: It specifies how many elements must be clear.
Return Value
    It does not return any value.
    VB.Net code to demonstrate the example of Array.Clear() method
    The source code to demonstrate the Clear() method of the Array class is given below. The given program is compiled and executed successfully.
    
'VB.NET program to demonstrate the Clear() 
'method of Array class.
Imports System
Module Module1
    Sub Main()
        Dim arr() As Integer = {10, 20, 30, 40, 50}
        'Reset item in the array from index 2 to 3 by 0.
        Array.Clear(arr, 2, 2)
        Console.WriteLine("Array elements:")
        For i = 0 To arr.Length - 1 Step 1
            Console.Write(arr(i) & " ")
        Next
        Console.WriteLine()
    End Sub
End Module
Output
Array elements:
10 20 0 0 50
Press any key to continue . . .
    Explanation
    In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program.
    In the Main() function, we created an array arr with 5 elements. Then we clear the value of elements from index 2 to 3 using the Clear() method of Array class and then print the updated array on the console screen.
    
    VB.Net Array Programs »
    
    
    
    
   
    
  
    Advertisement
    
    
    
  
  
    Advertisement