我在大学里从事 VB 工作,但我的一项作业遇到了障碍。有人可以帮忙吗?我的目标是尝试采用以下字典代码:
Public Class Inventory
Public ItemInventory As New Dictionary(Of String, Item)
Public Function iItem(ByVal key As String) As Item
Return ItemInventory(key)
End Function
Public Sub addItem(ByVal item As String, ByVal Desc As String, ByVal DRate As Double, ByVal WRate As Double, _
ByVal MRate As Double, ByVal Quantity As Integer)
With ItemInventory
.Add(item, New Item(item, Desc, DRate, WRate, MRate, Quantity))
End With
End Sub
Public Sub removeItem(ByVal item As String)
With ItemInventory
.Remove(item)
End With
End Sub
Public Function returnKeys() As String()
Dim Keys() As String
With ItemInventory
Keys = .Keys.ToList.ToArray
End With
Return Keys
End Function
End Class
不漂亮,我知道,但它完成了工作,这就是我的目标。现在这也与在程序中显示一个字典项目有关,我也遇到了问题,但是,我想一步一步来,所以我们稍后再谈, 如果可能的话。
根据写作,这是我当前的读写代码:
Imports System.IO
Public Class InventoryFile
Public Sub RFile(ByVal FPath As String, ByRef dInventory As Inventory)
Dim infile As StreamReader = File.OpenText(FPath)
Dim entireLine As String = infile.ReadLine()
Dim fields() As String = entireLine.Split(","c)
While infile.EndOfStream
Dim dItem As New Item
dItem.ID = fields(0)
dItem.Description = fields(1)
dItem.Daily = fields(2)
dItem.Weekly = fields(3)
dItem.Monthly = fields(4)
dItem.Quantity = fields(5)
'AddItem
dInventory.addItem(dItem.ID, dItem.Description, dItem.Daily, dItem.Weekly, _
dItem.Monthly, dItem.Quantity)
End While
End Sub
Public Sub WFile(ByVal FPath As String, ByRef dInventory As Inventory)
Dim outfile As StreamWriter = File.CreateText(FPath)
For Each Item As KeyValuePair(Of String, Item) In dInventory.ItemInventory
Next
End Sub
End Class
我希望发布正确。现在,据我了解,就文件进入字典而言,读入工作得很好,但是我的 StreamWriter 'WFile' 让我感到困惑。有人可以帮我吗?同样,它应该在关闭时关闭并写入文件,我关闭按钮的唯一代码是 Me.Close() 命令。我将如何编写触发器以使程序写入文件?知道主表单代码和我的“InventoryFile”都是独立的类,所以这必须通过引用其他有问题的类来完成