2013-10-23 38 views
0

我正在學習大學的VB,並且遇到了一個與我的任務中的一個障礙。有人可以幫忙嗎?我的目標嘗試採取以下字典代碼:將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()命令。我將如何編寫觸發器來使程序寫入文件?知道主表單代碼和我的'庫存文件'都是獨立的類,所以這必須通過引用其他類的問題來完成

+0

一個相當大的調整。 RFile中的while循環不包含Readline。如果你的文件只有一行,你的代碼將會工作。然而,不止一行,沒有任何內容會被添加到字典中,或者最多隻添加到最後一行。使用'While not infile.EndOfStream'並且也包含Readline語句。 – tinstaafl

+0

啊!我知道我忘了那部分!感謝那! – NFSRacer

回答

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

+0

我會在'Me.Close'參數中或通過其他方法調用'Inventory.WFile'嗎? – NFSRacer

+0

只要有意義保存數據,調用'Inventory.WFile'。您可能想在X次更改後進行調用。要將其保存爲結尾的一部分,請在執行Me.Close之前在關閉按鈕單擊事件中調用它。 – Plutonix

+0

對,但我試圖記住如何簡單地從主窗體調用它。我會用什麼樣的關鍵詞?只需「呼叫」? – NFSRacer

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 

UPDATE:

由於Item既是循環中使用的變量,也是類的名稱,請將其更改爲另一個名稱,如singleItem,然後從Value porti中提取其他信息在鍵/值對的,因爲Value實際上是一個Item類對象,像這樣:可能需要

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 
+0

嗯,就是這樣。我在字典中有關鍵字,所以我可以使用第一部分,但描述和附加值都在另一個類中。是否可以使用你提供的'Item.Value'的方法作爲包含條目上所述細節的類? – NFSRacer

+0

我認爲問題的一部分是,您在'For Each'循環中使用名稱'Item'作爲變量名,以及'Item'作爲類的名稱,請參閱UPDATE:在答案中。 –

+0

嗯,是的,但VB並沒有將代碼中的變量標記爲類;據我所知,我需要通過聲明變量來代表類引用來做到這一點,例如While循環中的聲明。我仍然會試一試,看看它是如何工作的。現在,我只需要獲取顯示的內容... – NFSRacer