VB.Net Array Swap elements

In this article we will learn how to swap element in an Array.
  • 7221

In this we swap the different element in array with different element. In this Example We have a string with element A,B,C,D,E and now we going to swap B to D.

1.Open visual Studio

2.create Project with console application using language with VB.Net

3.Write the following code on the page

Public Class Tester
    Public Shared Sub Main()
        Dim result As New System.Text.StringBuilder
        Dim arraySwap() As String = {"A", "B", "C", "D", "E")
        Swap(arraySwap, 1, 3)
        For Each fruit As String In arraySwap
            Console.WriteLine(fruit)
        Next fruit
        Console.Read()
    End Sub
 
    Public Shared Sub Swap(ByRef swapArray() As Object, _
            ByVal first As Integer, ByVal second As Integer)
        Dim tempObject As Object
 
        tempObject = swapArray(first)
        swapArray(first) = swapArray(second)
        swapArray(second) = tempObject
    End Sub

End Class

Output of the Application

sortarray.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.