By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,932 Members | 1,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,932 IT Pros & Developers. It's quick & easy.

Dictionary? ArrayList?

P: n/a
Art
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
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Art,

The use of the word "region" is a little bit dangerous in VBNet because that
is not the "culture" however more a shape, I do not know what you are after,
however I think that is not the most important part of your message.

Dictionaryentry is from the hashtable

http://msdn.microsoft.com/library/de...classtopic.asp

What can be of course your solution, where your "region" is the "key" and
the "value" your Class2 object.

Although an Arraylist of Arraylist will of course work as well, would I try
first the hashtable.

I hope this helps?

Cpr

"Art" <Ar*@discussions.microsoft.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

Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
Art
Cor,

As always, thank you for the help -- Hashtable may be what I need. Also
thank you for the warning about "region". You are correct that this is not a
critical part of my situation -- I'm using "region" to refer to a section of
the country, such as NW for North West. Perhaps I should choose a differnt
name.

Art
Nov 21 '05 #4

P: n/a
Art
Jay,

Thanks for the help -- it will take me some time to digest the information
you provided. I really appreciate your taking the time to help and to be so
complete.

Art
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.