0

我在大学里从事 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”都是独立的类,所以这必须通过引用其他有问题的类来完成

4

2 回答 2

2

尝试将每个字典键/值对写入文件中的一行:

Dim fs As FileStream

' Open the stream and write to it.
fs = File.OpenWrite(FPath)

For Each Item As KeyValuePair(Of String, Item) In dInventory.ItemInventory
    fs.Write("{0}:{1}", Item.Key, Item.Value)
Next

更新:

既然Item是循环中使用的变量,又是一个类的名字,把它改成另一个名字就好了,然后从键/值对singleItem的部分拉出其他的信息,因为实际上是一个类对象,比如这个:ValueValueItem

Dim fs As FileStream

' Open the stream and write to it.
fs = File.OpenWrite(FPath)

For Each singleItem As KeyValuePair(Of String, Item) In dInventory.ItemInventory
    fs.Write("{0}:{1}:{2}:{3}:{4}:{5}:{6}", singleItem.Key, singleItem.Value.ID, singleItem.Value.Description, singleItem.Value.Daily, singleItem.Value.Weekly, singleItem.Value.Monthly, singleItem.Value.Quantity)
Next
于 2013-10-23T15:51:45.300 回答
0

为了创建您的 RFile 过程可以读取的格式,您需要以下内容:

 For Each kvp As KeyValuePair(Of String, Item) In dInventory.ItemInventory

    ' using Karl's compact approach, but add commas 
    ' since your Read expects them
    outfile.Write("{0},{1},{2},{3},{4}...", kvp.Key, kvp.Value.ID, _
        kvp.Value.Description, ... kvp.Value.Quantity.Tostring)

    ' the Value part of the fvp is another class, right?  `Value.XXX` should drill
    ' into it to get to the members 

 Next
 outfile.flush
 outfile.close        ' look into 'Using...'

那不是我会怎么做的,研究序列化以寻找一种不那么脆弱的方式来读取/写入数据。它基本上只用于这类事情:保存类数据以备后用,而且使用起来并不难。

至于连接它,按钮点击只会调用Inventory.WFile

于 2013-10-23T15:51:07.280 回答