0

List在 Visual Basic 2010 中有一个对象(Appliance 作为 String,KwHHr 作为 Double), applianceListReturn 。每个对象都有两个成员。我想从每个对象中取出一个相同类型的成员(KwHHr),并从array这些单个成员中提取一个。我是 VB 新手,这并不像第一次出现那么容易。我试图使用 `for 每个循环来实现这一点,但它只返回一个值。这是我的代码:

    Imports System.Collections.Generic
    Public Class Form1
    Private ApplName As String '= Nothing
    Private ApplKwCost As Double
    Dim ConvAppliance As String
    Dim ConvKwHHr As String
    Public KwhCost As Double = 0

    Dim ApplianceCost As New ArrayList()

    'create structure for object in list
    Private Structure Appliance
        'public Variables

        Public Appliance As String
        Public KwHHr As Double

        ' provide get methods to allow return of objects in a formatted way
        Public ReadOnly Property ApplianceInfo() As String
            Get
                Return Appliance & " " & KwHHr
            End Get

        End Property
        Public ReadOnly Property ApplianceInfo2() As String
            Get
                Return Appliance & " " & KwHHr
                Return KwHHr
            End Get

        End Property

        Public Overrides Function ToString() As String
            Return ApplianceInfo2
        End Function
    End Structure

    Private ApplianceObj As Appliance
    Dim arraylistReturn As New List(Of Appliance)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' choose KwHr price
        KwCost.Text = KwhCost

        ' add appliance data to list
        ApplianceObj.Appliance = "Washer"
        ApplianceObj.KwHHr = 1.25

        arraylistReturn.Add(New Appliance With {.Appliance = ApplianceObj.Appliance, .KwHHr = ApplianceObj.KwHHr})

        ApplianceObj.Appliance = "Dryer"
        ApplianceObj.KwHHr = 1.3

        arraylistReturn.Add(New Appliance With {.Appliance = ApplianceObj.Appliance, .KwHHr = ApplianceObj.KwHHr})

        ApplianceObj.Appliance = "Toaster"
        ApplianceObj.KwHHr = 1.54

        arraylistReturn.Add(New Appliance With {.Appliance = ApplianceObj.Appliance, .KwHHr = ApplianceObj.KwHHr})

    End Sub

    Private Sub ApplAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApplAdd.Click
        KwhCost = KwCost.Text

        Dim newArray() As Double = {1.25, 1.3, 1.54}

        'enter appliance data
        ApplianceObj.Appliance = TxtBxApplAd.Text
        ApplianceObj.KwHHr = TextBoxKwhUse.Text
        ApplianceCost.Add(ApplianceObj.KwHHr)

        'displays added appliances in applist listbox
        displayAppliance(ApplianceObj)

        'add applianceobj entries to arraylistreturn

        arraylistReturn.Add(New Appliance With {.Appliance = ApplianceObj.Appliance, .KwHHr = ApplianceObj.KwHHr})

        For Each App As Appliance In arraylistReturn
            ArrayDispBox.Items.Add(App.ApplianceInfo.ToString())
        Next

        ReDim Preserve newArray(arraylistReturn.Count())
        'count items in list
        'AppList.Items.Add(arraylistReturn.Count().ToString)

        newArray(arraylistReturn.Count()) = TextBoxKwhUse.Text

        Dim sum As Double = 0
        For int As Integer = 0 To newArray.GetUpperBound(0)
            sum += newArray(int)
            TextBoxTotal.Text = sum.ToString()
        Next int
    End Sub

    Private Sub displayAppliance(ByVal App As Appliance)
        AppList.Items.Add(App.ApplianceInfo2)
    End Sub

    ' get selected appliances for calculation
    Private Sub ArrayDispBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ArrayDispBox.SelectedIndexChanged
        AppList.Items.Add(arraylistReturn.Item(ArrayDispBox.SelectedIndex))
    End Sub
End Class

我希望这是有道理的。谢谢您的帮助。

4

1 回答 1

2

您可以使用 LINQ to Object 来实现:

Dim results As Array(Of String) = applianceListReturn.Select(Function(a) a.KwHHr).ToArray()
于 2013-01-14T08:49:27.280 回答