简体   繁体   中英

…is inaccessible due to its protection level c#/asp.net

I have an application with a separate class, that i'm instantiating in the code behind-file (in Page_Load). In the class there's some methods that I want to be able to call from the code behind-file, but by some reason it doesn't work (SecretNumber.MakeGuess(int) is inaccessible due to it's protection level). The class as well as the methods are public so what can be the reason?

// Default.asx.cs

...

protected void btnCheckNr_Click(object sender, EventArgs e)
{
    if (!Page.IsValid){
        return;
    }

    else{
        var guessedNr = int.Parse(inputBox.Text);
        var result = SecretNumber.MakeGuess(guessedNr); <- inaccessible due to...
    }
}

// SecretNumber.cs

public class SecretNumber {

    enum Outcome {
        Indefinite,
        Low,
        High,
        Correct,
        NoMoreGuesses,
        PreviousGuess
    };

    // Other code goes here...

    public Outcome MakeGuess(int guess) {
        // Other code here
    }
}

It is because your Outcome enumeration is private. Also in the code you have, MakeGuess needs to be marked as static to be used the way you have it written.

You're calling the method as though it is static , yet it is not a static method. How did your code even compile??

To clarify, that is not the cause of accessibility issue and others have posted the solution for that. But the aforementioned way you call the method is also a glaring problem (unless you just put a typo by not showing that is in fact static when posting your code).

结果需要公开,我不知道你是否已经初始化了类,在这种情况下,它需要是静态的还是声明的。

您必须public Static Class SecretNumber而不是public Class SecretNumber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM