将VB字典中的条目写入文本文件

时间:2013-10-23 15:39:21

标签: vb.net file dictionary streamwriter

我正在大学学习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

我希望发布正确。现在,根据我的理解,读入工作就像进入字典的文件一样好,但是“WFile”,我的StreamWriter,让我感到难过。有人可以帮助我吗?同样,它应该在关闭时关闭并写入文件,而我关闭按钮的唯一代码是Me.Close()命令。如何编写触发器以使程序写入文件?知道主表单代码和我的'InventoryFile'都是单独的类,所以这必须通过引用其他有问题的类来完成

2 个答案:

答案 0 :(得分:1)

尝试此操作在文件中的一行上写下每个字典键/值对:

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之类的其他名称,然后从{{1}中提取其他信息。键/值对的一部分,因为Value实际上是一个Value类对象,如下所示:

Item

答案 1 :(得分: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