-1

This is my project. I want to call delete_created_page file code in program.cs file, But I have some errors.

Attached image my progect. https://plus.google.com/u/0/photos/photo/103928744837695537992/6652781093994177394

'DeleteCreatedPage.MainDeletePage()' is inaccessible due to its protection level

Help me please. I am beginner in C#, I am using monodevelop and usng Ubuntu 18.04 OS

This is the Delete_created_page.cs file

namespace DeletePage
{
    public class DeleteCreatedPage
    {
        static void MainDeletePage()
        {
            //Initialize TestName and ToolName. 
            string testName = "delete_page";
            string toolName = "editor";
            // Calling necessary classes
       }
    }
}

And this is the Program.cs file

namespace ProgramList
{
    public class MainClass
    {
        static void Main(string[] args)
        {
            DeletePage.DeleteCreatedPage del = new DeletePage.DeleteCreatedPage();
            del.MainDeletePage();
        }
    }
}
Narine Poghosyan
  • 853
  • 3
  • 16
  • 26
  • Add a public accessor to `static void MainDeletePage()` to make it `public static void MainDeletePage()` –  Feb 01 '19 at 02:06

4 Answers4

2

There's a couple of issues stacked together here.

The first is that the method is set to the most restrictive it can, if not explicitly defined, such as internal, or private. You want it to be public, so be sure to include that.

The second issue is that it is static, meaning it is not accessed through an instantiated object, rather it is called directly through the class, like so DeleteCreatedPage.MainDeletePage(). If you want the method to work with class members of the specific DeleteCreatedPage object, remove static. If you want the method to use the same values for every object, leave static on there.

So you have two options to fix this particular problem.

Either change the method definition to this

public void MainDeletePage()

or change how you call it to this (while still adding public to the method definition)

DeleteCreatedPage.MainDeletePage()
St. Pat
  • 749
  • 4
  • 12
  • you will still need to make the static method public with the second solution. – Spinnaker Jan 31 '19 at 22:14
  • this is true, I'll edit to make that clear – St. Pat Jan 31 '19 at 22:14
  • I changed, wtat you asked,, namespace DeletePage { public class DeleteCreatedPage { public void MainDeletePage() { and in program file. DeleteCreatedPage.MainDeletePage(); But I have a error yet.. see atached file. https://plus.google.com/u/0/photos/photo/103928744837695537992/6652790747407093154?authkey=COLJ4fOTtZCZEw – Narine Poghosyan Jan 31 '19 at 22:23
  • Now its working, when I writing this. public void MainDeletePage() and in program.cs file called it this. DeletePage.DeleteCreatedPage del = new DeleteCreatedPage(); del.MainDeletePage(); – Narine Poghosyan Jan 31 '19 at 22:34
0

If you are trying to call MainDeletePage, you must mark it as public so that other classes can see it. So, instead of static void MainDeletePage() try public static void MainDeletePage(). By default, C# defaults to private scope for a method if it does not explicitly define it's scope. Private methods can only be accessed by the class in which they are defined.

Wayne Allen
  • 1,605
  • 1
  • 10
  • 16
0

It's because MainPageDelete isn't public, you need to write it like this. The default accessibility modifier for class members is private, so it's not accessible outside of the class.

public static void MainDeletePage()
    {
        //Initialize TestName and ToolName. 
        string testName = "delete_page";
        string toolName = "editor";
        // Calling necessary classes
   }
Spinnaker
  • 326
  • 1
  • 12
0

Now its working, when I writing this in delete_created_page file.

public void MainDeletePage()

And in program.cs file called it this method.

DeletePage.DeleteCreatedPage del = new DeleteCreatedPage();    del.MainDeletePage(); 
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Narine Poghosyan
  • 853
  • 3
  • 16
  • 26