-1
  private void GetNewDeck_Click(object sender, EventArgs e)
    {
       string[] suit = { "C", "D", "H", "S" };
        string[] num = { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" };
        for (int j = 0; j < 4; j++)
        {
            for (int i = 0; i < 13; i++)
            {
               NewDecktextBox.Text + = (suit[j] , num[i]"\n");
            }
        }

    }

I was trying to display a new deck of cards in a multiline textbox(NewDecktextBox) when I click the button GetNewDeck_Click button.Iam having error with the NewDecktextbox.Text line..

The output should be

 C2 C3 C4 C5 C6 C7 C8 C9 C10 CJ CQ CK CA
 D2 D3 D4 D5 D6 D7 D8 D9 D10 DJ DQ DK DA
 H2 H3 H4 H5 H6 H7 H8 H9 H10 HJ HQ HK HA
 S2 S3 S4 S5 S6 S7 S8 S9 S10 SJ SQ SK SA

Thanks

user3648850
  • 19
  • 1
  • 2
  • 4
  • 1
    Is this meant to be C#?, if so NewDecktextBox.Text +=(suit[j] + num[i] + "\n") Oh and you need to look at teh StringBuilder class – Tony Hopkinson Jun 02 '14 at 23:18
  • 1
    @Tony: that entire line looks iffy (hints of [Cargo Cult programming](http://en.wikipedia.org/wiki/Cargo_cult_programming)?) but surely even with your fixed line, the `\n` is still not in a proper place? – Jongware Jun 02 '14 at 23:21
  • 1
    No it's not, but I'm a one problem at a time guy. :) – Tony Hopkinson Jun 02 '14 at 23:28
  • @Jongware: I know the line looks iffy...that's why I was asking for help.. I am trying to learn C# and iam a novice at programming...please don't make fun of me.. :( – user3648850 Jun 02 '14 at 23:36
  • @user3648850: well, better help us help *you* then. Add a tag for the language you are using (I suppose Tony guessed it anyway), the environment if relevant (as your "textbox" construct seems to indicate), and the exact error message you got. – Jongware Jun 02 '14 at 23:38
  • Only assignment call increment decrement await and new object expressions can be used as a statement --- Error Message for NewDecktextBox.Text... – user3648850 Jun 02 '14 at 23:42
  • That error basically means it can't make sense out of the right hand side += – Tony Hopkinson Jun 02 '14 at 23:54

1 Answers1

0

This will be a bit better

StringBuilder sb = new StringBuilder();
foreach (String suit in new string[] { "C", "D", "H", "S" })
{
  foreach (string value in new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A" } )
  {
    sb.Append(suit);
    sb.Append(value);
    sb.Append(" ");
  }
  sb.Append(Environment.NewLine);
}
NewDecktextBox.Text = sb.ToString()

Strings are immutable in .net. You way would build a new string for every card you added to the textbox, so basically 52 of them "C2 " then "C2 C3 " then ....

As soon as you start manipulating a string in a loop, StringBuilder is the way to go.

Another tip is to use foreach if you can, your way if you wanted to add another card e.g. Emperor (invented by me just now), you'd have to change the loop variables and your arrays.

Happy learning.

Environment.NewLine is the line end for the environent you are in, automatically copes with LF or CRLF.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • Thank you so much... I really helped me in understanding the concept. Also if I want to display a shuffled deal as the output, how should I proceed. I know concept wise we should use a random generator and swap the current card with the random generated card... but how to implement this? – user3648850 Jun 03 '14 at 00:56
  • You'll need to move your cards into some sort of collection, but that is a regular question see http://stackoverflow.com/questions/1150646/card-shuffling-in-c-sharp – Tony Hopkinson Jun 03 '14 at 08:45