我正在尝试获取一个类赋值的函数,但是当程序遇到有问题的特定行时它会消失,并且此行之后不执行任何操作。程序没有锁定,只是当前的执行路径死了。
我已经尝试过运行调试,但同样的情况也是如此。一旦我点击应该从存储在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 :(得分:1)
如果您只想插入一种类型,请使用List(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()
答案 1 :(得分:0)
没关系,我刚才想出来了。我不知道arraylists不是“arraylists”,而是一个集合。我想也许它会像其他面向集合的对象一样,你必须使用.item(i)来访问元素,事实证明是这样。
txtbGPH.text = appList.item(i).getAWusage()
产生正确的行为和其余代码,并且在被调用函数中设置的断点也是如此。