Solved

Setting an order of items in a Collection?

Posted on 2013-11-15
7
272 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 62

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 62

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now