Hey, I'm attempting to clone an object. The Update Student Scores form should create a clone of the current student object and then apply changes to the clone. That way the changes will be saved to the current student only if the user clicks the OK button. To create a clone, the Student class will need to implement the ICloneable interface, and the Clone method will need to implement a deep copy. So far, I've learned that you want a deep copy of something when you need a copy of the other objects contained by the original (rather than pointers to the location of those objects in the original), so that when you make changes to these copied properties, you don't affect the original object. What I'm struggling with though is applying it to my model. Below is the student class I'm trying to clone and the update student form. I've also attached the source code if that helps. Thanks!
Student.cs
- public class Student
- {
- public List Scores = new List();
-
- public string Name
- { get; set; }
-
- public bool AddScore(int score)
- {
- try
- {
- Scores.Add(score);
- }
- catch { return false; }
- return true;
- }
-
- public List GetScores()
- {
- return Scores;
- }
-
- public int GetScoreAt(int index)
- {
- return (int)Scores[index];
- }
-
- public int GetScoreTotal()
- {
- int sum = 0;
- foreach (int score in Scores)
- {
- sum += score;
- }
- return sum;
- }
-
- public int GetScoreCount()
- {
- return Scores.Count;
- }
-
- public int GetScoreAverage()
- {
- return GetScoreTotal() / GetScoreCount();
- }
-
- public void DestroyScores()
- {
- Scores = new List();
- }
- }
frmUpdateScores.cs
- public partial class frmUpdateStudent : Form
- {
- private Form1 parentForm;
- private Student studentToEdit;
- private int index;
-
- public frmUpdateStudent(Form1 parentForm, int index)
- {
- this.parentForm = parentForm;
- this.index = index;
- studentToEdit = this.parentForm.GetStudent(index);
-
- InitializeComponent();
-
- StudentName.Text = studentToEdit.Name;
- UpdateScoreDisplay();
- }
-
- public void AddScoreToStudent(int value)
- {
- studentToEdit.AddScore(value);
- UpdateScoreDisplay();
- }
-
- public void UpdateScoreAtIndex(int id, int value)
- {
- studentToEdit.GetScores()[id] = value;
- UpdateScoreDisplay();
- }
-
- public int GetScoreAtIndex(int id)
- {
- return studentToEdit.GetScoreAt(id);
- }
-
- private void UpdateScoreDisplay()
- {
- CurrentScores.DataSource = null;
- CurrentScores.DataSource = studentToEdit.GetScores();
- }
-
- private void AddScoreButton_Click(object sender, EventArgs e)
- {
- frmAddScore addScoreForm = new frmAddScore(this);
- addScoreForm.Show();
- }
-
- private void RemoveScoreButton_Click_1(object sender, EventArgs e)
- {
- studentToEdit.GetScores().RemoveAt(CurrentScores.SelectedIndex);
- UpdateScoreDisplay();
- }
-
- private void ClearScoresButton_Click_1(object sender, EventArgs e)
- {
- studentToEdit.DestroyScores();
- UpdateScoreDisplay();
- }
-
- private void CloseButton_Click_1(object sender, EventArgs e)
- {
- Close();
- }
-
- private void UpdateButton_Click_1(object sender, EventArgs e)
- {
- Student Form1 = new Student();
- Form1.Name = StudentName.Text;
- parentForm.UpdateStudent(index, Form1);
- Close();
- }
-
- private void UpdateScoresButton_Click(object sender, EventArgs e)
- {
- frmUpdateScore updateScoreForm = new frmUpdateScore(this, CurrentScores.SelectedIndex);
- updateScoreForm.Show();
- }
- }