我正在WPF中申请,这是一个运行比赛的“滑雪胜地”。我已经得到它所以添加成员,这些被添加到字典中。我现在需要它,所以当用户使用ID号搜索成员时,他们还可以更新名称,地址和分数等信息。到目前为止,它正在更新存储为字符串的名称和地址,但它不会更新分数。以下是Skier构造函数的代码:
public Skier(string inNumber, string inName, string inAddress, int inScore)
{
name = inName;
number = inNumber;
address = inAddress;
score = inScore;
}
并且有一种方法可以在同一个班级中设置分数:
private List<String> ScoreList = new List<string>(); //make a list for the scores
public bool SetScore(int skiScore)
{
ScoreList.Add(name + "'s score is " + skiScore.ToString()); //add to the list
skiScore = score; //set the score
return true;
}
在另一个类中,是将成员添加到字典的方法:
public Skier AddSkier (string inName, string inAddress, int inScore)
{
string newNumberString = newNumber.ToString();
Skier result = new Skier(newNumberString, inName, inAddress, inScore);
newNumber = newNumber + 1;
Skier S = new Skier(newNumberString, inName, inAddress, inScore);
Skiers.Add(newNumberString, S);
return result;
}
这里的主要部分是应该更新分数的代码:
Skier activeSkier = new Skier(null, null, null, 0);
SkiLodge newSkiLodge = new SkiLodge("SkiLodge");
public void UpdateAccountScoreSkier (int newSkierScore) //update the score
{
if (!activeSkier.SetScore(newSkierScore)) //if cannot update
{
MessageBox.Show("Invalid score"); //tell the user
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
string updatedName = NameTextBox.Text; //get the new name
string updatedAddress = AddressTextBox.Text; //get the new address
string updatedScoreText = ScoreTextBox.Text; //get the new score
int updatedScore = int.Parse(updatedScoreText); //convert to integer
UpdateAccountNameSkier(updatedName); //call the update name method
UpdateAccountAddressSkier(updatedAddress); //call the update address method
UpdateAccountScoreSkier(updatedScore); //call the update score method
}