Declare your own GetEnumerator()using the "new" keyword. As long as you "for
each" without casting to the base interface you should be ok.
> Declare your own GetEnumerator()using the "new" keyword. As long as you
> "for each" without casting to the base interface you should be ok.
Sorry, I think I don't understand your answer in full. Does that mean that I
cannot "foreach" from inside the class itself? IOW, if I inherit my class
from BindingList<BusinessObject>, can you please provide a sample foreach
that I can not do and a sample foreach that I can? I mean from inside the
class itself, samples to "foreach" using the "this" keyword.
Thanks,
-Benton
>> Hi there,
>>
[quoted text clipped - 7 lines]
>>
>> -Benton
Rene - 15 Jan 2006 01:22 GMT
> Sorry, I think I don't understand your answer in full. Does that mean that
> I cannot "foreach" from inside the class itself?
Yes you can, but you have to make sure you are "foreaching" using the
correct enumerator. Below is some sample code that shows how the Enumerator
is implemented using the "new" key word on your own class. If you
instantiate your class and do a "foreach" on it, you will loop using the
enumerator that *you* created.
Note that if you do not implement your own enumerator you would be using the
one provided by the System.Collections.ObjectModel.Collection class (base
class of the BindingList class).
// Create the class that inherits from BindingList.
public class Abc<T> : System.ComponentModel.BindingList<T>
{
public new IEnumerator<T> GetEnumerator()
{
// Return *your* IEnumerator here.
}
}
// foreach test.
static void Main(string[] args)
{
Abc<string> myAbc = new Abc<string>();
foreach (string str in myAbc)
{
// Here you are looping inside the enumerator you
implemented.
}
foreach (string str in
(System.Collections.ObjectModel.Collection<string>)myAbc)
{
// Here you are looping inside the enumerator implemented
// by the base class
(System.Collections.ObjectModel.Collection)
}
}
>> Declare your own GetEnumerator()using the "new" keyword. As long as you
>> "for each" without casting to the base interface you should be ok.
[quoted text clipped - 20 lines]
>>>
>>> -Benton
Benton - 15 Jan 2006 03:38 GMT
>> Sorry, I think I don't understand your answer in full. Does that mean
>> that I cannot "foreach" from inside the class itself?
[quoted text clipped - 7 lines]
> the one provided by the System.Collections.ObjectModel.Collection class
> (base class of the BindingList class).
Thanks a million. It's cryistal clear now.
Much appreciated,
-Benton
> // Create the class that inherits from BindingList.
> public class Abc<T> : System.ComponentModel.BindingList<T>
[quoted text clipped - 50 lines]
>>>>
>>>> -Benton