TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
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;
//main form
private
Student studentToEdit;
//student list
private
int
index;
//index
public
frmUpdateStudent(Form1 parentForm,
int
index)
//update parent form (Form1) with the new student and scores
{
this
.parentForm = parentForm;
this
.index = index;
studentToEdit =
this
.parentForm.GetStudent(index);
InitializeComponent();
StudentName.Text = studentToEdit.Name;
UpdateScoreDisplay();
}
public
void
AddScoreToStudent(
int
value)
//add score to current student and display in the list
{
studentToEdit.AddScore(value);
UpdateScoreDisplay();
}
public
void
UpdateScoreAtIndex(
int
id,
int
value)
//update a score selected from the list
{
studentToEdit.GetScores()[id] = value;
UpdateScoreDisplay();
}
public
int
GetScoreAtIndex(
int
id)
//get the score index
{
return
studentToEdit.GetScoreAt(id);
}
private
void
UpdateScoreDisplay()
//update the score display list
{
CurrentScores.DataSource =
null
;
CurrentScores.DataSource = studentToEdit.GetScores();
}
private
void
AddScoreButton_Click(
object
sender, EventArgs e)
//open the add score form
{
frmAddScore addScoreForm =
new
frmAddScore(
this
);
addScoreForm.Show();
}
private
void
RemoveScoreButton_Click_1(
object
sender, EventArgs e)
//remove a score from current index and update display list
{
studentToEdit.GetScores().RemoveAt(CurrentScores.SelectedIndex);
UpdateScoreDisplay();
}
private
void
ClearScoresButton_Click_1(
object
sender, EventArgs e)
//clear all scores
{
studentToEdit.DestroyScores();
UpdateScoreDisplay();
}
private
void
CloseButton_Click_1(
object
sender, EventArgs e)
{
Close();
//close form
}
private
void
UpdateButton_Click_1(
object
sender, EventArgs e)
//open update form for current student
{
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();
}
}
Attachment:
MaintainStudentScores.zip
Reply
Answers (
1
)
automatically open a combobox when value select from another
How to create and write into while install or unistall setup