Error in calling base constructor C #

class Student
{
    int id;
    string name;
    public Student(int id, string name)
    {
        this.id = id;
        this.name = name;
    }
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}
class SubStudent : Student
{
    int ssn;
    public SubStudent(int id, int name, int ssn)
        : base(int id, string name)
    {

    }
}

The above code generates an "invalid expression for term int" error. What could be wrong?

+3
source share
5 answers

You do not need to repeat the type names in the call to the base. It should be: -

class SubStudent : Student
{    
    int ssn;    
    public SubStudent(int id, string name, int ssn)
        : base(id, name)
    {
    }
}
+2
source
public SubStudent(int id, string name, int ssn)
    : base(int id, string name)

it should be

public SubStudent(int id, string name, int ssn)
    : base(id, name)

You do not yet declare the signature of the base constructor, you just call it. And, like in any other call, parameter types are not indicated on the call site.

Edit: adjusted int nameto string namein the SubStudentctor parameter list.

+11
source

, , .

, , . , , :

base(id, name)

, . , :

public SubStudent(int id, int name, int ssn) : base(10, "fixed name")

, :)

+2

You should not put types in a database constructor call:

public SubStudent(int id, int name, int ssn)
        : base(id, name)
    {

    }
+1
source
class SubStudent : Student 
{     
    int ssn;     
    public SubStudent(int id, int name, int ssn)         
       : base(id, name)     
    {      
    } 
} 

Try the code above.

0
source

All Articles