我在Visual Basic 2010中有List
个对象(Appliance as String和KwHHr as Double),applianceListReturn。每个对象都有两个成员。我想从每个对象中取一个相同类型的成员(KwHHr),并从这些单个成员中生成array
。我是VB新手,这并不像它第一次出现那么容易。我试图使用`for each循环来实现这一点,但它只返回一个值。这是我的代码:
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
我希望这是有道理的。谢谢你的帮助。
答案 0 :(得分:2)
您可以使用LINQ to Object来实现:
Dim results As Array(Of String) = applianceListReturn.Select(Function(a) a.KwHHr).ToArray()