Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 将一个值更改为数组中的下一个值_Vb.net - Fatal编程技术网

Vb.net 将一个值更改为数组中的下一个值

Vb.net 将一个值更改为数组中的下一个值,vb.net,Vb.net,我正在开发一个例行程序来计算每月的期货合约 如果我有一个表示月数的整数数组,即“1,4,7,10,12” 我有一个变量整数,是2 如果变量本身不在数组中,如何根据数组测试变量,并将变量更改为数组中下一个可用的最高值?即在这种情况下,变量的值2将变为4 我试过各种方法,但现在被卡住了 If datenum >= (targetdayofmonth + adjdays) Then currentmonth = currentmonth + 1 Dim currm

我正在开发一个例行程序来计算每月的期货合约 如果我有一个表示月数的整数数组,即“1,4,7,10,12” 我有一个变量整数,是2

如果变量本身不在数组中,如何根据数组测试变量,并将变量更改为数组中下一个可用的最高值?即在这种情况下,变量的值2将变为4

我试过各种方法,但现在被卡住了

If datenum >= (targetdayofmonth + adjdays) Then
        currentmonth = currentmonth + 1
        Dim currmonthname As String = MonthName(currentmonth, True)
        For x As Integer = 0 To contractmonths.Count - 1
            If GetMonthNumberfromShortMonthName(contractmonths(x)) = currentmonth Then
                currmonthname = currmonthname
            Else

            End If
        Next
    Else
        Dim currmonthname As String = MonthName(currentmonth, True)
    End If
因此,根据Tim的评论,我将代码更新为

 Dim contractmonthNos As New List(Of Int32)
    For Each childnode As XmlNode In From childnode1 As XmlNode In root Where childnode1.SelectSingleNode("futures/symbol/Code").InnerText = commodcode
        'get the available contract months for this contract
        Dim contractmonthnodes As XmlNode = childnode.SelectSingleNode("ContractMonths")
        contractmonthNos.AddRange(From subnode As XmlNode In contractmonthnodes Select GetMonthNumberfromShortMonthName(subnode.Name))
    Next
If datenum >= (targetdayofmonth + adjdays) Then
        currentmonth = currentmonth + 1
        Dim currmonthname As String = MonthName(currentmonth, True)
    Else
        Dim nextmonth = From month As Integer In contractmonthNos Where month >   currentmonth
        If nextmonth.Any() Then
            currentmonth = nextmonth.First()
        End If
        Dim currmonthname As String = MonthName(currentmonth, True)
    End If

但是在下个月的If-Then-Else中,我得到了一个VS2012的歪歪扭扭的警告“可能多次枚举IEnumerable”

我想这就是你想要的:

Dim intVar = 2
Dim months = { 1,4,7,10,12 }
Dim higherMonths = months.Where(Function(month) month > intVar).ToArray()
If higherMonths.Any() Then
    intVar = higherMonths.First()
End If
如果您不希望阵列中的下一个可用月份,但最近的月份,则必须在之前进行排序:

Dim higherMonths = months.Where(Function(m) m> intVar).
                          OrderBy(Function(m) m).
                          ToArray()
If higherMonths.Any() Then
    intVar = higherMonths.First()
End If
差不多

Module Module1

    Sub Main()
        ' N.B. this needs to the array to be sorted.
        Dim a() As Integer = {1, 4, 7, 10, 12}
        Dim toFind As Integer = 2
        Dim foundAt As Integer = -1
        For i = 0 To a.Length() - 1
            If a(i) >= toFind Then
                foundAt = i
                Exit For
            End If
        Next

        If foundAt >= 0 Then
            Console.WriteLine(String.Format("Looked for {0}, found {1}.", toFind, a(foundAt)))
        Else
            Console.WriteLine(String.Format("Did not find {0} or higher.", toFind))
        End If

        Console.ReadLine()

    End Sub

End Module

或者您可能想看看如何使用。

如何从for-each循环创建这个Dim months={1,4,7,10,12}?我从xml文件中获取整数数组,并将它们作为字符串获取,然后将它们转换为整数数组,如Dim contractMonters()as integer=GetMonthNumberfromShortMonthName(subnode.Name)(“subnode.Name是短月名”),然后我将使用
列表(Int312)
而不是数组。如果坚持使用数组,可以使用
list.ToArray()
在结尾。@dinotom:编辑了我的答案。用
ToList
ToArray
实现查询。我现在已经使用了方法语法,如果你想像以前一样使用Linq的查询语法,在添加
ToArray
之前,你需要用parantism包围查询。我已经按照你的建议进行了修复,非常有效,谢谢你,我同意你是谁