【问题标题】:Index must be greater than or equal to zero and less than the size of the argument list索引必须大于或等于零且小于参数列表的大小
【发布时间】:2013-05-27 13:27:15
【问题描述】:

我收到错误“索引(从零开始)必须大于或等于零且小于参数列表的大小。”不知道为什么。

我的代码如下:

'read directory and look for filenames that match pattern and have code elements from xml file

    Dim regElemName As New Regex("^code")
    Dim root = XElement.Load(xmlfile)
    Dim codeElements = root.Element("ApplicationSettings").Elements().Where(Function(xe) regElemName.IsMatch(xe.Name.LocalName)).Select(Function(xe) xe.Value)
    Dim codes = String.Join("|", codeElements.ToArray())
    Dim regFileName As New Regex(String.Format("^\d{5}\-(?<Year>(?:\d{4}))(?<Month>0?[1-9]|12|11|10)(?<Day>[12]\d|0?[1-9]|3[01])\-$", codes))
    Dim files = IO.Directory.GetFiles(TextBox1.Text, "*.pdf", IO.SearchOption.TopDirectoryOnly).Where(Function(path) regFileName.IsMatch(IO.Path.GetFileName(path)))

    For Each file As String In files
        Console.WriteLine(file)
    Next

有什么想法吗?

【问题讨论】:

  • 能否提供异常的堆栈跟踪?

标签: vb.net


【解决方案1】:

String.Format("^\d{5}\-(?&lt;Year&gt;(?:\d{4}))(?&lt;Month&gt;0?[1-9]|12|11|10)(?&lt;Day&gt;[12]\d|0?[1-9]|3[01])\-$", codes)

您在格式字符串中引用了{5}{4},但您只提供了一个参数codes。 4 和 5 将分别引用传递给 string.format 的第 5 个和第 6 个参数。

您收到此错误是因为您的格式字符串要求您传递至少 6 个参数(基于您在格式字符串中引用的最大索引)。

【讨论】:

    猜你喜欢
    • 2011-04-18
    • 2016-07-03
    • 2023-04-11
    • 2016-04-01
    • 2018-07-26
    • 2011-12-19
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多