1

I've placed a static class TestClass in the App_Code folder. The class contains a static method TestMethod. From Default.aspx.cs via Button_Click method, I'm trying to invoke TestMethod. - test = TestClass.TestMethod(). This gives error: 'TestClass' is inaccessible due to its protection level.

It feels like the static class and the _Default class should be placed into a common namespace, but this would "exclude" the Default.aspx controls references.

What am I doing wrong?

1

5 Answers 5

4

You need to declare TestClass as public:

public static class TestClass
{
    public static SomeType TestMethod()
    {
    }
}

The default visibility is internal for types and private for members of a type definition. The App_Code folder gets compiled into its own assembly, different from the assembly that is created when compiling the code behinds. internal types cannot be shared between assemblies (that's not 100% true but true in this case), hence why you are having this problem.

Sign up to request clarification or add additional context in comments.

Comments

1

Try to make it public static.

public class TestClass{
   public static TestMethod(){}

}

Comments

1

Make sure TestClass is public:

public class TestClass
{
}

The default when adding a class to Visual Studio is to omit the "public".

Comments

1

Make sure you have at least 'internal' scope on your method prototype.

Comments

0

Just because your class is static doesn't mean that it is accessible to all code/assemblies. Make sure that your static class has an access modifier (i.e. public) that is accessible by your web form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.