Collection events

Hi, just wondering if it is possible to have a collections base class send an event to the collection. Not worded well but heres an example.

Class Tree
Property color
Property species

Class TreeCollection
property ColorCount
property SpeciesCount

This is very basic but what I want is if the color of a tree changes an event is fired that the collection sees to reset its count of trees with a certain color.

Hope this is clear enough


Regards


Answer this question

Collection events

  • Sontiago

    Nothing wrong with asking for more help if what has been provided is something you cant use. I might suggest however that you take a little time and try to become "multi-lingual"; in .NET, the languages are so similar that it is pretty easy to pick up a new language once you have already learned one, and it can be a great advantage.

    Here's the VB.NET equivalent:



    Class Tree
    Public ReadOnly Collection As TreeCollection
    Public Sub New(ByVal collection As TreeCollection)
    Collection = collection

    End Sub

    Private _Color As Color

    Public Property Color As Color

    Get
    return _Color
    End Get

    Set
    _Color = value
    Collection.UpdateRequired()
    End Set

    End Property

    End Class
    Class TreeCollection
    Inherits List(Of Tree)
    Public Sub UpdateRequired()
    'Do stuff here
    End Sub

    End Class

    Note: I translated this by sight, and I havent used VB in a while, so I apologize if there are any egregious syntax errors. But that should at least get you started.



  • Fyodor Golos

    I'm sorry, but I don't use VB and can't write same thing in valid VB syntax.

  • MisterT006

    Never mind, thanks for your time anyway :)

  • ArnieM

    Hi!

    Add to Tree reference to the collection it is in. If Tree changed, it can call collection for update. This is better than events, because events require to attach/detach events and internal implementation require much more resources (memory & CPU).

    class Tree
    {

    public readonly TreeCollection Collection;

    public Tree(TreeCollection collection)
    {

    Collection = collection;

    }

    Color _Color;

    public Color Color
    {

    get { return _Color;}

    set { _Color = value; Collection.UpdateRequired(); }

    }

    }

    class TreeCollection : List<Tree>
    {

    public void UpdateRequired()

    {

    ...

    }

    }



  • cmsturg

    You could change it to inherit from CollectionBase intead, so you dont have to do all the re-learning at once. It will work just fine either way. List(Of T) is a better way though, since it gives you built-in strongly typed access to your Tree items without having to write all of the access methods yourself. If you have the time I would suggest looking in the MSDN and online for examples of generics, and then going that direction.

  • chrisoldfield

    Thanks, I totally agree with learning both languages. I'm from VB6 background (only 1 year) and am learning OO & VB.Net for syntax reasons, I absolutely love .Net and OO, unfortunately learning all at once is somewhat of a task as I work alone and don't have anyone to bounce ideas off.
    The problem I had with the above method initially is that my Collection class used the "Implements System.Collections.CollectionBase", changeing to "List (of Tree)" means re-learning how to interact with my collection. I lost access the item property of the Collection. I'll keep bashing away at your example and make it work.


  • Kathleen Koclanes

    Try as I might I cannot seem to get this to work in VB.Net.
    It appears that everytime I create a tree instance i create a new intance of collection which does not work, a circular reference occured, probably in my attempt to convert to VB.
    This is a request that will probably get "blasted" by C#'ers but could you possibly give example in VB.Net


    Regards

  • Collection events