Regarding using a reader to populate a custom collection, if you
instantiate
an object for each record that goes into the collection that doesn't slow the
application down any?
Yes. In fact, every instruction you write slows down your app. If you want a
really fast app, create an app that does nothing! ;-)
Don't worry, rodchar, I haven't missed your point. Optimization is
important. But you have to think about it a bit more deeply. For example, I
only wear RockPort shoes. They cost about $80.00 a pair, which is expensive
by most standards. However, they last about 4 times as long as shoes that
cost half as much. This means that, in the long run, I'm actually saving
money by using the more expensive shoes.
The same thing goes for writing software. Sometimes what seems to be more
expensive in terms of processing actually evens out in the end. For example,
you can use a DataSet, DataReader or a DataTable alone. If you use the
fastest (DataReader) you can only read forward, and one record at a time. If
you use a DataSet or a DataTable, you've already used a DataReader behind
the scenes to create an object, populate it, and add it to a Collection (The
Rows Collection of a DataTable, which is contained in a DataSet, both of
which are populated transparently by a DataReader). This gives you backwards
and forwards movement through the result set.
In some cases, it may be useful to populate a custom Collection, rather than
to use the generic DataTable and DataSet classes. In that case, you would
probably want to think about using a DataReader to do so, as you only need
to copy the data from each record one time.
However, as you can see, you're still populating a Collection from a
DataReader. But if you design it well, the custom Collection may require a
lot less overhead to work with in the long run than one of the built-in
"Collections," such as DataSet and DataTable (classes that implement
Collections - A DataSet, for example, has about a half-dozen Collections in
it, which you may or may not need).
As for why a given sample code did things a certain way: Many people don't
realize this, but sample code is not intended for use. It is intended to
show some functionality by example. For various reasons, such as
demonstrating how a commonly-used class like a DataSet can be used, they may
use some code that isn't particularly useful in the real world. This is one
reason that I continue to emphasize the importance of understanding your
tools.
The better you understand all the parts and pieces of a .Net app, the better
you will be at putting them together in an optimal way.
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living