Solved

How can I filter out duplicates from an array collection, but ensure I get the output I need from the collection.

Posted on 2004-09-07
5
268 Views
Last Modified: 2012-05-05
The subject doesnt really give you much insight so let me go into as much detail as possible.

Basically I am trying to take the values out of 2 collections and make sure that the duplicates are omitted, but I need to capture the current value if certain conditions are met.....hmm that may not explain it any clearer.

I am trying to do the following:

   * Check the values in the VP_Positive_Policy_Array against the currently checked values in the Listview, and if any of the values match (as in an INSTR) then flag that current value (or item in the Positive Policy Array) as I will want it 'excluded' from the final output...
   * Check the values in the VP_Negative Policy_Array against the currently checked values in the Listview, and if any of the values match (as in an INSTR) then flag that current value (or item in the Negative Policy Array) as I will want it 'excluded' from the final output...

Now here is where I am stuck.....

   *  If a match is found in one, but not the other (or vice versa) (found in the Positive Array, but not the Negative Array), i need to flag the current value (which will be the current checkitem in the listview), as if either one of these functions find a match, it means it should be flaged as ignored and go onto the next value in the checkitem list (not in the current collection of the array).
   *  Also, If there are NO matches found (meaning the checkeditem value is NOT in the positive OR the negative array), then it needs to display the current results in the listbox.


Here are some example values I am working with and you can assume are your inputs:

ListView1.CheckedItems Values:
  3_PALMTE_412_W2X
  C_ACG_22_W2X
  C_ACG_21_W2K
  3_SMIME_15_W2X

VP_Positive_Policy_Array Values:
  C_ACG_22_W2X
  C_WINZIP_81A_W2K
  C_OFFICE_2KSP3_W2K
    Note:  There are TONS more values but these are the only applicable ones...

VP_Negative_Policy_Array Values:
  C_ACG_21_W2K
  C_REFLECTIONSNAP_805_W2K
    Note:  There are TONS more values but these are the only applicable ones...



With the current code, here is what my tool OUTPUTS in the listbox:

  3_PALMTE_412_W2X  ::   No AD/Policy Found or Application Restricted"
  C_ACG_22_W2X ::   No AD/Policy Found or Application Restricted"
  C_ACG_21_W2K ::   No AD/Policy Found or Application Restricted"
  3_SMIME_15_W2X ::   No AD/Policy Found or Application Restricted"

Note:  Problem is, it shows these values for EVERY item in the array collection (both positive and negative).... I need it so if it finds it ONCE, it doesnt output it again.....

Hope someone can help!!!


*******************START CODE***********************

Private Sub VP_Finalize_Policy_Comparison()
        ''''''''''''''''''Compare the final values with whats selected in the Radia Instance List'''''''''''''''
        ResultListBox.Items.Clear()

        Dim VP_LVItem As ListViewItem
        Dim VP_Current_AppName
        Dim VP_Results_Collection As New ArrayList()

        '''''''''''''''''''''''''''Compare If Positive Values exist, or Negative Values Exist, and determine Results (omit duplicates but continue process)''''''''''''''''''''''''''''''''

        Dim Pos_Flag As Boolean
        Dim Neg_Flag As Boolean
        Dim Neg_Pol_Collection As New ArrayList()

        For Each VP_LVItem In ListView1.CheckedItems
            VP_Current_AppName = VP_LVItem.Text

            Dim Pos_Temp
            For Each Pos_Temp In VP_Positive_Policy_Array
                If Not InStr(Pos_Temp, VP_Current_AppName) > 0 Then

                    Dim Neg_Temp
                    For Each Neg_Temp In VP_Negative_Policy_Array
                        If Not InStr(Neg_Temp, VP_Current_AppName) > 0 Then
                            Dim Test
                            For Each Test In VP_Results_Collection
                                If Not InStr(Test, VP_Current_AppName) > 0 Then
                                    ResultListBox.Items.Add("" & VP_Current_AppName & "   ::   No AD/Policy Found or Application Restricted")
                                End If
                            Next
                        End If
                    Next


                End If
            Next
        Next

*****************END CODE**************************
0
Comment
Question by:phesser
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
Comment Utility
Hi there... try this code...

    Private Sub VP_Finalize_Policy_Comparison()
        Dim SelectedItm As ListViewItem
        For Each SelectedItm In ListView1.SelectedItems
            If Not CheckItm(SelectedItm.Text, VP_Positive_Policy_Array) Then
                If Not CheckItm(SelectedItm.Text, VP_Negative_Policy_Array) Then
                    Dim x As Int16 = -1
                    x = ResultListBox.FindString(SelectedItm.Text)
                    If x = -1 Then
                        ResultListBox.Items.Add(SelectedItm)
                    End If
                End If
            End If
        Next
    End Sub

    Function CheckItm(ByVal Itm As String, ByVal ArrayToSearch As ArrayList) As Boolean
        Dim ArrItm As Object
        For Each ArrItm In ArrayToSearch
            If ArrItm.ToString() = Itm Then
                Return True
                Exit Function
            End If
        Next
        Return False
    End Function

-Baan
0
 

Author Comment

by:phesser
Comment Utility
Thanks DotNetLover_Baan, I will try this first thing tommorow and let you know...hopefully it will work flawlessly :)
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
Take a look at this:

   Private Sub VP_Finalize_Policy_Comparison()
        Dim VP_LVItem As ListViewItem

        ResultListBox.Items.Clear()

        'Iterate through all items in the ListView
        For Each VP_LVItem In ListView1.CheckedItems

            'If the checked item is not found in either positive or negative list
            'then added to the output listbox
            If Not VP_Negative_Policy_Array.Contains(VP_LVItem.Text) AndAlso _
                Not VP_Positive_Policy_Array.Contains(VP_LVItem.Text) Then

                'If the checked item was already added to the output listbox
                'don't add it again in order to avoid duplicates in the output
                If Not ResultListBox.Items.Contains(VP_LVItem.Text) Then
                    'the was not found, so we can add it to the output list box

                    ResultListBox.Items.Add(VP_LVItem.Text)
                End If
            End If
        Next
    End Sub
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
I can see DotNetLover_Baan and my code do the same thing.

I posted my code since i did last night and don't have access to internet in my house.

You don't need to iterate an arraylist to find an item, you can use

 VP_Negative_Policy_Array.Contains

 If Not VP_Negative_Policy_Array.Contains(VP_LVItem.Text) AndAlso _
                Not VP_Positive_Policy_Array.Contains(VP_LVItem.Text) Then

AndAlso means that if the first condition becomes false do not evaluate the second.

The same contains in the listbox.
 ResultListBox.Items.Contains
0
 

Author Comment

by:phesser
Comment Utility
That looks to have worked DotNetLover_Baan...really appreciate the quick and accurate turnaround!

Also wguerram, thanks for your input, sorry about accepting before I noticed your solution as well...gives me some great insight into the process and works as well...!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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.

771 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