c# 构造函数错误 "CS1061" "x does not contain a definition for y"

标签 c# class

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);
        }
    }

嘿,伙计们;这个类不会编译。我得到的错误是:

'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?)

无论如何,调用“this.KeyEvent(D)”或类似代码的三行都会抛出此错误。 我不明白该错误,因为它争辩说没有方法接受“KeyEvent”的第一个参数,但我并没有尝试使用 KeyEvent 调用方法...谢谢。

最佳答案

要从一个构造函数调用另一个构造函数,请使用以下语法:

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

就您的代码而言,它正在尝试调用名为 KeyEvent 的常规方法(非构造函数)。

关于c# 构造函数错误 "CS1061" "x does not contain a definition for y",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2012155/

相关文章:

c# - 一台电脑可以同时启动多少个Selenium驱动的自动化浏览器

c# - 当对 C# mvc Controller 的 ajax 调用不起作用时处理 session 超时

c# - "this is ClassName"语句在 C# 中意味着什么?

c# - Entity Framework : Migration classes missing from list of pending explicit migrations after moving them to the migrations folder

oop - 如何在 MATLAB 中获取对象(类 inst)中的方法句柄

c++ - 最烦人的解析 : why doesn't `g( ( f() ) );` call `f` 's default constructor and pass the result to `g` 's ctor that takes a `f` ?

java - 创建使用数组的自定义类

c# - Microsoft Bot Framework,LUIS,在消息没有意图时采取一些行动

java - 从类和 javadoc 生成代码 stub

C++,改变属于一个类的结构对象的值