comparing values on fields in arraylists
-
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
-
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
Arrays can have multiple dimensions. ArrayList's always have exactly one dimension. Steve
-
Arrays can have multiple dimensions. ArrayList's always have exactly one dimension. Steve
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
-
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 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
-
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
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
-
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
-
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
-
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.