Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

Use this code instead:

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

    }

The protection level for variable is private by default. A good practice is using property for members in class.

You should make a property for Keywords as follows

public String[] Keywords
{
get{return keywords;}
}

its a protection violation as variables are private by default

Try to make the string-array public:

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

}

You need to define the your class with "public" keyword

Please ensure the constructor is added as with public access specifier. this could be a reason for this kind of error. Example :

Customer() {
this.CustomerId = ++counter;
}
Change it to : 
public Customer() {
this.CustomerId = ++counter;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top