Member is inaccessible due to its protection level error
Asked Answered
V

6

7

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?

Vlaminck answered 28/2, 2013 at 7:52 Comment(0)
H
16

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.

Helmet answered 28/2, 2013 at 7:53 Comment(0)
W
6

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.

Waggoner answered 28/2, 2013 at 7:58 Comment(0)
O
1

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

Octuple answered 28/2, 2013 at 7:56 Comment(1)
the difference between this and other answers it that you cannot set the value outside of your class unless you define a set methodOctuple
F
0

Try to make the string-array public:

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

}
Fleeman answered 28/2, 2013 at 7:53 Comment(0)
V
0

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

Valadez answered 13/11, 2018 at 6:48 Comment(1)
The class already has a public keyword. The arguments are missing this keword, not the class.Filial
D
0

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;
}
Dormitory answered 10/4, 2021 at 3:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.