Hayden Meherg

Hayden Meherg

  • NA
  • 35
  • 6k

How to apply changes to the clone in C#

May 2 2019 4:43 PM
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
  1. public class Student  
  2.    {  
  3.        public List Scores = new List();  
  4.   
  5.        public string Name  
  6.        { getset; }  
  7.   
  8.        public bool AddScore(int score)  
  9.        {  
  10.            try  
  11.            {  
  12.                Scores.Add(score);  
  13.            }  
  14.            catch { return false; }  
  15.            return true;  
  16.        }  
  17.   
  18.        public List GetScores()  
  19.        {  
  20.            return Scores;  
  21.        }  
  22.   
  23.        public int GetScoreAt(int index)  
  24.        {  
  25.            return (int)Scores[index];  
  26.        }  
  27.   
  28.        public int GetScoreTotal()  
  29.        {  
  30.            int sum = 0;  
  31.            foreach (int score in Scores)  
  32.            {  
  33.                sum += score;  
  34.            }  
  35.            return sum;  
  36.        }  
  37.   
  38.        public int GetScoreCount()  
  39.        {  
  40.            return Scores.Count;  
  41.        }  
  42.   
  43.        public int GetScoreAverage()  
  44.        {  
  45.            return GetScoreTotal() / GetScoreCount();  
  46.        }  
  47.   
  48.        public void DestroyScores()  
  49.        {  
  50.            Scores = new List();  
  51.        }  
  52.    }  
frmUpdateScores.cs
  1. public partial class frmUpdateStudent : Form  
  2.     {  
  3.         private Form1 parentForm;  //main form  
  4.         private Student studentToEdit; //student list  
  5.         private int index; //index  
  6.   
  7.         public frmUpdateStudent(Form1 parentForm, int index)  //update parent form (Form1) with the new student and scores  
  8.         {  
  9.             this.parentForm = parentForm;  
  10.             this.index = index;  
  11.             studentToEdit = this.parentForm.GetStudent(index);  
  12.   
  13.             InitializeComponent();  
  14.   
  15.             StudentName.Text = studentToEdit.Name;  
  16.             UpdateScoreDisplay();  
  17.         }  
  18.   
  19.         public void AddScoreToStudent(int value) //add score to current student and display in the list  
  20.         {  
  21.             studentToEdit.AddScore(value);  
  22.             UpdateScoreDisplay();  
  23.         }  
  24.   
  25.         public void UpdateScoreAtIndex(int id, int value)  //update a score selected from the list  
  26.         {  
  27.             studentToEdit.GetScores()[id] = value;  
  28.             UpdateScoreDisplay();  
  29.         }  
  30.   
  31.         public int GetScoreAtIndex(int id)  //get the score index  
  32.         {  
  33.             return studentToEdit.GetScoreAt(id);  
  34.         }  
  35.   
  36.         private void UpdateScoreDisplay()  //update the score display list  
  37.         {  
  38.             CurrentScores.DataSource = null;  
  39.             CurrentScores.DataSource = studentToEdit.GetScores();  
  40.         }  
  41.   
  42.         private void AddScoreButton_Click(object sender, EventArgs e)  //open the add score form  
  43.         {  
  44.             frmAddScore addScoreForm = new frmAddScore(this);  
  45.             addScoreForm.Show();  
  46.         }  
  47.   
  48.         private void RemoveScoreButton_Click_1(object sender, EventArgs e) //remove a score from current index and update display list  
  49.         {  
  50.             studentToEdit.GetScores().RemoveAt(CurrentScores.SelectedIndex);  
  51.             UpdateScoreDisplay();  
  52.         }  
  53.   
  54.         private void ClearScoresButton_Click_1(object sender, EventArgs e) //clear all scores  
  55.         {  
  56.             studentToEdit.DestroyScores();  
  57.             UpdateScoreDisplay();  
  58.         }  
  59.   
  60.         private void CloseButton_Click_1(object sender, EventArgs e)  
  61.         {  
  62.             Close();  //close form  
  63.         }  
  64.   
  65.         private void UpdateButton_Click_1(object sender, EventArgs e)  //open update form for current student  
  66.         {  
  67.             Student Form1 = new Student();  
  68.             Form1.Name = StudentName.Text;  
  69.             parentForm.UpdateStudent(index, Form1);  
  70.             Close();  
  71.         }  
  72.   
  73.         private void UpdateScoresButton_Click(object sender, EventArgs e)  
  74.         {  
  75.             frmUpdateScore updateScoreForm = new frmUpdateScore(this, CurrentScores.SelectedIndex);  
  76.             updateScoreForm.Show();  
  77.         }  
  78.     }  

Answers (1)