c# - 由于保护级别错误,成员无法访问

标签 c# .net csharpcodeprovider

在本主题内连接: How to connect string from my class to form

我正在尝试做与他们的答案相关的解决方案(特别是 Jeremy 爵士的答案),但这个错误不断出现

'KeyWord.KeyWord.keywords' is inaccessible due to its protection level

KeyWords.cs 代码:

namespace KeyWord
{
    public class KeyWord
    {
        String[] keywords = { "abstract", "as", "etc." };

    }
}

main.cs 的代码

// Check whether the token is a keyword. 
var keyboardCls = new KeyWord.KeyWord();
String[] keywords = keyboardCls.keywords;

for (int i = 0; i < keywords.Length; i++)
{
    if (keywords[i] == token)
    {
        // Apply alternative color and font to highlight keyword.        
        rtb.SelectionColor = Color.Blue;
        rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
        break;
    }
}

我该怎么办?

最佳答案

你需要定义keywords为public

public String[] keywords = { "abstract", "as", "etc." };

目前它是private,这就是为什么不能在类外访问的原因。

Access Modifiers (C# Programming Guide)

Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default.

关于c# - 由于保护级别错误,成员无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130231/

相关文章:

c# - 如何使用 ClickOnce 部署基于 C#(4.0)Visual studio 2010 windows 窗体的应用程序?

c# - 如何通过 C# 检索所有可能的服务器名称

c# - 在运行时创建属性并传递值

c# - 我如何创建一种方法来检查游戏对象是否包含特定组件?

C#/WPF,如何使窗口(使用 Window.ShowDialog() 创建)在单击其父窗口时标题栏闪烁(就像 MessageBox 一样)?

c# - 域对象和服务类对象应该放在哪里?

c# - 检查复选框 MVVM 中是否未选择任何项目

.net - Rhino 模拟 : How to mock ADO. NET 的 DataRow?

C# csharpcompiler 编译器程序集无法导入任何内容

c# - 使用 CSharpCodeProvider 进行缓慢的动态编译