【问题标题】:C# - Zero Based String error [duplicate]C# - 基于零的字符串错误 [重复]
【发布时间】:2013-04-24 09:23:38
【问题描述】:

当我尝试登录时出现此错误。

索引(从零开始)必须大于或等于零且小于参数列表的大小。

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}" ));
        }

    }
}

【问题讨论】:

  • 对不起,你是对的。我自己标记了它。

标签: c# string zero


【解决方案1】:

string.Format("Welcome {1}" )

需要参数

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

【讨论】:

  • 这停止了错误。但是,如果它没有显示我告诉它 {0} 这是“最佳”的文字,那么它有什么意义。还是我误解了这完全是做什么的?
  • ?请帮忙,因为我现在很困惑
  • 我想你误解了 0 的作用,零是对逗号后面的参数的引用,这是一个很好的例子 techotopia.com/index.php/Formatting_Strings_in_C_Sharp
  • 这只是您的第一个string format“最佳”
【解决方案2】:

在这一行抛出了错误:

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

因为您使用了占位符{1},但没有为string.Format 函数提供参数。除此之外,您还没有从索引 0 开始。

您必须提供一个参数并从索引 0 开始:

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

【讨论】:

    【解决方案3】:

    您需要执行以下操作:

    string.Format("Welcome {0}", "some value here");
    

    【讨论】:

      【解决方案4】:
      MessageBox.Show(string.Format("Welcome {0}", "some text"));
      

      【讨论】:

        猜你喜欢
        • 2019-09-19
        • 1970-01-01
        • 2019-03-26
        • 1970-01-01
        • 1970-01-01
        • 2019-09-06
        • 2017-02-15
        • 1970-01-01
        • 2010-09-24
        相关资源
        最近更新 更多