Reputation: 17
I have a piece of code were i declare default constructor and parameter constructor.
Student_APP Secondstudent = new Student_APP("2345");
Secondstudent.score1 = 30;
Secondstudent.score2 = 20;
Secondstudent.score3 = 10;
Console.WriteLine("Student Number: " + Secondstudent.studentNumber + "\nAverage: {0:F1}",
Secondstudent.CalculateAverage());
Student_APP Thirdstudent = new Student_APP("5432", "xyz", "zxy");
Thirdstudent.major = "Maths";
Thirdstudent.score1 = 97;
Thirdstudent.score2 = 56;
Thirdstudent.score3 = 76;
Console.WriteLine("\n\n Third student ");
Console.WriteLine(Thirdstudent);
Student_APP LastStudent = new Student_APP("2255", "yxx", "xyy",94,54,74,"CS");
//LastStudent.major = "CS";
//LastStudent.score1 = 94;
//LastStudent.score2 = 54;
//LastStudent.score3 = 74;
I have not used this keyword here, but i get compilation error like
Error 4 'StudentAppWithoutgetset.Student_APP' does not contain a constructor that takes 7 arguments C:\Users\hariharan.v\Desktop\Office Training\MyfirstProgCarpet\StudentApp\StudentAppWithoutgetset\StudentAppWithoutgetset\Program.cs 47 39 StudentAppWithoutgetset
Please help me.
Upvotes: 0
Views: 80
Reputation: 216293
Supposing that your Student_APP class is something like this
public class Student_APP
{
// Public properties with get/set and an automatic backing field
public int score1 {get; set;}
public int score2 {get; set;}
public int score3 {get; set;}
public string major {get; set;}
// Internal properties
private int _id;
private string _surname;
private string _name;
public Student()
{}
public Student(int idnumber)
{
_id = idnumber;
}
public Student(int idnumber, string surname, string name)
{
_id = idnumber;
_surname = surname;
_name = name;
}
}
Now you could declare and initialize an instance of this class using the Object and Collection Initializer syntax
Student_APP s = new Student_APP(2255, "xyz", "xyz") {score1 = 94, score2=54, score3=74, major = "CS"};
Of course, if your purpose is to eliminate the public properties of the Student_APP
class then you need to add an appropriace constructor that take 7 parameters as explained in other answers.
In my example above the Student_APP
class has three constructors but none of them has the 7 parameters signature required by your code and thus you get the mentioned error.
Upvotes: 0
Reputation: 51
If I understand you correctly, everytime you do a new, e.g.
Student_APP LastStudent = new Student_APP("2255", "yxx", "xyy",94,54,74,"CS");
You are instantiating an object of Student_APP
. The arguments you pass in the brackets ("2255", "yxx", "xyy",94,54,74,"CS")
and the error message seems to suggest that you need to a constructor for Student_APP
that takes in 7 parameters.
So in you class Student_APP
you need :
public Student_APP(string firstParam, string secondParam, string thirdParam, int fourthParam, int fifthParam, int sixthParam, string seventhParam)
{
_firstParam = firstParam;
_secondParam = secondParam;
//....
_seventhParam = seventhParam;
}
Can you also give us an example of what you mean by 'this' so we can address the question with more clarity please. I think that its the root of the misunderstanding.
Upvotes: 0
Reputation:
the issue not related to this keyword, but it is related to the constructor it self. in your class there are no constructor that take 7 arguments so you should write it in your class before using it in the object.
public Student_APP(string p1, string p2, string p3, int p4, int p5, int p6, string p7)
{
//...
}
Upvotes: 1
Reputation: 483
Do you have a constructor that takes 7 arguments? Try adding:
public Student_APP(string p1, string p2, string p3, int p4, int p5, int p6, string p7)
{
//...
}
Upvotes: 1