Home > front end >  Inaccessible due to its protection level?
Inaccessible due to its protection level?

Time:12-29

I started learning C#, and I was trying to create a class, I saw a video, and I did the exact same thing.

But I don't understand why I'm getting this error "Student.Student(string, int, double)' is inaccessible due to its protection level."

        static void Main(string[] args)
        {

            Student Student1 =  new Student("Aya", 00001, 4.0);
            Console.WriteLine(Student1.getName());

            Student1.setGPA(3.5);
            Console.WriteLine(Student1.getGPA());

        }
    }
    public class Student
    {
        private String Name;
        private int StudentId;
        private double GPA;
        private Student(String Name, int StudentId, double GPA)
        {
            this.Name = Name;
            this.StudentId = StudentId;
            this.GPA = GPA;
        }
        public String getName()
        {
            return this.Name;
        }
        public int getStudentId()
        {
            return this.StudentId;
        }
        public double getGPA()
        {
            return this.GPA;
        }
        public void setGPA(double GPA)
        {
            this.GPA = GPA;
        }

    }
}

CodePudding user response:

Your constructor is private, so it cannot be accessed outside the class. Another thing is that you can use properties, for example you can replace:

private String Name;
public String getName()
{
    return this.Name;
}

with this:

public string Name {get; set; }

CodePudding user response:

constructor have to be public to be able to access it

https://dotnetfiddle.net/S3aW62

  • Related