Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / January 2006

Tip: Looking for answers? Try searching our database.

My own enumerator for BindingList<T>?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Benton - 14 Jan 2006 21:38 GMT
Hi there,

I need to override the GetEnumerator() of my BindingList<BusinessObject>
class. The BusinessObject class itself has a "Visible" property and I want
to filter out "invisible" objects with a custom GetEnumerator(). But I can't
find a way to declare/override the base GetEnumerator() of BindingList<T>.

Thanks for any help,

-Benton
Rene - 14 Jan 2006 23:46 GMT
Declare your own GetEnumerator()using the "new" keyword. As long as you "for
each" without casting to the base interface you should be ok.

> Hi there,
>
[quoted text clipped - 7 lines]
>
> -Benton
Benton - 14 Jan 2006 23:57 GMT
> 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
Helge Jensen - 18 Jan 2006 11:17 GMT
> Hi there,
>
> I need to override the GetEnumerator() of my BindingList<BusinessObject>
> class. The BusinessObject class itself has a "Visible" property and I want
> to filter out "invisible" objects with a custom GetEnumerator(). But I can't
> find a way to declare/override the base GetEnumerator() of BindingList<T>.

You can easily write a helper class, somewhat like:

class Visibles: IEnumerable {
 public readonly IEnumerable Parent;
 public Visibles(IEnumerable parent) { this.Parent = parent; }
 public IEnumerator GetEnumerator() {
   foreach ( BusinessObject bo in Parent )
     if ( bo.Visible )
       yield return bo;
 }
}

Use as:

foreach ( BusinessObject bo in new Visibles(a_bindinglist) )
  // bo.Visible == true

You can add genericity if you like.

Signature

Helge Jensen
 mailto:helge.jensen@slog.dk
 sip:helge.jensen@slog.dk
              -=> Sebastian cover-music: http://ungdomshus.nu <=-


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.