【问题标题】:Function is inaccessible due to its protection level in C#由于 C# 中的保护级别,函数无法访问
【发布时间】:2016-11-07 10:01:21
【问题描述】:

我正在开发一个 Windows 窗体应用程序,我有一个 TextBox 并且我有一个 textBox1_KeyDown 事件触发一个函数,它在另一个类中,在调用它的 OBJECT 后调用,所有设置都是默认的.但是我收到以下错误...

错误 CS0122 'ClassName.FunctionName(object, KeyEventArgs)' 是 由于其保护级别而无法访问

现在我的主窗体代码如下...

namespace NewDEMOApps
{
    public partial class MainForm : Form
    {
        ClassName newObj = new ClassName();

        public MainForm()
        {
            InitializeComponent();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            newObj.FunctionName(sender, e);
        }       
    }
}

我的课程代码如下所示...

namespace NewDEMOAppsClass
{
    public class ClassName
    {
        private void FunctionName(object sender, KeyEventArgs e)
        {
            if (true)
            {
                if (e.KeyCode.Equals(Keys.Up))
                {
                    MessageBox.Show(UP Key Pressed);
                }
                if (e.KeyCode.Equals(Keys.Down))
                {
                    MessageBox.Show(DOWN Key Pressed);
                }
                if (e.KeyCode.Equals(Keys.Enter))
                {
                    MessageBox.Show(Enter Key Pressed);
                }
                e.Handled = true;
            }
        }
    }
}

现在我想通过实用而不是通过编辑/更改 Visual Studio 等中的 GUI 设置来解决这个问题。那么,我可以做些什么来解决这个问题吗?

【问题讨论】:

标签: c# .net keyevent access-modifiers protection


【解决方案1】:

更改以下内容:

private void FunctionName(object sender, KeyEventArgs e)

public void FunctionName(object sender, KeyEventArgs e)

您通常无法从其类外部访问private 方法。 Read here for further information

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2015-09-26
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多