Comparing Structure Objects w/ LastIndexOf

  • Thread starter ccshine via DotNetMonster.com
  • Start date
C

ccshine via DotNetMonster.com

I'm working on an app that implements a Structure to store a recordset in an
ArrayList. I used this setup to bind to a DataGrid and it worked out so well,
I thought it might be a better solution that moving back and forth through a
DataSet to perform some simple though numerous calculations.

In order to perform these calcs, I have to compare elements of the array
checking for duplicates on some columns and then just comparing others to
strings utilizing IndexOf and LastIndexOf. Unfortunately I haven't been able
to get it to work. On another site I read that these 2 methods use Object.
Equals and as the array contains structure objects regardless of whether the
values appear to be equal or not, both methods will always return -1. It
also stated that the Equals function needs to be overridden. I tried this
as well with no luck.

The code below generates an index is out of range error on the line that
assigns the array item to 'last' because the LastIndexOf search returns -1.
Also noteworthy is that the stored procedure I use to retrieve the recordset
guarantees that there would be a match on first.account. Can anyone point me
in the right direction?


Public Overrides Function Equals(ByVal obj As Object) As Boolean

If obj Is Nothing Or Not Me.GetType() Is obj.GetType() Then

Return False

End If

Dim d As duplicates = CType(obj, duplicates)

Return Me.account = d.account

End Function


'Errant code below

conn.Open()


Dim reader As SqlDataReader = cmd.ExecuteReader()

If reader.HasRows Then

'Create array to hold entries for accounts tracked more than once per LOB

Dim arrResults As New ArrayList

Dim dupEntries As New duplicates

While reader.Read

With arrResults
..Add(

New duplicates(reader.GetValue(0), reader.GetValue(1), reader.GetValue(2), _
reader.GetValue(3), reader.GetValue(4), reader.GetValue(5), reader.GetValue(6)
, _

reader.GetValue(7), reader.GetValue(8), reader.GetValue(9), reader.GetValue
(10), _

reader.GetValue(11), reader.GetValue(12), reader.GetValue(13), reader.
GetValue(14)))


End With

End While
reader.Close()


Dim first As duplicates

Dim last As duplicates

Dim i As Integer

For i = 0 To arrResults.Count - 1
first =

CType(arrResults.Item(i), duplicates)
Trace.Warn(arrResults.LastIndexOf(first.account))

last = arrResults.Item(arrResults.LastIndexOf(first.account))

Trace.Warn(

"first date" & first.tDate)
Trace.Warn(

"last date " & last.tDate)

Next

End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top