Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

Setting an order of items in a Collection?

I was wondering if it's possible to order a Collection of Items. The values that I am storing in this collection are numeric. Is there anyway to put them in a sequential order and keep them in that order whenever a new item is added?

Thanks!
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

You can use the SortedList collection. It is a key/Value collection so make the key and value of the same type and value.
Avatar of BlakeMcKenna

ASKER

Okay, here is what I'm trying to do but I'm getting an error. The first part of the code adds an entry to the SortedList (tabLst). There are 7 "Add" Snippets just with different values.

THIS CODE EXECUTED FIRST:
       Dim t1 As Integer = 1
       Dim t2 As Integer = 2
       Dim t3 As Integer = 3
       Dim t4 As Integer = 4
       Dim t5 As Integer = 5
       Dim t6 As Integer = 6

       'Add Snippet
        If cboElectricalTest.Checked Then
              If Not tabTests.TabPages.Contains(tabElectricalTest) Then
                   tabLst.Add(t1, t1)
                   AddTabPage(t1, tabElectricalTest)
              End If
              lblTestCategory.Text = "ELECTRICAL TEST"
        Else
              If tabTests.TabPages.Contains(tabElectricalTest) Then
                   tabTests.TabPages.Remove(tabElectricalTest)
                   tabLst.Remove(t1)
              End If
        End If

THIS CODE EXECUTED SECOND:
        Private Sub AddTabPage(ByVal idx As Integer, ByVal tabPg As TabPage)
        Try
            If Not tabTests.Contains(tabPg) Then
                For Each itm In tabLst
                    If idx < itm Then   ----> Error Message occurs, see Screenshot
                        tabTests.TabPages.Insert(idx, tabPg)
                        Exit For
                    Else
                        tabTests.TabPages.Add(tabPg)
                        Exit For
                    End If
                Next
            End If

            EH.strRetVal = ""

        Catch ex As Exception
            EH.strRetVal = gfrmID & "/EnableZeroOffsetAndMiscellaneous() - " & ex.Message & "~E"
        End Try
    End Sub
Screenshot.jpg
Can someone please show me an example of using the IComparer in conjunction with the SortedList()? I have NO idea how to use this functionality...

Thanks!
Hi BlakeMcKenna;

' I am initializing the SortedList with values. You could also use the Add method as you did in your code snippet
Dim tabLst As New SortedList(Of Integer, Integer)() From {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}}

' Doing a For Each loop as you did in your code and shown in the code below the variable itm is an object
' of a Key/Value pair and NOT a single integer value. So for example the Console.WriteLine in the below code it will
' display the Key/Value pair for the first value as {1, 1} and NOT as 1. 
For Each itm In tabLst
    Console.WriteLine(itm)
Next

' So returning the value of the key in idx you should do it as follows.
Dim idx As Integer = 5
For Each itm In tabLst.Keys
    Dim val As Integer
    tabLst.TryGetValue(idx, val)
    If idx < val Then
        tabTests.TabPages.Insert(idx, tabPg)
        Exit For
    Else
        tabTests.TabPages.Add(tabPg)
        Exit For
    End If
Next

' Because you do not iterate over the collect in the above For Each loop cause each branch of the if
' statement exits the For Each this code like this would be more efficient.
Dim idx As Integer = 5
Dim val As Integer

If tabLst.TryGetValue(idx, val) Then
   tabTests.TabPages.Insert(idx, tabPg) 
Else
   tabTests.TabPages.Add(tabPg)
End If

Open in new window

The requirement for this piece of code is that a Tab Control has 7 tabPages. TabPage1 will always be visible. TabPages 2-6 will be invisible when the page initially loads.

In design mode, I have added all 7 tabPages. In the Form Load Event, I remove Tabs 2-6 (this functionality is in a called subprocedure).

                If blnInitialRun Then
                    tabTests.TabPages.Remove(tabElectricalTest)
                    tabTests.TabPages.Remove(tabShuntTest)
                    tabTests.TabPages.Remove(tabCreepTest)
                    tabTests.TabPages.Remove(tabRecoveryTest)
                    tabTests.TabPages.Remove(tabRepeatabilityTest)
                    tabTests.TabPages.Remove(tabReproducabilityTest)
                End If

When a CheckBox is checked, it executes the Procedure that either Inserts or Adds the specified tabPage. The key here is that the tabPages are in a specific order (setup at design time) and need to stay in that order. For example. If tabPages 1, 2 & 3 are already showing and a User checks the checkbox for tabPage 6, then tabPage6, which is the last tabPage in the collection, is added to the Tab rather than inserted because there is no other tabPage after 6.

Now that tabPage6 has been added back to the collection, the User clicks the checkbox for tabPage4. TabPage4 now has to be INSERTED rather than added because TabPage6 is now active.

Obviously you have tested your code and it works under your conditions. However, I can not get it to work with my scenario. Hopefully my explanation of the functionality will help. Sorry for any lack of clarity.
ASKER CERTIFIED SOLUTION
Avatar of BlakeMcKenna
BlakeMcKenna
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I finally figured this out...