-3

It comes up with this error when I try to login.

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //string name = textBox1.Text;
        string.Format ("{0} {1}", "Best", "Regards");

        if (textBox1.Text == "Ryan" && textBox2.Text == "password")
        {
            MessageBox.Show(string.Format("Welcome {1}" ));
        }

    }
}
Mr.B
  • 15
  • 7

4 Answers4

8

string.Format("Welcome {1}" )

needs an argument

string.Format("Welcome {0}", textBox1.Text )

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • this stopped the error. but what is the point in have it if it doesnt showing up the text I told it {0} which is "best". Or have I misunderstood what this completely does? – Mr.B Apr 30 '13 at 13:14
  • ? please help as Im pretty confused now – Mr.B Apr 30 '13 at 13:18
  • I think you have misunderstood what the 0 does, the zero is a reference to an argument that is found after the comma, here is a pretty good example http://www.techotopia.com/index.php/Formatting_Strings_in_C_Sharp – Sayse Apr 30 '13 at 13:46
  • it is only "best" for your first `string format` – Sayse Apr 30 '13 at 13:53
5

The error is thrown in this line:

MessageBox.Show(string.Format("Welcome {1}" ));

because you have used the placeholder {1} but havn't provided an argument to the string.Format function. Beside that you have not started with the index 0.

You have to provide an argument and start with the index 0:

MessageBox.Show(string.Format("Welcome {0}", textBox1.Text));
Jan
  • 15,802
  • 5
  • 35
  • 59
3

You need to do the following:

string.Format("Welcome {0}", "some value here");
Ric
  • 12,855
  • 3
  • 30
  • 36
2
MessageBox.Show(string.Format("Welcome {0}", "some text"));
burning_LEGION
  • 13,246
  • 8
  • 40
  • 52