0

我试图让一个类分配的函数起作用,但是当程序遇到有问题的特定行时,它会死掉,并且在该行之后什么都不会执行。程序没有锁定,只是当前执行路径死了。

我尝试过运行调试,但发生了很多相同的情况。一旦我点击应该从存储在 Arraylist 元素中的对象调用函数的链接,应该调用的实际函数处的断点就不会被命中,并且不会发生任何进一步的事情。

Public Structure Appliances

    ' Create New Appliance object
    Public Sub New(name As String, pusage As Double)
        aName = name
        aPUsage = pusage
    End Sub

    ' Create New Washer Object
    Public Sub New(name As String, pusage As Double, wusage As Double)
        aName = name
        aPUsage = pusage
        aWUsage = wusage
    End Sub

    ' Functions
    Public Function getAName()
        Return aName
    End Function

    Public Function getAPUsage()
        Return aPUsage
    End Function

    Public Function getAWUsage()
        Return aWUsage
    End Function

    Dim aName As String ' Appliance Name
    Dim aPUsage As Double ' Appliane Power Usage
    Dim aWUsage As Double ' Appliance Water Usage

End Structure

...

Public Class Form1

...
    Dim appList As New ArrayList() ' Create an arraylist appliance objects
    Public appTemp As Appliances ' To store appliance objects until they can be added to the arraylist

...
    Private Function getAppInfo()
        getAppInfo = Nothing

        Do While fInStream.Peek() <> -1

            s = fInStream.ReadLine() ' Get a line from the file and set s to it

            Dim words As String() = s.Split(New Char() {","c}) ' Split the line contents along commas and set those parts into words

            words(0) = words(0).Replace("_", " ") ' Reaplce underscores with spaces

            If (words.Count = 3) Then ' If words contains the washer appliance
                appTemp = New Appliances(words(0), Double.Parse(words(1)), Double.Parse(words(2)))
                appList.Add(appTemp)

            Else ' For all other appliances
                appTemp = New Appliances(words(0), Double.Parse(words(1)))
                appList.Add(appTemp)

            End If

        Loop
    End Function

    Private Function setUsage(name As String)
        setUsage = Nothing

        ' Find appliance
        For i = 0 To appList.Count
            If (name = appList(i).getAName()) Then
                If (name = "Washer") Then
                    s = appList(i).getWUsage() ' !!!This is the line where the execution dies at, nothing after this line is processed and the function call is not completed
                    txtbGPH.Text = s
                End If

                MsgBox("Test 1")
                Exit For

            ElseIf (i = appList.Count) Then
                MsgBox("Appliance could not be found")
            End If
        Next
    End Function

End Class
4

2 回答 2

1

如果您只想插入一种类型,请使用 aList(Of X)而不是:ArrayList

Dim appList As New List(Of Appliances)

除非必要,否则我建议您在方法中声明您的 temp var。无论如何,在这种情况下你不需要它,你可以通过这种方式添加你的 var:

appList.Add(New Appliances(words(0), Double.Parse(words(1))))

使用这种用法(使用列表),您将不需要使用arraylistObj.Item(i).Method(),您可以简单地使用常用方式:

s = appList(i).getWUsage()
于 2013-04-09T08:29:49.050 回答
0

没关系,我刚才想通了。我不知道数组列表不是“数组列表”而是一个集合。我认为它可能会像其他面向集合的对象一样,并且您必须使用 .item(i) 来访问元素,事实证明就是这样。

txtbGPH.text = appList.item(i).getAWusage()

在 OP 中指示的有问题的行执行后产生正确的行为和其余代码,就像在被调用函数处设置的断点一样。

于 2013-04-09T04:15:20.630 回答