Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. How to implement properties where score should be read only? namespace Assign

ID: 3622091 • Letter: 1

Question

1. How to implement properties where score should be read only?

namespace Assignment2
{
public class Quiz : List<Question>
{
public double PointsPerQuestion = 0.0;
// Implement properties--Score should be read only
public double Score = 0.0;
public bool AdministerQuiz()
{
Score = 0.0; // Must be modified
foreach (Question q in this)
{
MultipleChoice dlg = new MultipleChoice(q);
if (dlg.ShowDialog() == DialogResult.OK)
{
if (dlg.Correct) Score = Score + PointsPerQuestion; // Must be modified
}
else return false;
}
return true;
}
}
}

Explanation / Answer

To implement a getter and setter for the Score member in your data model you do this: 1) Make the member private: private double score; 2) Create a property for it: public double Score { get { return this.score; } set { this.score = value; } } 3) Now whenever you want to access your score data member you refer to it via the getter and setter you created which is named as Score (with a capital S). You can create any post processes whenever setting or getting values, you just write your code before the "return this.score" or the "this.score = value". Value is an arbitrary place holder that represents data that is to be stored on your member. Whenever you call the this.Score property outside of your class it calls the appropriate method for it, for example you call it to read data, it calls the getter method but if you call it to store data it calls the setter method. Please post here if you have more questions, if not please give karma where it is due :)