The only reason that I asked is that the enumerator is usually something
that you don't touch directly. Instead, if the object that you want to loop
through supports IEnumerable, you generally use a foreach loop (which uses
the enumerator under the covers for your). The foreach lets you declare a
variable for the member type of the collection you are enumerating. The form
looks like this:
foreach(MemberType memberVar in CollectionToBeEnumerated)
{
}
Basically, this uses the enumerator to loop through every member in the
collection. Each member is cast as MemberType and assigned to memberVar.
So, you could rewrite your code something like this (haven't tried
compiling, might need some tweaking):
foreach(Control aControl in pnlList.Controls)
{
if(aControl is ListItemControl)
i++;
}
NOTE: I am using Control because I know that all of the members of Controls
inherit from control. If I use a type that is too restrictive I will get
invalid cast errors. So, if I used TextBox, and there was a button in the
Controls collection, I would get an invalid cast exception when I hit the
button.
"h" <my@address.com> wrote in message
news:h5********************************@4ax.com...
On Fri, 11 Jun 2004 10:54:18 -0500, "J.Marsch"
<je****@ctcdeveloper.com> wrote:
Oh, I'm curious: Why did you explicitly retrieve the enumerator instead
ofjust using a foreach() loop?
I'm still getting used to C# (late transition from VB which I have
used for quite a while). I was looking for "better" or other way to do
this.
Can you give advice if this is preferable way or not?
thanks everyone for your replies!
h