tags:

views:

281

answers:

6
public class KeyEvent {
        private Keys[] keys = null;
        public delegate void eventmethod();
        private eventmethod em;
        private object[] args;
        private bool thrown = false;
        public bool repeat = true;

        public bool isKey(Keys key) {
            if (keys == null) return true;
            foreach (Keys k in keys) {
                if (key == k) return true;
            }
            return false;
        }

        public void ThrowEvent() {
            if (!repeat && thrown) return;
            em.DynamicInvoke(args);
            this.thrown = true;
        }

        public KeyEvent(eventmethod D) {
            em = D;
        }
        public KeyEvent(Keys[] keys, eventmethod D) {
            this.keys = keys;
            this.KeyEvent(D);
        }
        public KeyEvent(eventmethod D, object[] args) {
            this.args = args;
            this.KeyEvent(D);
        }
        public KeyEvent(Keys[] keys, eventmethod D, object[] args) {
            this.args = args;
            this.KeyEvent(keys, D);
        }
    }

Hey, guys; this class won't compile. The error I'm getting is:

'BLBGameBase.KeyEvent' does not contain a definition for 'KeyEvent' and no extension method 'KeyEvent' accepting a first argument of type 'BLBGameBase.KeyEvent' could be found (are you missing a using directive or an assembly reference?)

Anyway, this error is thrown for the three lines that call "this.KeyEvent(D)" or similar. I don't understand the error, as it argues that no method accepts a first argument of a "KeyEvent", but I'm not trying to call a method with a KeyEvent... Thank you.

+4  A: 

To call one constructor from another, use this syntax:

public KeyEvent(Keys[] keys, eventmethod D)
    : this(D)
{
    this.keys = keys;
}

As your code stands, it is trying to call a regular method (non-constructor) called KeyEvent.

HTH,
Kent

Kent Boogaart
Thank you, works fine.
Motig
A: 

your call to this.KeyEvent(D) is generating an error because you're calling the class's own constructor in an illegal fashion. Give your KeyEvent method a return type to not make a constructor.

Tony
A: 

That's not how you call other constructors. You should use an approach like this:

    public KeyEvent(eventmethod D)
        : this(null, D, null)
    {
    }
    public KeyEvent(Keys[] keys, eventmethod D)
        : this(keys, D, null)
    {
    }
    public KeyEvent(eventmethod D, object[] args)
        : this(null, D, args)
    {
    }
    public KeyEvent(Keys[] keys, eventmethod D, object[] args) {
        this.args = args;
        this.keys = keys;
        em = D;
    }
David M
A: 

If you want to call another constructor you must use the base keyword:

public KeyEvent(eventmethod D, object[] args)
    :this(D) {
    this.args = args;
}
Megacan
+1  A: 

As Kent has said, you chain constructors using either this (to call another constructor in the same class) or base (to call a base clas constructor) with the syntax

ClassName(parameters) : this(args)
{
    // Body
}

or

ClassName(parameters) : base(args)
{
    // Body
}

Note that this means the chained constructor is executed before the rest of your constructor body - you can't make a chained constructor call after executing any other code in your body.

I would suggest that you have one constructor body which actually does something, and make all the other constructors just chain to that (directly or indirectly):

public KeyEvent(eventmethod D) : this(null, D){
}

public KeyEvent(Keys[] keys, eventmethod D) : this(keys, D, null) {
}

public KeyEvent(eventmethod D, object[] args) : this(null, D, args) {
}

public KeyEvent(Keys[] keys, eventmethod D, object[] args) {
    this.keys = keys;
    this.args = args;
    this.eventmethod = D;
}

(I'd also suggest renaming the eventmethod type and the D parameter to be more conventional.)

Jon Skeet
A: 

You should use constructor chaining:

public KeyEvent(eventmethod D) : this(null, D, null {
}

        public KeyEvent(Keys[] keys, eventmethod D) : this(keys, D, null) {
        }

        public KeyEvent(eventmethod D, object[] args) : this(null, D, args) {

        }

        public KeyEvent(Keys[] keys, eventmethod D, object[] args) {
            this.args = args;
            this.keys = keys;
            this.em = D;
        }
Frederik Gheysels