473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Dictionary? ArrayList?

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
4 1829
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: JohnK | last post by:
ok, ya got me here. I'm trying to removing items from a dictionary inside a loop. Obviously using enumeration does not work as that assumes the dictionary stays unchanged. So how so I iterate...
11
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be...
1
by: Adnan | last post by:
Hi, I am porting an existing ASP applcation to ASP.NET which heavily depends on Mainpulation through Scripting.Dictionary. Can anyone help me out with an alternative to use of Scripting.Dictionary....
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
6
by: Fao, Sean | last post by:
Is it possible to bind a GridView to a generic Dictionary object? When I try it in my ASP.NET application, it throws an exception acknowledging that the specified field or property does not exist....
3
by: shrishjain | last post by:
Hi All, I have to make multiple dictionary objects(Dictionary<string key, int value>) with same set of string keys, and that really eats up lot of space. I am trying to find a solution where I...
20
by: Gustaf | last post by:
This is two questions in one really. First, I wonder how to convert the values in a Dictionary to an array. Here's the dictionary: private Dictionary<Uri, Schemaschemas = new Dictionary<Uri,...
4
by: Marek Zegarek | last post by:
Hello! I'm trying to write a simple sub, but somewhere I'm making a mistake..... Problem is somewhere in adding elements to arraylist or dictionary.... I need to add in Dictionary one string...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.