Solved

Setting an order of items in a Collection?

Posted on 2013-11-15
7
275 Views
Last Modified: 2013-11-23
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!
0
Comment
Question by:BlakeMcKenna
  • 5
  • 2
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39652451
You can use the SortedList collection. It is a key/Value collection so make the key and value of the same type and value.
0
 

Author Comment

by:BlakeMcKenna
ID: 39652528
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
0
 

Author Comment

by:BlakeMcKenna
ID: 39652539
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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39652851
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

0
 

Author Comment

by:BlakeMcKenna
ID: 39656654
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.
0
 

Accepted Solution

by:
BlakeMcKenna earned 0 total points
ID: 39657809
I actually figured this out.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 39671122
I finally figured this out...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question