-1

プログラムの実行後、FileStreamが使用する拡張子は、「setプロパティ」で指定した拡張子ではなく、Classヘッダーで提供されるデフォルトの拡張子です。

どうして変わらないの?

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
4

1 に答える 1

4

うん - ここに問題があります:

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

インスタンスが作成された時点で_fileの値で初期化されます。andをメンバ フィールドとして持つ代わりに、それらを 内のローカル変数にしないのはなぜですか? そうすれば、それらが構築されるときに、その時点の の値が使用されます。holdPathWallet_filefileCheckholdPath


また、おそらくそれらを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
于 2012-07-04T12:11:21.203 に答える