Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. comparing values on fields in arraylists

comparing values on fields in arraylists

Scheduled Pinned Locked Moved Visual Basic
question
21 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ssbelfast
    wrote on last edited by
    #1

    I need to compare the value of a field in a row on an arraylist with the value of a field on a second arraylist I have this bit of code working for arrays but cant get it working for arraylists The second argument here (1) represents the second field in the row, with arraylists I get a message saying to many arguments. Can I do this with arraylists or do I need to copy the arraylists to arrays? Thanks For q As Integer = 0 To 9 For j As Integer = 0 To 9 If StrArray1(q, 1) = StrArray2(j, 1) Then count = count + 1 End If Next Next Thanks

    S 1 Reply Last reply
    0
    • S ssbelfast

      I need to compare the value of a field in a row on an arraylist with the value of a field on a second arraylist I have this bit of code working for arrays but cant get it working for arraylists The second argument here (1) represents the second field in the row, with arraylists I get a message saying to many arguments. Can I do this with arraylists or do I need to copy the arraylists to arrays? Thanks For q As Integer = 0 To 9 For j As Integer = 0 To 9 If StrArray1(q, 1) = StrArray2(j, 1) Then count = count + 1 End If Next Next Thanks

      S Offline
      S Offline
      Stephen McGuire
      wrote on last edited by
      #2

      Arrays can have multiple dimensions. ArrayList's always have exactly one dimension. Steve

      S 1 Reply Last reply
      0
      • S Stephen McGuire

        Arrays can have multiple dimensions. ArrayList's always have exactly one dimension. Steve

        S Offline
        S Offline
        ssbelfast
        wrote on last edited by
        #3

        so how can I do something like this with an arraylist there are actually two fields on each row, I understand what you said about only one dimension, in this case if the first field is the one each array, the 2nd field will be the say, so I can just compare the entire row but when I try something like If StrArraylist(q) = StrArraylist(j) Then do something I get a message 'Operator is not valid for type 'field name' I thought this meant to try == instead of = , but this wouldn't compile thanks

        S S N 3 Replies Last reply
        0
        • S ssbelfast

          so how can I do something like this with an arraylist there are actually two fields on each row, I understand what you said about only one dimension, in this case if the first field is the one each array, the 2nd field will be the say, so I can just compare the entire row but when I try something like If StrArraylist(q) = StrArraylist(j) Then do something I get a message 'Operator is not valid for type 'field name' I thought this meant to try == instead of = , but this wouldn't compile thanks

          S Offline
          S Offline
          ssbelfast
          wrote on last edited by
          #4

          I tried to create one dimensional arrays and copy me arraylists to arrays so that I could do the compare in the arrays, but can't get that to work either, maybe its because my one dimensional arrays have two fields Dim tempArray1 As Array = Array.CreateInstance(GetType(String), 10) Dim tempArray2 As Array = Array.CreateInstance(GetType(String), 10) Arraylist1.CopyTo(tempArray1) Arraylist2.CopyTo(tempArray2) always get this message At least one element in the source array could not be cast down to the destination array type

          1 Reply Last reply
          0
          • S ssbelfast

            so how can I do something like this with an arraylist there are actually two fields on each row, I understand what you said about only one dimension, in this case if the first field is the one each array, the 2nd field will be the say, so I can just compare the entire row but when I try something like If StrArraylist(q) = StrArraylist(j) Then do something I get a message 'Operator is not valid for type 'field name' I thought this meant to try == instead of = , but this wouldn't compile thanks

            S Offline
            S Offline
            Stephen McGuire
            wrote on last edited by
            #5

            OK. So you know ArrayList's do not have fields - only one dimension One value per row if you like... You could use the ArrayList.Item(index) property passing the index or ArrayList.Contains(Value) passing the value. Do you just want to see if a value exists in each array? Do you want to see if a value exists in each array at the same index? Do your arrays contain duplicate values? Steve

            S 1 Reply Last reply
            0
            • S Stephen McGuire

              OK. So you know ArrayList's do not have fields - only one dimension One value per row if you like... You could use the ArrayList.Item(index) property passing the index or ArrayList.Contains(Value) passing the value. Do you just want to see if a value exists in each array? Do you want to see if a value exists in each array at the same index? Do your arrays contain duplicate values? Steve

              S Offline
              S Offline
              ssbelfast
              wrote on last edited by
              #6

              I want to see if the value at a given index in one array is the same as the value at the same index in a different array, I don't need to know what the value is, just want to see if the values are the same

              S 1 Reply Last reply
              0
              • S ssbelfast

                I want to see if the value at a given index in one array is the same as the value at the same index in a different array, I don't need to know what the value is, just want to see if the values are the same

                S Offline
                S Offline
                ssbelfast
                wrote on last edited by
                #7

                I tried For r As Integer = 0 To 9 If arraylist1.Item(r) = arralist2.Item(r) Then count = count + 1 End If Next but still get this message "Operator is not valid for type "

                S 1 Reply Last reply
                0
                • S ssbelfast

                  I tried For r As Integer = 0 To 9 If arraylist1.Item(r) = arralist2.Item(r) Then count = count + 1 End If Next but still get this message "Operator is not valid for type "

                  S Offline
                  S Offline
                  Stephen McGuire
                  wrote on last edited by
                  #8

                  Should be: For r As Integer = 0 To 9 If arraylist1.Item(r) = arralist2.Item(r) Then r = r + 1 End If Next Where did 'count' come from? You can also increment by using r += 1

                  S S 2 Replies Last reply
                  0
                  • S Stephen McGuire

                    Should be: For r As Integer = 0 To 9 If arraylist1.Item(r) = arralist2.Item(r) Then r = r + 1 End If Next Where did 'count' come from? You can also increment by using r += 1

                    S Offline
                    S Offline
                    Stephen McGuire
                    wrote on last edited by
                    #9

                    You may need to try something like this: Dim str1, str2 As String For r As Integer = 0 To 9 str1 = arraylist1.Item(r) str2 = arralist2.Item(r) If str1 = str2 Then 'Implementation code here - set bool flag, exit sub etc End If r = r + 1 Next

                    1 Reply Last reply
                    0
                    • S Stephen McGuire

                      Should be: For r As Integer = 0 To 9 If arraylist1.Item(r) = arralist2.Item(r) Then r = r + 1 End If Next Where did 'count' come from? You can also increment by using r += 1

                      S Offline
                      S Offline
                      ssbelfast
                      wrote on last edited by
                      #10

                      I should not need to increment 'r' the for - next structure does that the count is a count of the number of times the two rows match, dimmed before the for next statement but when I try to run this I get a message Operator is not valid for type

                      S 1 Reply Last reply
                      0
                      • S ssbelfast

                        so how can I do something like this with an arraylist there are actually two fields on each row, I understand what you said about only one dimension, in this case if the first field is the one each array, the 2nd field will be the say, so I can just compare the entire row but when I try something like If StrArraylist(q) = StrArraylist(j) Then do something I get a message 'Operator is not valid for type 'field name' I thought this meant to try == instead of = , but this wouldn't compile thanks

                        N Offline
                        N Offline
                        Nouvand
                        wrote on last edited by
                        #11

                        I'm still have no idea on what will compare with what. Lets figure these out: say, you have 2 multidimension arrays +-----------+ +-----------+ | StrList1 | | StrList2 | +-----------+ +-----------+ | 0 | Jenny | | 0 | Jenny | | 1 | Fred | | 1 | Fred | | 2 | Sony | | 2 | Rudy | | 3 | Marti | | 3 | Marti | | 4 | Rudy | | 4 | Sony | +-----------+ +-----------+ You want to compare the second coloumn of each array. if the values is match then will return the row index. Make sure, the arrays have a same data type or you can convert it to specific datatype before you campare the values. ------------------------------------------------- Dim RowIdx() as Integer = {} For i as Integer=0 to StrList1.GetUpperBound(0) if StrList1(i, 1) = StrList2(i, 1) then 'you can change with yours Dim x as integer = RowIdx.GetUpperBound(0) Redim RowIdx(x + 1) Dim y as Integer = RowIdx.GetUpperBound(0) RowIdx(y)=i End If Next ---------------------------------------------------- Is it what you mean? -- modified at 7:41 Thursday 3rd August, 2006

                        S 1 Reply Last reply
                        0
                        • N Nouvand

                          I'm still have no idea on what will compare with what. Lets figure these out: say, you have 2 multidimension arrays +-----------+ +-----------+ | StrList1 | | StrList2 | +-----------+ +-----------+ | 0 | Jenny | | 0 | Jenny | | 1 | Fred | | 1 | Fred | | 2 | Sony | | 2 | Rudy | | 3 | Marti | | 3 | Marti | | 4 | Rudy | | 4 | Sony | +-----------+ +-----------+ You want to compare the second coloumn of each array. if the values is match then will return the row index. Make sure, the arrays have a same data type or you can convert it to specific datatype before you campare the values. ------------------------------------------------- Dim RowIdx() as Integer = {} For i as Integer=0 to StrList1.GetUpperBound(0) if StrList1(i, 1) = StrList2(i, 1) then 'you can change with yours Dim x as integer = RowIdx.GetUpperBound(0) Redim RowIdx(x + 1) Dim y as Integer = RowIdx.GetUpperBound(0) RowIdx(y)=i End If Next ---------------------------------------------------- Is it what you mean? -- modified at 7:41 Thursday 3rd August, 2006

                          S Offline
                          S Offline
                          ssbelfast
                          wrote on last edited by
                          #12

                          thanks but these are arraylists, not arrays so yes the data rows look like this +-----------+ +-----------+ | StrList1 | | StrList2 | +-----------+ +-----------+ | 0 | Jenny | | 0 | Jenny | | 1 | Fred | | 1 | Fred | | 2 | Sony | | 2 | Rudy | | 3 | Marti | | 3 | Marti | | 4 | Rudy | | 4 | Sony | +-----------+ +-----------+ And yes, I want to count the number of times the second value in each row matches, i;e Jenny = Jenny 1, Fred = Fred 2, Sony not = Rudy etc But arraylists are one dimensional Okay, no problem I think because 0 Jenny is still = 0 Jenny; i;e; if I have 0 Jenny and 1 Jenny, it's not a match, its only a match when every thing in the datarow is the same So I tried this with the arraylist, assuming a one dimensional arraylist Dim RowIdx() As Integer = {} For r As Integer = 0 To 9 If arraylist1(r) = arraylist2(r) Then Dim x As Integer = RowIdx.GetUpperBound(0) ReDim RowIdx(x + 1) Dim y As Integer = RowIdx.GetUpperBound(0) RowIdx(y) = i End If Next but I get the same error message I've been getting all lone Operator is not valid for type ( field name) I've had the logic working for arrays, but the program uses arraylists I've tried copying the arraylist to an array, but it doesn't work

                          N 2 Replies Last reply
                          0
                          • S ssbelfast

                            I should not need to increment 'r' the for - next structure does that the count is a count of the number of times the two rows match, dimmed before the for next statement but when I try to run this I get a message Operator is not valid for type

                            S Offline
                            S Offline
                            Stephen McGuire
                            wrote on last edited by
                            #13

                            Oh! You are right. I'm sorry...don't know what I was thinking there! Did you try: str1 = ArrayList1(0) str2 = ArrayList2(0) and then compare the strings? Steve

                            S 1 Reply Last reply
                            0
                            • S Stephen McGuire

                              Oh! You are right. I'm sorry...don't know what I was thinking there! Did you try: str1 = ArrayList1(0) str2 = ArrayList2(0) and then compare the strings? Steve

                              S Offline
                              S Offline
                              ssbelfast
                              wrote on last edited by
                              #14

                              I've tried various combinations setting = to a string, using Cstr, referencing field, not referencing field I've also tried copying to a array using copyto and setting an specific row = to a specific row in an array, but nothing works My compare logic works for arrays, if I could figure out how to copy the arralyist to an array I would do the compare there

                              S 1 Reply Last reply
                              0
                              • S ssbelfast

                                I've tried various combinations setting = to a string, using Cstr, referencing field, not referencing field I've also tried copying to a array using copyto and setting an specific row = to a specific row in an array, but nothing works My compare logic works for arrays, if I could figure out how to copy the arralyist to an array I would do the compare there

                                S Offline
                                S Offline
                                Stephen McGuire
                                wrote on last edited by
                                #15

                                Ok. Give me a while and I'll try doing it myself. I'll get back to you... Steve

                                S 1 Reply Last reply
                                0
                                • S Stephen McGuire

                                  Ok. Give me a while and I'll try doing it myself. I'll get back to you... Steve

                                  S Offline
                                  S Offline
                                  ssbelfast
                                  wrote on last edited by
                                  #16

                                  I'm thinking actually to create two sets of data one in arraylists and one in arrays, sounds like a bad design, but there are things that I can only figure out how to do in an arraylist and things I can only figure out how to do in an array Anyway, if I do this, my problem is solved, I can make it work this way thanks

                                  S 1 Reply Last reply
                                  0
                                  • S ssbelfast

                                    I'm thinking actually to create two sets of data one in arraylists and one in arrays, sounds like a bad design, but there are things that I can only figure out how to do in an arraylist and things I can only figure out how to do in an array Anyway, if I do this, my problem is solved, I can make it work this way thanks

                                    S Offline
                                    S Offline
                                    Stephen McGuire
                                    wrote on last edited by
                                    #17

                                    I have just tried it and this works: Dim Arr1 As ArrayList Dim Arr2 As ArrayList Arr1 = New ArrayList(10) Arr2 = New ArrayList(10) Arr1.Add("Fred") Arr2.Add("Fred") If Arr1.Item(0) = Arr2.Item(0) Then MessageBox.Show("There is a match!") Else MessageBox.Show("There is no match...") End If Change the string for Arr2 and you will get the 'no match' message. Hope that helps. Steve

                                    S 1 Reply Last reply
                                    0
                                    • S Stephen McGuire

                                      I have just tried it and this works: Dim Arr1 As ArrayList Dim Arr2 As ArrayList Arr1 = New ArrayList(10) Arr2 = New ArrayList(10) Arr1.Add("Fred") Arr2.Add("Fred") If Arr1.Item(0) = Arr2.Item(0) Then MessageBox.Show("There is a match!") Else MessageBox.Show("There is no match...") End If Change the string for Arr2 and you will get the 'no match' message. Hope that helps. Steve

                                      S Offline
                                      S Offline
                                      ssbelfast
                                      wrote on last edited by
                                      #18

                                      Steve got it working ,great thanks for all your help (also Steve)

                                      S 1 Reply Last reply
                                      0
                                      • S ssbelfast

                                        Steve got it working ,great thanks for all your help (also Steve)

                                        S Offline
                                        S Offline
                                        Stephen McGuire
                                        wrote on last edited by
                                        #19

                                        Great! Pleased I could help. Steve

                                        1 Reply Last reply
                                        0
                                        • S ssbelfast

                                          thanks but these are arraylists, not arrays so yes the data rows look like this +-----------+ +-----------+ | StrList1 | | StrList2 | +-----------+ +-----------+ | 0 | Jenny | | 0 | Jenny | | 1 | Fred | | 1 | Fred | | 2 | Sony | | 2 | Rudy | | 3 | Marti | | 3 | Marti | | 4 | Rudy | | 4 | Sony | +-----------+ +-----------+ And yes, I want to count the number of times the second value in each row matches, i;e Jenny = Jenny 1, Fred = Fred 2, Sony not = Rudy etc But arraylists are one dimensional Okay, no problem I think because 0 Jenny is still = 0 Jenny; i;e; if I have 0 Jenny and 1 Jenny, it's not a match, its only a match when every thing in the datarow is the same So I tried this with the arraylist, assuming a one dimensional arraylist Dim RowIdx() As Integer = {} For r As Integer = 0 To 9 If arraylist1(r) = arraylist2(r) Then Dim x As Integer = RowIdx.GetUpperBound(0) ReDim RowIdx(x + 1) Dim y As Integer = RowIdx.GetUpperBound(0) RowIdx(y) = i End If Next but I get the same error message I've been getting all lone Operator is not valid for type ( field name) I've had the logic working for arrays, but the program uses arraylists I've tried copying the arraylist to an array, but it doesn't work

                                          N Offline
                                          N Offline
                                          Nouvand
                                          wrote on last edited by
                                          #20

                                          Hmm.. have you check the datatype on both arrays.. I believe you knew that Integer and Long were not the same things and it would throw exception if you compare those values. or try to edit this line: .... If arraylist1(r) = arraylist2(r) Then .... replace with: .... if CType(arrayList1(r), Object) = CType(arrayList2(r), Object) Then .... you can replace Object data type into another data type but remember to set the sama data type on both side of comparison process.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups