2012-07-04 43 views
-1

在程序執行後,FileStream使用的擴展名是在Class標題中提供的默認擴展名,而不是通過「set屬性」指定的擴展名」。VB.NET - 創建類,使用WriteOnly屬性,但它不替代變量值

它從未改變過?

Form1.vb的代碼

Option Strict On 

    Imports S3_BalanceBook_Dayan.Wallet 

    Public Class Form1 
     Dim myWallet As New Wallet(DataGridView1, DateTimePicker1) 

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 


      'Make default selection when program starts. 
      optCheck.Checked = True 
      myWallet.StatementsFileName = "statements.dat" 
      DataGridView1.Rows.Add(New String() {"12/21/1986", "Test", "44554", "44.22", "45.12"}) 




     End Sub 

     Private Sub cmdAddTransaction_Click(sender As System.Object, e As System.EventArgs) Handles cmdAddTransaction.Click 
      If optCheck.Checked Then 
       lblAvailableFunds.Text = FormatCurrency(myWallet.Check(CInt(Trim(txtCheck.Text)), _ 
                     CDec(Trim(txtMoney.Text)))) 
      End If 
     End Sub 



     Private Sub optDeposit_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optDeposit.CheckedChanged 
      'Disable un-needed fields when Deposit Radio button is selected! 
      txtCheck.Enabled = False 
      txtMoney.Enabled = False 
      txtDeposit.Enabled = True 
      txtFee.Enabled = False 
      txtDescription.Enabled = True 
     End Sub 
     Private Sub optCheck_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optCheck.CheckedChanged 
      'Disable un-needed fields when Check Radio button is selected! 
      txtCheck.Enabled = True 
      txtMoney.Enabled = True 
      txtDeposit.Enabled = False 
      txtFee.Enabled = False 
      txtDescription.Enabled = True 

     End Sub 
     Private Sub optServiceFee_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optServiceFee.CheckedChanged 
      'Disable un-needed fields when Fee Radio button is selected! 
      txtCheck.Enabled = False 
      txtMoney.Enabled = False 
      txtDeposit.Enabled = False 
      txtFee.Enabled = True 
      txtDescription.Enabled = True 

     End Sub 


    End Class 

Wallet.vb代碼

Option Strict On 
Imports System 
Imports System.IO 


Public Class Wallet 

    Private lcheckNumber As Integer = Nothing 
    Private lcheckmoney As Decimal = Nothing 
    Private ldepositAmount As Decimal = Nothing 
    Private lfee As Decimal = Nothing 

    Private holdInstance As DataGridView 
    Private holdDate As DateTimePicker 
    Private holdPath As String = "default.txt" 
    Private _file As New FileStream(holdPath, FileMode.OpenOrCreate, FileAccess.ReadWrite) 
    Private file As New StreamWriter(_file) 

    'Default Constructor 
    Public Sub New() 

    End Sub 

    Public Sub New(ByRef Data As DataGridView, ByRef StatementDate As DateTimePicker) 
     'This constructor takes in references to use in class as private 
     holdInstance = Data 
     holdDate = StatementDate 
    End Sub 


    ''' <summary> 
    ''' Property allows the change of file path for the statements log. 
    ''' </summary> 
    Public WriteOnly Property StatementsFileName() As String 
     Set(ByVal Value As String) 
      holdPath = Value 
     End Set 
    End Property 


    ''' <summary> 
    ''' Enter Deposit amount as Decimal, returns remainding account balance as Decimal. 
    ''' </summary> 
    Public Function Deposit(ByVal Amount As Decimal) As Decimal 

     Return 0D 
    End Function 
    'Function Check - Deduct the amount and returns current balance. 
    Public Function Check(ByVal CheckNumber As Integer, ByVal CheckAmount As Decimal) As Decimal 
     Try 

      file.WriteLine(CheckNumber & " - " & CheckAmount) 

     Catch e As IOException 
      MessageBox.Show(e.ToString) 
     End Try 

     Return 0D 
    End Function 
    'Function Fee - Deduct the fee from balance and returns current balance. 
    Public Function Fee(ByVal FeeAmount As Decimal) As Decimal 

     Return 0D 
    End Function 



End Class 
+5

誰知道呢?也許在某個時候'myWallet'被一個新實例替換,或者'Save'(你沒有顯示)在不同的實例上被調用。或者'Save'在其中有'default.txt'硬編碼。在* Form1_Load運行之前,可能'Save'被調用*。很多可能的問題,但我們不能告訴你,因爲你沒有向我們展示這些代碼段。 –

+1

(也許'Save'使用一些其他對象用於實際保存,並且該對象在'Wallet'構造函數中被初始化) –

+0

它是一個只有一個錢包實例的簡單項目。在沒有其他地方holdFile被操縱。這讓我感到困惑。只有我們引用holdFile的地方位於holdFile的set屬性和標頭聲明上。除此之外,下一個聲明是FileStream的聲明,它將holdFile作爲參數。 – Dayan

回答

4

是的 - 它在這裏的問題是:

Private holdPath As String = "default.txt" 
Private _file As New FileStream(holdPath, FileMode.OpenOrCreate, FileAccess.ReadWrite) 
Private file As New StreamWriter(_file) 

這將初始化_file與值holdPath,在創建Wallet實例的位置。而不是_filefile作爲成員字段,爲什麼不使它們在Check內的局部變量?這樣,當它們被構建時,它們將使用值holdPath,就像當時那樣。


你也應該,可能,把它們放在裏面Using陳述,或以其他方式確保他們在合適的點關閉 - 否則,可能不會出現你寫的文件中的內容,直到你的程序關閉。


所以,我們必須:

Public Function Check(ByVal CheckNumber As Integer, ByVal CheckAmount As Decimal) As Decimal 
    Try 
     Using _file As New FileStream(holdPath, FileMode.OpenOrCreate, FileAccess.ReadWrite) 
      Using file As New StreamWriter(_file) 
       file.WriteLine(CheckNumber & " - " & CheckAmount) 
      End Using 
     End Using 

    Catch e As IOException 
     MessageBox.Show(e.ToString) 
    End Try 

    Return 0D 
End Function