0

我在编写我的chip8 Emu 时遇到了另一个问题。除了那些需要输入或涉及图形的指令之外,我已经设法让所有指令都能正常工作。

我正在尝试编写一个单独的类来简单地确定按钮是否被按下。我已经为此工作了一段时间,我认为我一定遗漏了一些非常简单的东西,或者我在概念上不理解某些东西。这是我正在写的课程,评论中有确切的错误

using System;
 using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
{
    bool pressed = false;
    public userIn()
    {

    }

    public bool checkPress() //Error    9   'WindowsFormsApplication1.userIn.checkPress()': not all code paths return a value   C

    {  //<---- it is teling me that a closing bracket is expected here?
         private void key5_Click(object sender, EventArgs e)
    {
             pressed = true;
    }

    private void keyb_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key0_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keya_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keye_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key9_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key8_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key7_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keyd_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keyf_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key6_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key4_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void keyc_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key3_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key2_Click(object sender, EventArgs e)
    {
        pressed = true;
    }

    private void key1_Click(object sender, EventArgs e)
    {
        pressed = true;
    }


        return pressed; //Error 6   Invalid token 'return' in class, struct, or interface member declaration    

    }
}

} // 错误 8 类型或命名空间定义,或预期文件结尾

最后一行不适合该框

有没有人有任何想法或建议?

一如既往地感谢任何帮助!

4

3 回答 3

4

}forcheckPress方法在课程结束时,您的正确方法:

public bool checkPress()
{ 
    return pressed; 
}

你的代码应该是这样的:

using System;
...

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress() // This method
        {
            return pressed;
        } // Must close here, not end of class

        .
        . 
        .

        private void key1_Click(object sender, EventArgs e)
        {
            pressed = true;
        }
    }
}
于 2012-10-15T04:31:01.453 回答
0

如果按钮属于类,XXX那么您可以通过创建只读属性来公开其状态。创建另一个依赖于XXX.

于 2012-10-15T04:33:05.157 回答
0

简短的回答:

您在另一个方法中打开了一个方法。


长答案

以下是您正确缩进的代码(我建议您使用 AStyle):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress() //Error    9   'WindowsFormsApplication1.userIn.checkPress()': not all code paths return a value   C
        {  //<---- it is teling me that a closing bracket is expected here?
            private void key5_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keyb_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key0_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keya_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keye_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key9_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key8_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key7_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keyd_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keyf_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key6_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key4_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void keyc_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key3_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key2_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            private void key1_Click(object sender, EventArgs e)
            {
                pressed = true;
            }
            return pressed; //Error 6   Invalid token 'return' in class, struct, or interface member declaration
        }
    }
} // Error 8 Type or namespace definition, or end-of-file expected

你还是看不到问题吗?好的,这是一个精简版:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress() //Error    9   'WindowsFormsApplication1.userIn.checkPress()': not all code paths return a value   C
        {  //<---- it is teling me that a closing bracket is expected here?
            private void key5_Click(object sender, EventArgs e)
            {
                pressed = true;
            }

            //And some other methods

            return pressed; //Error 6   Invalid token 'return' in class, struct, or interface member declaration
        }
    }
} // Error 8 Type or namespace definition, or end-of-file expected

你觉得合适吗?

好吧,这只是一种表达方式,这是编译器看到的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress() //Error    9   'WindowsFormsApplication1.userIn.checkPress()': not all code paths return a value   C
        {  //<---- it is teling me that a closing bracket is expected here?
            private void key5_Click(object sender, EventArgs e)
            {
                pressed = true;
        }

        //And some other methods

        return pressed; //Error 6   Invalid token 'return' in class, struct, or interface member declaration
    }
}
} // Error 8 Type or namespace definition, or end-of-file expected

现在有意义吗?

您可能认为上面的代码是正确的,如果是这样,这不是答案……而是一个问题:您认为您要做什么?

好的,是的,C# 支持 lambda,您是否尝试执行以下操作?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress()
        {

            key5.Click += (sender, e)  =>
            {
                pressed = true;
            };

            //And some other (anonymous) methods

            return pressed;
        }
    }
}

但我的直觉告诉你,你只是做了一些奇怪的复制粘贴,而预期的代码是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class userIn
    {
        bool pressed = false;
        public userIn()
        {

        }

        public bool checkPress()
        {               
            return pressed;
        }

        private void key5_Click(object sender, EventArgs e)
        {
            pressed = true;
        }

        //And some other methods
    }
}

有道理?我希望。

于 2012-10-16T23:56:47.693 回答