VB.NET 日期数组处理问题

发布于 2024-12-10 17:52:24 字数 1031 浏览 0 评论 0原文

我在尝试在 Visual Basic .NET 中设置子例程来遍历记录数组并删除日期字段在当前日期之前的所有记录时遇到一些问题。 这是我当前的代码:

Sub FutureDate()
        Dim movefrom As Integer
        For x As Integer = 0 To UBound(notifications)

            If notifications(x).MeetingTime < Now.Date Then  'Finds first current/future date.
                movefrom = x
            End If
        Next
        Dim moveto As Integer = 0
        For x As Integer = movefrom To UBound(notifications) 'Moves dates after this to beginning of array.
            movefrom += 1
            notifications(moveto) = notifications(movefrom)
            moveto += 1
        Next
        ReDim Preserve notifications(moveto)  'Shortens the array to the correct length.
    End Sub

调用此子程序后,程序在消息框中显示数组中的前三个记录(用于调试目的)。但是,当我运行该程序时,消息框永远不会显示。这个子程序肯定是问题所在,因为注释掉调用它的行可以解决问题,并且会出现适当的消息,尽管第一个框包含过去的日期。这是用于通知/即将举行的会议系统,所以我显然不想包含已经过去的日期。

记录已经按日期排序,所以在我看来,这应该执行我想要的操作,即删除具有过去日期的记录,将其他所有内容移动到数组的前面,然后删除末尾的空格记录被移出。然而,我经常在类似的事情上犯愚蠢的错误,所以非常感谢外部的输入。您能提供的任何帮助都会很棒。

谢谢。

I am having a few problems trying to set up a subroutine in Visual Basic .NET to go through an array of records and remove all records where the date field is before the current date.
Here is my current code:

Sub FutureDate()
        Dim movefrom As Integer
        For x As Integer = 0 To UBound(notifications)

            If notifications(x).MeetingTime < Now.Date Then  'Finds first current/future date.
                movefrom = x
            End If
        Next
        Dim moveto As Integer = 0
        For x As Integer = movefrom To UBound(notifications) 'Moves dates after this to beginning of array.
            movefrom += 1
            notifications(moveto) = notifications(movefrom)
            moveto += 1
        Next
        ReDim Preserve notifications(moveto)  'Shortens the array to the correct length.
    End Sub

After this sub is called, the program shows the first three records in the array in message boxes (for debugging purposes). However, when I run the program, the message boxes are never displayed. This sub is definitely the problem, as commenting out the line that calls it fixes the problem, and the appropriate messages appear, albeit with the first box containing a date in the past. This is for a notifications/upcoming meetings system, so I obviously don't want to include dates that have passed.

The records are already sorted by date, so to my eyes this should do what I want it to, which is remove the records with dates in the past, move everything else to the front of the array, and then remove the spaces at the end that the records were moved from. However, I often make stupid mistakes with things like this, so outside input is much appreciated. Any help you can give would be great.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2024-12-17 17:52:24

这是一句简单的话:

Sub FutureDate()
    notifications = notifications.Where(Function(n) n.MeetingTime < Today).ToArray()
End Sub

但更好的设计是构建一个返回新数组的函数:

Function FutureDate(ByVal items() As MyType) As MyType()
    Return items.Where(Function(n) n.MeetingTime < Today).ToArray()
End Function

更好的是考虑序列而不是数组:

Function FutureDate(ByVal items As IEnumerable(Of MyType)) As IEnumerable(Of MyType)
    Return items.Where(Function(n) n.MeetingTime < Today)
End Function

This is a one-liner:

Sub FutureDate()
    notifications = notifications.Where(Function(n) n.MeetingTime < Today).ToArray()
End Sub

But a better design is to build a function that returns the new array:

Function FutureDate(ByVal items() As MyType) As MyType()
    Return items.Where(Function(n) n.MeetingTime < Today).ToArray()
End Function

And even better still is to think in terms of sequences rather than arrays:

Function FutureDate(ByVal items As IEnumerable(Of MyType)) As IEnumerable(Of MyType)
    Return items.Where(Function(n) n.MeetingTime < Today)
End Function
满栀 2024-12-17 17:52:24
DateTime[] futureDates = notifications.Where(n => n.MeetingTime > DateTime.Now).Select(n => n.MeetingTime).ToArray();

我认为这应该有效。如果不是,那就非常相似了。这应该只返回晚于“现在”的会议日期。

希望这有帮助。

DateTime[] futureDates = notifications.Where(n => n.MeetingTime > DateTime.Now).Select(n => n.MeetingTime).ToArray();

That should work, I think. If not, it's very similar. That should return just the dates from the meetings that are later than Now.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文