Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to do some practice of OOP but I seem to not understand something. Here is my parents class:
C#
class Human
    {
        public string name;
        public int age;

        public Human(string name, int age)
        {
            this.name = name;
            this.age = age;
        }
        public string Name
        {
            get { return name; }
            set { this.name = value; }
        }
        public int Age
        {
            get { return age; }
            set { this.age = value; }
        }


And here is my child class :
C#
class Student:Human
    {
        private string speciality;
        private int facultynumber;

        public Student(string speciality, int facultynumber)
            : base(name,age)
        {
            this.facultynumber = facultynumber;
            this.speciality = speciality;
        }

        public string Speciality
        {
            get { return speciality; }
            set { this.speciality = value;}
        }
        public int Facultynumber
        {
            get { return facultynumber; }
            set { this.facultynumber = value; }
        }


Why is it firing me an Error, on the base : object reference is required to access non-static member

What I have tried:

I really don't know what to do...
Posted
Updated 4-Aug-16 0:16am

You should be passing all the parameters to the Student:

public Student(string name, int age, string speciality, int facultynumber)
            : base(name,age)
        {
            this.facultynumber = facultynumber;
            this.speciality = speciality;
        }


Also, consider making the name/age fields in the human class protected or private.
 
Share this answer
 
v2
Hello ,
you cannot do this . It can be
C#
public Student(string name, int age, string speciality, int facultynumber)
            : base("SomeName",10)

or
it can be
C#
public Student(string speciality, int facultynumber)
              : base(speciality, facultynumber)

Thanks
 
Share this answer
 
this is the problematic code

C#
public Student(string speciality, int facultynumber)
    : base(name,age)
{
    this.facultynumber = facultynumber;
    this.speciality = speciality;
}



because you are trying to pass parent class variables as parameter in the constructor of parent class.

Try using, test hard coded values than it should work (for e.g "test" ,1)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900