Click here to Skip to main content
12,697,076 members (29,809 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
Hi,

I need to change the array item position in vb.net

{1,2,3,4} to {3,2,4,1}

Thanks,

Sivasankaran G
Posted 10-Jan-13 1:55am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Public Shared Sub ChangeItemPos(ByVal a As Array)
    If (a IsNot Nothing) AndAlso (a.Length > 1) AndAlso (a.Rank = 1) Then
        Dim list As New ArrayList(a)
        Dim firstItem As Object = list.Item(0)
        list.RemoveAt(0)
        For index As Integer = 1 To list.Count - 1 Step 2
            ' Swap the items
            Dim temp As Object = list.Item(index)
            list.Item(index) = list.Item(index - 1)
            list.Item(index - 1) = temp
        Next
        list.Add(firstItem) ' Put firstItem into last
        list.CopyTo(a)
    End If
End Sub


Example:

Dim a As Integer() = {1, 2, 3, 4}
ChangeItemPos(a)
  Permalink  
Comments
Sivaguru88 17-Jan-13 6:08am
   
Thanks for your response guna

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100