NameObjectCollectionBase or something else?

  • Thread starter Thread starter Chad Z. Hower aka Kudzu
  • Start date Start date
C

Chad Z. Hower aka Kudzu

I have a text file which is of the format:

01001=AGAWAM, MA
01002=CUSHMAN, MA
01005=BARRE, MA
01007=BELCHERTOWN, MA
01008=BLANDFORD, MA
01010=BRIMFIELD, MA
01011=CHESTER, MA


I need to load it into some class and access it by specifying the name (ie
01002) and so on. This is just for a demo - so I dont want to go over kill
with a DB or other. Just a simple solution.

In Delphi I can load this into a StringList easily:

FMyList.LoadFromFile(file name);

Then get values like this:

LCity := FMyList.Values['01002'];

I am hoping that I am just missing something but this appears to be a lot
more work to do in .Net.

NameObjectCollectionBase appears to be the best suited class. Is this
correct?

If so - how can I easily load the values from a file? It appears there is no
built in way? Is the only way to read the file and manually parse the
contents and add each pair myself?
 
Hi Chad,

Did you consider using Hashtable?
And yes, you won't be able to automatically load data formated that way.
 
Which is more appropriate?

Not the best solution, but:

Hashtable values = new Hashtable();
using (StreamReader reader = new StreamReader(@"C:\Test.txt"))
{
foreach (String line in reader.ReadToEnd().Split('\n', '\r'))
{
String[] parts = line.Split(new Char[] { '=' }, 2);
if (parts.Length == 2)
values.Add(parts[0], parts[1]);
}
}
 
Miha Markic said:
Did you consider using Hashtable?

What are the differences between hashtable and NameObjectCollectionBase ?

I ended up using a Hashtable. 2 lines in Delphi ended up being like 30 in
..net. :(

Its really a shame .net doenst realy have the equivalent of Delphi's
TStringList. It is specialized, but its VERY VERY commonly used
functionality.

I can just import it from the VCL namespace - but no good for demos.
 
Hi Chad,

NameObjectCollectionBase is an abstract class - base for more specialized
classes.
If I understand properly it combines both index and key search approach.
While Hashtable is a out of the box usable class and it supports only search
by key.
Both classes are generic (while TStringList is just string list) so I
understand why there is no support for things like you are asking.
You can derive a class by yourself and re-use it.

But, if i recall properly, TStringList is a collection and not a hashtable.
Thus, equivalent would be an ArrayList that can be xml
serialized/deserialized with few lines.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
 
Hashtable is a generic class that maps objects to objects (key and value can
be of any type).
NameObjectCollectionBase serves as a base class for your classes mapping a
string to an object and also allows you to perform a lookup by index, not
just by the key. NameValueCollection is a specialization of
NameObjectCollectionBase to map a string to more than one value.

What you're probably looking for is
System.Collections.Specialized.StringDictionary (maps a string key to a
string value, lookup by key only).
StringCollection in the same namespace additionally has support for mapping
one string to many values and for indexed lookup.

Dennis


Miha Markic said:
Hi Chad,

Did you consider using Hashtable?
And yes, you won't be able to automatically load data formated that way.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Chad Z. Hower aka Kudzu said:
I have a text file which is of the format:

01001=AGAWAM, MA
01002=CUSHMAN, MA
01005=BARRE, MA
01007=BELCHERTOWN, MA
01008=BLANDFORD, MA
01010=BRIMFIELD, MA
01011=CHESTER, MA


I need to load it into some class and access it by specifying the name (ie
01002) and so on. This is just for a demo - so I dont want to go over kill
with a DB or other. Just a simple solution.

In Delphi I can load this into a StringList easily:

FMyList.LoadFromFile(file name);

Then get values like this:

LCity := FMyList.Values['01002'];

I am hoping that I am just missing something but this appears to be a lot
more work to do in .Net.

NameObjectCollectionBase appears to be the best suited class. Is this
correct?

If so - how can I easily load the values from a file? It appears there
is
 
Dennis Homann said:
Hashtable is a generic class that maps objects to objects (key and value
can be of any type).
NameObjectCollectionBase serves as a base class for your classes mapping
a string to an object and also allows you to perform a lookup by index,
not just by the key. NameValueCollection is a specialization of
NameObjectCollectionBase to map a string to more than one value.

Its too bad the .Net docs dont have a good comparison (At least not that I
can find).

It also seems that some of the classes are just to specialized. I realize
some are there for the lack of generics, but still...
What you're probably looking for is
System.Collections.Specialized.StringDictionary (maps a string key to a
string value, lookup by key only).

Thanks. This one is a bit better than hashtable. I changed it and could
remove the cast I had to use with hashtable.
 
Miha Markic said:
But, if i recall properly, TStringList is a collection and not a
hashtable. Thus, equivalent would be an ArrayList that can be xml
serialized/deserialized with few lines.

Its kind of a collection. Its very slow on many of its more advanced
functions - but at least it has them. :)
 
Lasse V?gs?ther Karlsen said:
Not the best solution, but:

Hashtable values = new Hashtable();
using (StreamReader reader = new StreamReader(@"C:\Test.txt"))
{
foreach (String line in reader.ReadToEnd().Split('\n', '\r'))
{
String[] parts = line.Split(new Char[] { '=' }, 2);
if (parts.Length == 2)
values.Add(parts[0], parts[1]);
}
}

Thanks. I need to remember that foreach is available in C#. The problem with
this is that it reads the whole file in, which is not huge bit is large. I've
used this instead:

using (StreamReader LFile = new StreamReader(
new FileInfo
(System.Windows.Forms.Application.ExecutablePath).DirectoryName
+ @"..\..\..\..\..\ZipCodes.dat")) {
string s;
string[] t;
while ((s = LFile.ReadLine()) != null) {
t = s.Split(new Char[] {'='});
FZipCodeList.Add(t[0], t[1]);
}
}

Anyone see a better way to do this?

I could cache the array of = I guess.
 
Back
Top