[C#] Delegates and passing parameters

Started by
4 comments, last by ChaosEngine 13 years, 9 months ago
I have the following delegate:

public delegate T CreateNewObjectGame(Game1 game);


and would like to use it in the following method:

        public ResourcePool(int allocation, ValidateObject objectCheck, CreateNewObjectGame objectCreate, bool isWrappable)        {            this.isWrappable = isWrappable;            this.objectCheck = objectCheck;            numberOfInvalidObjects = allocation;            objects = new T[allocation];            for (int i = 0; i < allocation; ++i)            {                T t = objectCreate();                objects = t;            }        }


The problem is that objectCreate() require game to be passed into the method but this is already the case with this:

poolItem = new ResourcePool<Item>(200, Item.IsValid, Item.Instance(this), true);


The 'this' refers to the Game1 class being passed into the delegate. So how can I then access the class when calling objectCreate without having to pass in the class again via another argument?
Advertisement
I am not quite sure what you are trying to do but I suspect the design to be wrong.

Please provide more context: what class is containing

poolItem = new ResourcePool<Item>(200, Item.IsValid, Item.Instance(this), true);

It looks like this is refering to yet another constructor of the ResourcePool class. How is this constructor related to this method?:

public ResourcePool(int allocation, ValidateObject objectCheck, CreateNewObjectGame objectCreate, bool isWrappable)

Many times when you notice that you are passing an object from method to method and you are adding it to each and every parameter list the design is flawed.


The ResourcePool class contains the following variables:

    /// A pre-allocated pool of objects with the ability to sort    /// and maintain objects as they become invalidated.    public class ResourcePool<T> where T : class    {        bool isWrappable;        int wrapIndex;        int numberOfInvalidObjects;        ValidateObject objectCheck;        T[] objects;


It pools objects of different classes.

The problem I face is that each class needs the Game1 class passed into it during the constructor stage.

ValidateObject and CreateNewObjectGame for an ammo class looks like this:

        public static bool IsValid(IAmmo ammo)        {            return ammo.isAlive;        }        public static PlasmaAmmo Instance(Game1 game)        {            return new PlasmaAmmo(game);        }


That means that:

T t = objectCreate();


should be

T t = objectCreate(game);


but how do I get the Game1 game passed in correctly using delegates?
Just pass your Game1 instance into your ResourcePool method
public ResourcePool(int allocation, ValidateObject objectCheck, Game1 game,                    CreateNewObjectGame objectCreate, bool isWrappable){    this.isWrappable = isWrappable;    this.objectCheck = objectCheck;    numberOfInvalidObjects = allocation;    objects = new T[allocation];    for (int i = 0; i < allocation; ++i)    {        T t = objectCreate(game);        objects = t;    }}


and call it like this
poolItem = new ResourcePool<Item>(200, Item.IsValid, Item.Instance, this, true);


assuming Item.Instance has a method signature that conforms to the CreateNewObjectGame delegate.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Thanks Chaos, I was went down this path after reading through my code and changing it.

I was hoping that I didn't have to pass the Game1 parameter into the pool but I guess there is no way otherwise.
Quote:Original post by Spa8nky
Thanks Chaos, I was went down this path after reading through my code and changing it.

I was hoping that I didn't have to pass the Game1 parameter into the pool but I guess there is no way otherwise.


Well, you could change your CreateNewObjectGame delegate to

public delegate T CreateNewObjectGame()


and then call your function like this

poolItem = new ResourcePool<Item>(200, Item.IsValid,   delegate() { Item.Instance(this); },  true);


or even easier with a lambda
poolItem = new ResourcePool<Item>(200, Item.IsValid,   () => Item.Instance(this),  true);


This depends on how CreateNewObjectGame will effect the rest of your code.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement