Member is inaccessible due to its protection level error

user2118160 picture user2118160 · Feb 28, 2013 · Viewed 50.5k times · Source

connected within this topic: How to connect string from my class to form

im trying to do solutions related to their answers (specifically answer of sir Jeremy) but this error keeps on appearing

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

code for KeyWords.cs:

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

    }
}

code for 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;
    }
}

what should i do?

Answer

Habib picture Habib · Feb 28, 2013

You need to define keywords as public

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

Currently it is private and that is why not accessible outside the class.

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.