Results 1 to 3 of 3

Thread: C#- Help with code

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    C#- Help with code

    Hello, I just need some quick help here:

    Code:
    this.Controls[string.Format("S" + S)].Show();
    Code:
    this.Controls[string.Format("S" + S)].Visible = true;
    None of these works. I have controls called S0-S10(LineShapes). The S variable is currently 0. I get a NullReferenceException error during runtime.

    Note: tried it with a button aswell.

    Edit: It's for a hangman game, S0-S10 are lines and a sphere.

    Thanks, I hope it's just something easy.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: C#- Help with code

    What exactly is the string.Format call for? There's no formatting going on. If you were to use string.Format it would look like this:
    Code:
    this.Controls[string.Format("S{0}", S)].Show();
    but that's pointless with so simple a string, so just do this:
    Code:
    this.Controls["S" + S].Show();

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: C#- Help with code

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width