TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Array.Copy Example

Use the Array.Copy subroutine to copy a range of elements from one array to another.
Array.Copy. Arrays are initialized to empty memory. But we can copy one array to another with the Array.Copy subroutine. This assigns each element in one to each element in another. In this way we effectively initialize arrays.Array
Example. This program creates two five-element arrays. In VB.NET, when we create a five-element array we specify the maximum index of 4. In this program, we assign all five elements. Then we create another, separate array.

Array.Copy: This populates the elements of the target array with the elements of the source array. The two arrays are separate in memory.

Sub

So: When we change the values in the source array at this point, the target array is not affected by that change.

Finally: We use a For-Each loop to display each Integer element in the target array, revealing its contents.

For Each, For
VB.NET program that uses Array.Copy Module Module1 Sub Main() ' Source array of 5 elements. Dim source(4) As Integer source(0) = 1 source(1) = 10 source(2) = 100 source(3) = 1000 source(4) = 10000 ' Create target array and use Array.Copy. Dim target(4) As Integer Array.Copy(source, target, target.Length) ' Change source array after copy was made. source(0) = 300 ' Display target array. For Each element As Integer In target Console.WriteLine(element) Next End Sub End Module Output 1 10 100 1000 10000
Discussion. Several overloads of Array.Copy are available in the .NET Framework. The simplest overload is shown above—it copies a specified number of elements starting at the first element from one array to another.
With other overloads, we can specify a sourceIndex and a destinationIndex. This makes it possible to copy a range of elements at any location in one array to any location in another. We use offsets.

Tip: Using the simplest overload for the required task is ideal. But some programs are simpler overall with the complex overloads.

Summary. Arrays are powerful but are harder to initialize and copy in programs. With Array.Copy, and related subroutines such as Array.Resize, we manually manipulate arrays—their lengths and their elements.Array.Resize
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf