Newbie help with else statements

Hi everyone. I'm having difficulty getting my else statement to work correctly. I was hoping someone might be able to find something wrong with it. Basically my goal is to give the user an error if the player has already been registered to the team, and a different message if they haven't yet picked a team to associate that player with. Is this sufficient enough code to provide? The problem I keep getting is that "That player has already registered" is being skipped over. It's automatically going to "Choose a team to associate that player with".

//When adding a new player to the team, check formatting, make sure that player //isn't already on a team, and that a team has been chosen. private void btnAddPlayer_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtboxPlayerName.Text)) { MessageBox.Show(@"Check the formatting on this entry."); } else { var player = new Player() { Name = txtboxPlayerName.Text, BirthDay = dtpBirthdate.Value }; if (comboxChooseTeam.SelectedItem != null) { var teamName = (string)comboxChooseTeam.SelectedItem; var team = MainForm.Teams.FirstOrDefault(t => t.Name == teamName); bool isNewPlayer = true; if (team != null) team.Players.ForEach(p => { if (p == player) { isNewPlayer = false; } }); if (isNewPlayer) { var firstOrDefault = MainForm.Teams.FirstOrDefault(t => t.Name == teamName); if (firstOrDefault != null) firstOrDefault.Players.Add(player); ClearForm(); } else { MessageBox.Show(@"That player has already registered!"); } } else { MessageBox.Show(@"Choose a team to associate this player with"); } } } 

by Servatose via /r/csharp

Leave a Reply