2013-04-09 54 views
0

我想獲得一個類分配工作的功能,但隨着程序擊中特定的線路,它會死掉,沒有任何行後執行。程序沒有鎖定,只是當前執行路徑死亡。從Arraylist元素不起作用的函數調用

我試過運行調試,但發生的情況大致如此。一旦我點擊應該從存儲在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 

回答

0

沒關係,我想明白了。我不知道ArrayList不是「陣列列表」,而是一個集合。我想也許它會像其他面向集合的對象一樣行事,並且你必須使用.item(i)來訪問元素,事實證明是這樣的。

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

產生適當的行爲和代碼的其餘部分之後在OP指示的問題的行執行一樣設定在所調用的函數的斷點。

1

使用List(Of X)而不是ArrayList如果你要插入只有一種類型:

Dim appList As New List(Of Appliances) 

,我建議你宣佈你的臨時VAR方法裏面,除非是必要的。不管怎樣,在這種情況下,你不需要它,你可以通過這種方式添加VAR:

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

有了這個使用(使用列表),您將不需要使用arraylistObj.Item(i).Method(),你可以簡單地使用通用方式:

s = appList(i).getWUsage() 
+0

感謝您的建議。我會記住他們爲未來的項目。 – Geowil 2013-04-11 13:08:34