“无法将'System.String'类型的对象强制转换为'System.Collections.ArrayList'”?

时间:2017-05-21 22:49:10

标签: arrays string vb.net visual-studio arraylist

My Application

我只是想通过ID号码将表格上的所有信息保存到阵列和放大器中。的ArrayList。然后,能够通过它的ID号检索该订单。 我可以保存订单,但是当我去检索时,我收到了这个错误:

Error

我查看了所有相关的类/函数/子...等。但无法找到此错误的来源。 我相信,由于异常被抛出,Visual Basic无法准确识别输出窗口中错误发生的行。

订单上“保存”和“检索”按钮的代码

Public Class frmChild

Private controller As clsController
Private itemsMenu() As String
Private itemsPrices() As String

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    'Save the order
    'We are going to use a combination of ArrayLists and Arrays to pass
    'All the order information to the business logic layer
    'By using this approach we use simple data structure to 
    'communicate between our objects

    'Create an Arraylist to hold all the order Info
    Dim orderList As New ArrayList

    Try
        orderList.Add(txtID.Text)   'Add Order ID
        orderList.Add(txtName.Text) 'Add Customer Name
        orderList.Add(txtPhone.Text) 'Add Phone Number
        orderList.Add(txtDate.Text) 'Add Date

        'Create an ArrayList to hold all the order detail info
        'for the first 3 lines
        Dim detailList As New ArrayList
        For i As Integer = 0 To 2
            'Create an Array to hold the information for each line item
            'Line Number, Item Description, Qty, Price
            Dim arrDetailItems(3) As String
            arrDetailItems(0) = Me.Panel1.Controls("lblLine" & (i + 1)).Text
            arrDetailItems(1) = Me.Panel1.Controls("cboItem" & (i + 1)).Text
            arrDetailItems(2) = Me.Panel1.Controls("txtQty" & (i + 1)).Text
            arrDetailItems(3) = Me.Panel1.Controls("txtPrice" & (i + 1)).Text

            'Add the array to our Detail arraylist
            detailList.Add(arrDetailItems)
        Next

        'Add the detail arraylist to the order arraylist
        orderList.Add(detailList)

        'Save the order
        controller.Save(orderList)

        'Check for errors
        If controller.getError <> "" Then

            'Display the list of all errors
            MessageBox.Show(controller.getError, "Error saving order", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else

            'Loop through each detail line
            'and retrieve the total for that line
            'use the order ID and the line ID to identify
            'the order and the line item
            For i As Integer = 0 To 2
                Me.Panel1.Controls("lblTotal" & (i + 1)).Text = FormatCurrency(controller.getLineTotal(txtID.Text, i + 1))
            Next

            '=== Add code below here to display totals
            txtTotal.Text = FormatCurrency(controller.getTotal(txtID.Text))
        End If

    Catch ex As Exception
        'Anything else
        MessageBox.Show(ex.Message, "Error Saving Item", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

End Sub

Private Sub btnRetrieve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrieve.Click
    'Retrieve the order
    Dim orderinfo As ArrayList
    Dim orderDetailList As ArrayList
    Try
        orderinfo = controller.Retrieve(txtID.Text)

        txtID.Text = orderinfo(0)
        txtName.Text = orderinfo(1)
        txtPhone.Text = orderinfo(2)
        txtDate.Text = orderinfo(3)

        orderDetailList = orderinfo(3)

        Dim arrDetail As String()
        For i As Integer = 0 To orderDetailList.Count - 1
            arrDetail = orderDetailList.Item(i)
            Me.Panel1.Controls("cboItem" & (i + 1)).Text = arrDetail(1)
            Me.Panel1.Controls("txtQty" & (i + 1)).Text = arrDetail(2)
            Me.Panel1.Controls("txtPrice" & (i + 1)).Text = FormatCurrency(arrDetail(3))
            Me.Panel1.Controls("lblTotal" & (i + 1)).Text = FormatCurrency(arrDetail(4))
        Next
        txtTotal.Text = FormatCurrency(orderinfo(3))
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

代码“保存()”

Public Class clsController

'A comment for this class goes here.
'The comment should describe the purpose of 
'the class and anthing else that is relevant 
'for future development.
'Please comment all your Subs / Functions / Variable Declaration
'appropriately

'Collection to hold our orders
Private colOrders As New Hashtable
Private anOrder As clsOrder
Private strError As String

Public Sub Save(ByVal OrderInfoList As ArrayList)
    'Check to see if order is already saved
    'If that's the case exit
    If colOrders.Contains(OrderInfoList(0)) Then
        addError("ID: Duplicate Order ID (Order exists already!)")
        Exit Sub
    End If

    'Create an order object an Order
    Dim order As New clsOrder

    'Clear the Class error variable
    strError = ""

    Try
        'Store the Order
        order.ID = OrderInfoList(0)                     '0
        order.CustomerName = OrderInfoList(1)           '1
        order.PhoneNumber = OrderInfoList(2)            '2
        order.OrderDate = OrderInfoList(3)              '3

        'Store the detail
        order.AddDetail(OrderInfoList(4))               '4

        'Check for errors
        If order.getError = "" Then
            colOrders.Add(order.ID, order)
        Else
            addError(order.getError)
        End If

    Catch ex As Exception
        'Add anything we haven't handled to our
        'Class error message
        addError(ex.Message)
    End Try
End Sub

代码“RETRIEVE()”

 Public Function Retrieve(ByVal anID As String) As ArrayList
    'Retrieve an Order
    Dim order As New clsOrder
    Dim orderInfo As New ArrayList
    Try
        'get the order object
        order = colOrders.Item(anID)

        'retrieve the order header info
        orderInfo.Add(order.ID)
        orderInfo.Add(order.CustomerName)
        orderInfo.Add(order.PhoneNumber)
        orderInfo.Add(order.OrderDate)

        'retrieve the order detail info
        orderInfo.Add(order.GetDetail())

        'retrieves the order total
        orderInfo.Add(order.getTotal())

    Catch ex As Exception
        'If there is not matching order
        'return an error
        If IsNothing(order) Then
            Throw New ApplicationException("Item not found, ID: " & anID)
            'Else
            '    'Any other error push back up the stack
            '    Throw
        End If
    End Try

    Return orderInfo
End Function

0 个答案:

没有答案