1迭代后的循环停止

时间:2017-02-23 21:02:40

标签: vb.net visual-studio

对于我的任务的这部分,我需要做以下事情:

  

创建一个函数,该函数返回字符串类型键和整数类型值的排序列表。在函数内部,声明一个新的排序列表,其结构与函数的返回类型相同。然后,使用嵌套for循环填充已排序列表,浏览产品列表和年份列表,以便将产品和年份连接在一起以创建密钥,并从请求表数组中检索相应的需求值作为值将此密钥输入到排序列表中。

我已经编写了以下代码来执行此操作,但它会抛出"' System.ArgumentException'在System.dll"经过1次迭代。测试MessageBox只显示" ModelX 2013 205"然后会抛出错误。

    Public Function GetProductDemand() As SortedList(Of String, Integer)
    Dim prodDemand As New SortedList(Of String, Integer)

    Dim count As Integer = 0
    For Each product In products
        For Each yr In years
            Dim key As String
            key = product & " " & yr
            For i As Integer = 0 To 4
                prodDemand.Add(key, demand(count, i)) 'stops here after 1 iteration
                MessageBox.Show(key & " " & prodDemand(key)) 'test to display results
            Next
        Next
        count += 1
    Next
    Return prodDemand
End Function

出现这种情况的原因是什么?以下是相关代码的其余部分:

    Dim products As List(Of String)
Dim years As List(Of String)
Dim demandList As SortedList(Of String, Integer)
Dim demand As Integer(,)

Private Sub frmDemand_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    products = GetProducts()
    years = GetYears()
    demand = GetDemand()
    demandList = GetProductDemand()
End Sub

Private Function GetDemand() As Integer(,)
    Return {
            {205, 220, 245, 230},
            {160, 174, 152, 144},
            {480, 424, 396, 456}
           }
End Function

Public Function GetProducts() As List(Of String)
    Dim prods As New List(Of String)
    prods.Add("ModelX")
    prods.Add("ModelY")
    prods.Add("ModelZ")
    Return prods
End Function

Public Function GetYears() As List(Of String)
    Dim yr As New List(Of String)
    yr.Add("2013")
    yr.Add("2014")
    yr.Add("2015")
    yr.Add("2016")
    Return yr
End Function

0 个答案:

没有答案