VB.NET 日期数组处理问题
我在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一句简单的话:
但更好的设计是构建一个返回新数组的函数:
更好的是考虑序列而不是数组:
This is a one-liner:
But a better design is to build a function that returns the new array:
And even better still is to think in terms of sequences rather than arrays:
我认为这应该有效。如果不是,那就非常相似了。这应该只返回晚于“现在”的会议日期。
希望这有帮助。
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.