Art,
You mean something like:
Public Class Class1
Private ReadOnly m_region As String
Private m_value As Integer
Public Sub New(ByVal region As String)
m_region = region
End Sub
Public ReadOnly Property Region() As String
Get
Return m_region
End Get
End Property
Public Property Value() As Integer
Get
Return m_value
End Get
Set(ByVal value As Integer)
m_value = value
End Set
End Property
' Needed for ArrayList.IndexOf
Public Overloads Function Equals(ByVal other As Class1) As Boolean
Return m_region = other.m_region
End Function
' Needed for ArrayList.IndexOf
Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is Class1 Then
Return Equals(DirectCast(obj, Class1))
Else
Return False
End If
End Function
' should override if overriding Equals
' allows Class1 itself to be used as a key in a HashTable
Public Overrides Function GetHashCode() As Integer
Return m_region.GetHashCode()
End Function
End Class
Public Class Class2
Public Shared Sub UseArrayList()
Dim list As New ArrayList
Dim reader As System.Data.SqlClient.SqlDataReader
Do While reader.Read
Dim region As New Class1(CStr(reader!region))
If list.Contains(region) Then
region = DirectCast(list(list.IndexOf(region)), Class1)
End If
region.Value += CInt(reader!amount)
Loop
End Sub
Public Shared Sub UseDictionary()
Dim list As New Hashtable
Dim reader As System.Data.SqlClient.SqlDataReader
Do While reader.Read
Dim region As Class1
If list.Contains(reader!region) Then
region = DirectCast(list(reader!region), Class1)
Else
region = New Class1(CStr(reader!region))
list.Add(reader!region, region)
End If
region.Value += CInt(reader!amount)
Loop
End Sub
End Class
I would normally create a Class1Collection that inherits from either
CollectionBase (uses an ArrayList) or DictionaryBase (uses a Hashtable)
allowing for type safe methods, I would give Class1Collection a method that
would do the above updating or adding of a new Class1 object...
Hope this helps
Jay
"Art" <Ar*@discussions.microsoft.com> wrote in message
news:AB**********************************@microsof t.com...
Hi,
Class1 will be instantiated for each "region".
Class2 will do the instantiation as it reads through records in a
database.
In Class2 I need to create and use the information in an instance of
Class1.
As I read a record with a new "region" I need to instantiate a new Class1.
If I read a record with a "region" I've already seen, I'll need to update
the
appropriate member of Class1.
I was thinking that I might create a DictionaryEntry in Class2 to hold the
instances of Class1, but I'm not sure how to do that. Alternatively I
could
create ArrayList1 to hold the instances and also an ArrayList2 with just
the
"regions". Then I could use ArrayList2.IndexOf to find which member of
the
ArrayList1.
I'm guessing that this is a common activity and someone can tell me the
best
way to do this.
Thanks,
Art