0

I am trying to get a function for a class assignment to work however as the program hits the specific line in question it dies off and nothing after this line will execute. The program does not lock up, just the current execution path dies.

I have tried running debugging but much the same happens. Once I hit the link that should call a function from the object stored in the Arraylist element the break point at the actual function that should be called is not hit and nothing further happens.

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

2 Answers 2

1

Use a List(Of X) instead of ArrayList if you are going to insert only one type:

Dim appList As New List(Of Appliances)

And I recommend you to declare your temp var inside the methods unless is necessary. Anyway, in this case you don't need it, you can add your var in this way:

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

With this use (using lists) you won't need to use arraylistObj.Item(i).Method() and you can simply use the common way:

s = appList(i).getWUsage()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestions. I will remember them for future projects.
0

Nevermind, I figured it out just now. I did not know that arraylists are not "arraylists" but a collection. I thought maybe it would act like other collection oriented objects and that you have to use a .item(i) to access the elements, which turns out to be the case.

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

produces the proper behavior and the rest of the code after the problematic line indicated in the OP executes as does the break point set at the called function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.