NFSRacer
NFSRacer

Reputation: 19

Writing Entries in a VB Dictionary into a Text File

I'm working on VB in college and am running into a snag with one of my assignments. Can someone help? I'm aiming to try to take the following dictionary code:

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

Not pretty, I know, but it gets the job done, that's all I aim to do. Now a bit of this also has to do with displaying a dictionary item in the program, which I'm also having issues with, however, I'd like to take this one step at a time, so we'll get to that later, if possible.

As per writing, this is my current code for reading and writing:

    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

I hope that posted right. Now, reading in, as far as I understand, works just fine, in terms of a file going into a dictionary, however 'WFile', my StreamWriter, is what's got me stumped. Can someone help me with that? Likewise, its supposed to close and write to the file upon closing, and my only code for the close button is the Me.Close() command. How would I write a trigger to make the program write to the file? Know that the main form code, and my 'InventoryFile' are both separate classes, so this has to be done by referencing the other classes in question

Upvotes: 0

Views: 3407

Answers (2)

In order to create the format your RFile procedure can read you need something like this:

 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...'

Thats NOT how I would do it, look into serialization for a less fragile way to read/write the data. It is basically meant for just this sort of thing: save class data for later, and it is not that hard to use.

as for hooking it up, the button click would just call Inventory.WFile

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34846

Try this to write each dictionary key/value pair on a single line in the file:

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

UPDATE:

Since Item is both the variable used in the loop and the name of a class, change it to another name like singleItem and then pull out the other pieces of information from the Value portion of the key/value pair, because the Value is actually an Item class object, like this:

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

Upvotes: 2

Related Questions