×

VB.Net Programs

VB.Net program to demonstrate the Copy() method of Array class

By Nidhi Last Updated : November 11, 2024

Array.Copy() Method in VB.Net

The Copy() method is used to copy the elements of one array to another array from the starting index, here we use length to specify, how many elements get copied.

Syntax

Sub Copy (ByVal source_arr() as object, 
    ByVal dest_arr() as object, 
    Byval length as integer) 

Parameter(s)

  • Source_arr: It is the specified source array.
  • Dest_arr: It is the specified destination array.
  • Length: It specifies how many elements to be copied.

Return Value

It does not return any value.

VB.Net code to demonstrate the example of Array.Copy() method

The source code to demonstrate the Copy() method of the Array class is given below. The given program is compiled and executed successfully.

'VB.NET program to demonstrate the Copy() 
'method of Array class.

Imports System

Module Module1
    Sub Main()
        Dim arr1() As Integer = {10, 20, 30, 40, 50}
        Dim arr2() As Integer = {60, 70, 80, 90, 100}

        'Copy elements of arr1 element to arr2 from 
        'starting to with specified length.
        Array.Copy(arr1, arr2, 4)

        Console.WriteLine("Arr2 Array elements:")
        For i = 0 To arr2.Length - 1 Step 1
            Console.Write(arr2(i) & " ")
        Next
        Console.WriteLine()
    End Sub
End Module

Output

Arr2 Array elements:
10 20 30 40 100
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 two arrays arr1 and arr2. Then we copy the elements of one integer array to another integer array using the Copy() method of Array class, here we used the length to specify how many elements will be copied to the destination array.

VB.Net Array Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.