Delegate Using "this"
I'd like to create a class that has overrideable methods in the form of
delegates.
I basically would like to create an interface, but not have to create a
new class every time I want to make it slightly different.
Further, I would like to bundle the delegate with a number of other
variables in a struct.
Now here are some more specifics.
class Gun
{
public delegate void ShootDelegate;
public double fireRate; //There are more variables, I'm just using
this one as an example
public Gun(GunStats stats)
{
this.Shoot = stats.Shoot;
this.fireRate = stats.fireRate;
}
public ShootDelegate Shoot;
}
struct GunStats
{
public ShootDelegate Shoot;
public double fireRate;
}
then, what I'd like to be able to do is make a gun like this
GunStats stats;
stats.fireRate = 3;
stats.Shoot = new delegate() { this.fireRate++; /* stupid example */ };
new Gun(stats);
however, when I create the delegate, it obviously can't interact with the
inner class variables.
What's the best way to handle this?
No comments:
Post a Comment