在本主题内连接: 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/