comparing values on fields in arraylists
-
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 "
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
-
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
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
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
Ok. Give me a while and I'll try doing it myself. I'll get back to you... Steve
-
Ok. Give me a while and I'll try doing it myself. I'll get back to you... Steve
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
-
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
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
-
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
-
Great! Pleased I could help. Steve
-
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
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.
-
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
Or... Here we have 2 array list ArrList1 -------- arrayA1 * arrayA2 * arrayA3 * arrayA4 * -------- ArrList2 -------- ArrayB1 * arrayB2 * arrayB3 * arrayB4 * -------- *= 1 dimensional array. Lets say you want to compare arrayA2(n).Length with arrayB2(n).Length. But before we compare the values, we need to compare the bounds of arrays. ----------------------- Dim x as integer = ArrList1.GetUpperBound(0), _ y as integer = ArrList2.GetUpperBound(0) if x = y then For i as integer = 0 to x If ArrList1(i).Length = ArrList2(i).Length Then 'put your code here End If Next End If You can't compare "arraylist1(r) = arraylist2(r)" because the value on both side is an array, not property of array. Every array has properties(Length, UpperBound, LowerBound, i;e) I mean you can't compare your gf with your friend's gf, except they properties such as skin color, cute face, height, etc. ;P Here sample code to copy an array from array list to a new array: Dim newarr As Object() = ArrList1(1).Clone Good Luck -- modified at 4:59 Friday 4th August, 2006