VB.NET 2008应用程序在Do Loop期间崩溃 [英] VB.NET 2008 Application crashing during Do Loop

查看:81
本文介绍了VB.NET 2008应用程序在Do Loop期间崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将listbox1上的每个项目与listbox2上的所有项目进行比较的应用程序。如果找到该项目,则将其从两个列表中删除。目标是只将未找到的项目保留在两个列表中。



问题是,应用程序只是挂起,而我从没得到任何结果。我几次看了我的代码,却不知道发生了什么(编程新手,我知道...)。



有人可以帮我吗?



代码段:

  Private Sub Button2_Click(ByVal发送者作为System.Object, ByVal e As System.EventArgs)处理Button2.Click 

Dim a As字符串
Dim b As字符串
Dim y As字符串

For i As整数= 0至ListBox1.Items.Count-1
a = ListBox1.Items(i)
y = 1
y = 1
对于x作为整数= 0至ListBox2。 Items.Count-1
b = ListBox2.Items(x)
Dim res As Int16 = String.Compare(a,b)
如果res = 0则
y = 0
ListBox2.Items.Remove(i)
ListBox2.Items.Remove(x)
ElseIf x = ListBox1.Items.Count然后
退出
如果
下一个
结束
下一个
结束Sub


解决方案

如果ListBox1.Items.Count大于ListBox2.Items.Count-1,X将永远不等于ListBox1.Items.Count,因此Exit Do将永远不会运行,并且代码只会在

  Do y = 1 


例如,您是否考虑过使用Linq来简化列表管理?



编辑:此外,删除您正在使用的列表中遍历的项目(用For Each进行操作是完全违法的),因为每次删除都会抵消循环计数器。



edit2:这是一个可以完成任务的Linq代码段:

  Dim itemsFirst =(来自ListBox1.Items选择项目中的字符串形式的项目)
Dim itemsSecond =(从ListBox2.Items选择项目中的字符串形式的项目)

Dim dupes = System.Linq.Enumerable.Intersect(itemsFir st,itemsSecond).ToList
对于重复的每个项目
ListBox1.Items.Remove(项目)
ListBox2.Items.Remove(项目)
下一项

基本上是从两个列表中都提取字符串(这是必需的,因为ListBox.Items集合有点怪异)



之后,我们运行intersect方法并将结果复制到列表中。 (.ToList部分)
复制是必需的部分,因为,否则dupes将只是ListBox中Items的子集,我们将再次尝试通过拉紧鞋带来提升自己。 p>

最后一部分只是一个简单的删除循环,将从集合中删除项目


I am writing an application to compare each item on listbox1 to all items on listbox2. If the item is found, then remove it from both lists. The goal is to only have the items that were not found remain on both lists.

The problem is, the application just hangs and I never get any results. I looked at my code several times and I cannot figure out what's going on (programming noob I know...).

Can anybody help me with this?

Code Snippet:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim a As String
    Dim b As String
    Dim y As String

    For i As Integer = 0 To ListBox1.Items.Count - 1
        a = ListBox1.Items(i)
        y = 1
        Do While y = 1
            For x As Integer = 0 To ListBox2.Items.Count - 1
                b = ListBox2.Items(x)
                Dim res As Int16 = String.Compare(a, b)
                If res = 0 Then
                    y = 0
                    ListBox2.Items.Remove(i)
                    ListBox2.Items.Remove(x)
                ElseIf x = ListBox1.Items.Count Then
                    Exit Do
                End If
            Next
        Loop
    Next
End Sub

解决方案

if ListBox1.Items.Count is more that ListBox2.Items.Count - 1, X will never equal ListBox1.Items.Count, so the Exit Do will never run, and the code will just loop endlessly in the

Do While y = 1

Have you considered using Linq for example, for easier list management?

edit: Additionally it's wrong to delete an item from the list you are traversing with a for (it's downright illegal to do that with a For Each) because each deletion will offset the loop counter.

edit2: here's a Linq snippet that accomplishes the task:

    Dim itemsFirst = (From item As String In ListBox1.Items Select item)
    Dim itemsSecond = (From item As String In ListBox2.Items Select item)

    Dim dupes = System.Linq.Enumerable.Intersect(itemsFirst, itemsSecond).ToList
    For Each item In dupes
        ListBox1.Items.Remove(item)
        ListBox2.Items.Remove(item)
    Next item

what is does is basically extract the strings from both list (this is necessary because the ListBox.Items collection is a little weird)

After that we run the intersect method and copy the results into a list. (the .ToList part) The copying is a required part because, otherwise dupes would just be a subset of the Items of the ListBox, and once again we would be trying to lift ourselves by pulling on our shoestrings.

The last part is just a simple delete loop, that removes the items from the collection

这篇关于VB.NET 2008应用程序在Do Loop期间崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆