Results 1 to 16 of 16

Thread: my own collection

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    my own collection

    when you create a listview, it has an items property. this returns a collection.
    so you can do
    for each item in myListView.Items
    ...
    next

    now, if i make a class myItemGroup which contains the class myItem and a property items() that returns an array of myItems, how can i use this same functionality that i can do

    for each myItem in myItemGroup
    ...
    next

    ??? thanks for the help.. (i can explain more if this is unclear)

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Just like you said yourself. (If I understand correctly)

    You can do a For...Each on all kinds of collections.
    Arrays, ArrayLists, Collections etc.

    VB Code:
    1. Dim mi as MyItem
    2.  
    3. For Each mi In MyClass.Items
    4. '
    5. '
    6. '
    7. Next

    You Items property can be a normal Array, a collection or anything like that.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    If you're still not sure let me know...I could try and whip up a small sample for you.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    MSDN has a good example of creating your own collections if you do a search on collectionbase

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You need to implement IEnumerable (or something like this) .

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    If I can remember, tomorrow I will post a custom collection class that you can modify to create a strongly typed collection of custom objects. I also have it implementing the IBinding interface, which is great for binding the collection to datagrids...

    I will try to remember, but if you send me an email, that will help refresh my memory.

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    OK, first off, I know it is C# code..., but it is all I use anymore, and if I ever get around to it, I will convert it. Someone else can take the time to convert it if they wish.

    Now, the class is pretty simple. First, change the namespace to match yours...duh.

    Next, change the name of the class to better represent what it is holding. Right now it is named MyObjectCollection. This indicates it holds a collection of MyObjects. So lets say you want it to hold a collection of UserObject's. You would rename it to UserObjectCollection. (of course, your own naming conventions can also apply, mine are just suggestions) When you change the name of the class, you also have to change the name of the constructor to match.

    Last but not the least, you need to strongly type your collection. To do that, do a search and replace for "MyObject" and replace with "UserObject" (if that is the object type you are holding in this class).

    Now you can create a instance of this class, and add your objects to it. You can foreach through your objects, and you can bind to bindable datasources such as datagrids, combo boxes, listboxes, etc. Use property names as the valuemember/displaymembers and you are all set.

    I can code up an example on how this is used, but it won't be till after this weekend sometime. I have a completely full schedule through Tuesday of next week.

    Code:
    using System; 
    using System.Collections; 
    using System.ComponentModel; 
    
    
    namespace MYNAMESPACE // Change 
    { 
    public class MyObjectCollection : 
    System.Collections.CollectionBase, IBindingList 
    { 
    #region Main Methods Used 
    public MyObjectCollection() 
    { 
    
    } 
    
    public MyObject this[int index] 
    { 
    get 
    { 
    return ((MyObject)List[index]); 
    } 
    set 
    { 
    List[index] = value; 
    } 
    } 
    public int Add(MyObject value) 
    { 
    int index = List.Add(value); 
    OnListChanged(new 
    ListChangedEventArgs(ListChangedType.ItemAdded, index)); 
    return index; 
    } 
    
    public int IndexOf(MyObject value) 
    { 
    return(List.IndexOf(value)); 
    } 
    
    public void Insert(int index, MyObject value) 
    { 
    List.Insert(index, value); 
    OnListChanged(new 
    ListChangedEventArgs(ListChangedType.ItemAdded, index)); 
    } 
    
    public void Remove(MyObject value) 
    { 
    int index = List.IndexOf(value); 
    List.Remove(value); 
    if (index >= 0) OnListChanged(new 
    ListChangedEventArgs(ListChangedType.ItemDeleted, index)); 
    } 
    
    public new void RemoveAt(int index) 
    { 
    base.RemoveAt(index); 
    if (index >= 0) OnListChanged(new 
    ListChangedEventArgs(ListChangedType.ItemDeleted, index)); 
    } 
    
    public bool Contains(MyObject value) 
    { 
    // If value is not of type MyObject, this will return 
    false. 
    return(List.Contains(value)); 
    } 
    
    public object AddNew() 
    { 
    MyObject itm = new MyObject(); 
    List.Add(itm); 
    return itm; 
    } 
    #endregion 
    
    #region Events 
    /// <summary>Fires the ListChanged event if any delegates 
    are registered with it.</summary> 
    /// <param name="e">Arguments with which to fire the 
    event.</param> 
    private void OnListChanged(ListChangedEventArgs e) 
    { 
    if (_listChanged != null) _listChanged(this, e); 
    } 
    
    /// <summary>Occurs when the list changes or an item in the 
    list changes.</summary> 
    private ListChangedEventHandler _listChanged; 
    
    /// <summary>Occurs when the list changes or an item in the 
    list changes.</summary> 
    public event ListChangedEventHandler ListChanged 
    { 
    add { _listChanged += value; } 
    remove { _listChanged -= value; } 
    } 
    #endregion 
    
    #region Forcing DataBind Refresh 
    /// <summary>Force the list to notify any binders that the 
    list has been changed.</summary> 
    public void ResetNotification() 
    { 
    OnListChanged(new 
    ListChangedEventArgs(ListChangedType.Reset, -1)); 
    } 
    #endregion 
    
    #region IBindingList Default Properties 
    /// <summary>Gets whether you can add items to the list 
    using AddNew.</summary> 
    public bool AllowNew { get { return true; } } 
    /// <summary>Gets whether you can update items in the 
    list.</summary> 
    public bool AllowEdit { get { return true; } } 
    /// <summary>Gets whether you can remove items from the 
    list, using Remove or RemoveAt.</summary> 
    public bool AllowRemove { get { return true; } } 
    /// <summary>Gets whether the list supports 
    sorting.</summary> 
    public bool SupportsSorting { get { return false; } } 
    /// <summary>Gets whether the list supports searching using 
    the Find method.</summary> 
    public bool SupportsSearching { get { return false; } } 
    /// <summary>Gets whether a ListChanged event is raised when 
    the list changes or an item in the list changes.</summary> 
    public bool SupportsChangeNotification { get { return true; 
    } } 
    #endregion 
    
    #region NotSupportedExceptions 
    /// <summary>Not supported.</summary> 
    public void 
    AddIndex(System.ComponentModel.PropertyDescriptor property) { throw new 
    NotSupportedException(); } 
    /// <summary>Not supported.</summary> 
    public void 
    ApplySort(System.ComponentModel.PropertyDescriptor property, 
    System.ComponentModel.ListSortDirection direction) { throw new 
    NotSupportedException(); } 
    /// <summary>Not supported.</summary> 
    public int Find(System.ComponentModel.PropertyDescriptor 
    property, object key) { throw new NotSupportedException(); } 
    /// <summary>Not supported.</summary> 
    public void RemoveSort() { throw new 
    NotSupportedException(); } 
    /// <summary>Not supported.</summary> 
    public void 
    RemoveIndex(System.ComponentModel.PropertyDescriptor property) { throw 
    new NotSupportedException(); } 
    /// <summary>Not supported.</summary> 
    public ListSortDirection SortDirection { get { throw new 
    NotSupportedException(); } } 
    /// <summary>Not supported.</summary> 
    public PropertyDescriptor SortProperty { get { throw new 
    NotSupportedException(); } } 
    /// <summary>Not supported.</summary> 
    public bool IsSorted { get { throw new 
    NotSupportedException(); } } 
    #endregion 
    } 
    }

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by kleinma
    MSDN has a good example of creating your own collections if you do a search on collectionbase
    My code above uses that example your talking about, as well as some IBindable example I found somewhere on the net.

  9. #9

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    Originally posted by hellswraith
    Last but not the least, you need to strongly type your collection. To do that, do a search and replace for "MyObject" and replace with "UserObject" (if that is the object type you are holding in this class).
    what does making my collection strongly typed mean?

  10. #10
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    In a standard collection you can add all kinds of objects.

    If it's strongly typed you can only add objects of a specific type.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  11. #11

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    anybody have an example of this in vb.net rather than c#?

  12. #12
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Here you go...
    Attached Files Attached Files
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  13. #13
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by marvinklein
    what does making my collection strongly typed mean?
    Like said above, it makes it to where the collection can only contain one specific type of object. The compiler won't let a object of another type be held in the collection. The opposite end would be a object collection, which is what you get with an ArrayList.

  14. #14
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by marvinklein
    anybody have an example of this in vb.net rather than c#?
    If I have time next week, I can convert it. I just have very little time, and I don't want to have an error in my conversion and you all cursing me out...lol.

  15. #15
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by pax
    Here you go...
    That collection doesn't do much but let you add. Not that useful.

  16. #16
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Originally posted by hellswraith
    That collection doesn't do much but let you add. Not that useful.
    But it's strongly typed (if I understand the term correctly).
    And that is what he was looking for.
    And the original question was how to do this with his own properties.

    Now, he can just make a property of the collectiontype and it will work.

    Normally you would have some other features in there, but since the dude have never made this before, I thought it best to take one step at the time.

    The IndexOf and other functions like that are just overriding existing functions, and should be easy to do when you first get the idea. After all, it's just a matter of casting.

    I have no doubt that your class is just fine, but it may be a little overkill for a guy just trying to understand what it's all about.

    I'm sorry to disappoint you, but what were you expecting? (no offense intended )

    Edit: I accidently wrote overloads. That should of course have been override.
    Last edited by pax; Jul 24th, 2004 at 02:37 AM.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width