Dimitar Vouldjeff
Dimitar Vouldjeff

Reputation: 2107

Raise event among all instances of a class

I have a abstact class named Organism which has a Born event. Also I have some classes that inherit the Organism class... So my question is how can I raise the Born event among all instances of all classes, which inherited the Organism class?

EDIT: Sorry, The event I meant to write was Tick not Born... Born is a instance level event...

Upvotes: 3

Views: 1197

Answers (4)

Jimmy Hoffa
Jimmy Hoffa

Reputation: 5967

public class Organism : IDisposable
{
    public static List<Organism> All = new List<Organism>();

    private bool disposed = false;

    public Organism()
    {
        Organism.All.Add(this);
    }

    public void BeBorn()
    {
        throw new NotImplementedException();
    }

    public void Dispose()
    {
        Dispose(true);

        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                Organism.All.Remove(this);
            }

            disposed = true;
        }
    }

    ~Organism()
    {
        Dispose(false);
    }

}

Upvotes: 2

Steven Evers
Steven Evers

Reputation: 17196

Why wouldn't each concrete organism invoke it's parent's Born event when it's birthed. Something like:

public abstract class Organism
{
    public event EventHandler<EventArgs> Born;
    public string Name { get; set; }

    public Organism()
    {
    }

    public void Birth()
    {
        this.raiseBorn(new EventArgs());
    }

    protected void raiseBorn(EventArgs args)
    {
        EventHandler<EventArgs> handler = this.Born;
        if (handler != null)
            handler(this, args);
    }
}

// Concrete organisms ========--------

public class Dog : Organism
{
    public Dog()
    {
        this.Name = "Dog";
    }

    public override string ToString()
    {
        return this.Name;
    }
}

public class Cat : Organism
{
    public Cat() 
    {
        this.Name = "Cat";
    }

    public override string ToString()
    {
        return this.Name;
    }
}

// Creature Factory ========--------

public class CreatureCreator : List<Organism>
{
    public event EventHandler<BornArgs> CreaturBorn;
    private void raiseCreatureBorn(BornArgs args)
    {
        EventHandler<BornArgs> handler = this.CreaturBorn;
        if (handler != null)
            handler(this.CreaturBorn, args);
    }

    public void CreateCreature<T>() where T : Organism, new()
    {
        Organism o = new T();
        o.Born += o_Born;
        this.Add(o);

        o.Birth();
    }

    private void o_Born(object sender, EventArgs e)
    {
        this.raiseCreatureBorn(new BornArgs((Organism)sender));
    }
}

public class BornArgs : EventArgs
{
    public Organism Creature { get; set; }

    public BornArgs(Organism o)
    {
        this.Creature = o;
    }
}

// Usage ========--------

static void Main(string[] args)
{
    CreatureCreator deity = new CreatureCreator();
    deity.CreaturBorn += deity_CreaturBorn;

    deity.CreateCreature<Dog>();
    deity.CreateCreature<Cat>();

    Console.ReadKey();
}

static void deity_CreaturBorn(object sender, BornArgs e)
{
    Console.WriteLine(e.Creature.ToString() + " was born");
}

output:

Dog was born
Cat was born

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68687

It isn't great, but this might work:

public abstract class Organism : IDisposable
{
    private static readonly List<Organism> LiveOrganisms = 
        new List<Organism>();

    private event EventHandler onBorn;

    public void InvokeBorn(EventArgs e)
    {

        foreach (var liveOrganism in LiveOrganisms
            .Where(liveOrganism => liveOrganism.onBorn != null))
        {
            liveOrganism.onBorn(this, e);
        }
    }

    public event EventHandler Born
{
    add
    {
        onBorn += value;
    }
    remove
    {
        onBorn -= value;
    }
}

    protected Organism()
    {
        LiveOrganisms.Add(this);
    }

    public void Dispose()
    {
        LiveOrganisms.Remove(this);
    }
}

Upvotes: 0

dtb
dtb

Reputation: 217293

There is no way to get a list of all instances of some class, so you have to keep track of new instances yourself:

List<Organism> organisms = new List<Organism>();

Organism bob = new Amoeba();
organisms.Add(bob);

Later you can iterate your list of instances and make them all raise the event, one after another:

foreach (Organism organism in organisms)
{
    organism.Awake();
}

Upvotes: 0

Related Questions