0

I've added an array that's split to show name and a number and I've outputted those in their own respective individual areas but I want to output every entry in the array to 1 list that shows the inputted names and name (they're entered at the same time)

Most of the questions I've found on here are dealing with outputting the array into Console but I'm using a Windows Form Application so can I output them to a multi-line textbox?

public void AddPlayer(string input) 
{ 
   string[] addingPlayer = input.Split(); 
   playerNames[value] = addingPlayer[0]; 
   playerScores[value] = int.Parse(addingPlayer[1]); 
   value++; 
}
5
  • Can you show some code? Commented Mar 27, 2015 at 2:10
  • Are you looking for a part in specific? It's using 2 classes, one for GUI process and the other for business logic. All of that is working fine, I just need to output all of the elements in an array. Commented Mar 27, 2015 at 2:14
  • public void AddPlayer(string input) { string[] addingPlayer = input.Split(); playerNames[value] = addingPlayer[0]; playerScores[value] = int.Parse(addingPlayer[1]); value++; } Commented Mar 27, 2015 at 2:54
  • Grouped arrays are an anti-pattern. You should have a Player class with Name and Score properties. Commented Mar 27, 2015 at 2:59
  • I'm not sure that I follow? Commented Mar 27, 2015 at 3:02

1 Answer 1

4

Grouped arrays (when you have multiple arrays that are related by index) are an anti-pattern. Don't do that. Instead, you should have a Player class with Name and Score properties:

public class Player
{
    public string Name {get;set;}
    public int Score {get;set;}
}

Additionally, whenever you have an array that grows, as implied by the need for an AddPlayer() function, you should use a Collection type like List<Player> instead:

List<Player> players = new List<Player>();

Now your AddPlayer() method will look like this:

public void AddPlayer(string input) 
{ 
   string[] addingPlayer = input.Split(); 
   players.Add(new Player(){Name = addingPlayer[0], Score = int.Parse(addingPlayer[1])});
}

To write the contents of the players list to a mutli-line textbox, you could then do this:

StringBuilder sb = new StringBuilder();
foreach (Player p in players) 
{
    sb.AppendFormat("{0} - {1}\n", p.Name, p.Score);
}
MyTextBox.Text = sb.ToString();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.