【问题标题】:VB.NET - Created Class, Working with WriteOnly property but its not replacing variable valueVB.NET - 创建类,使用 WriteOnly 属性,但不替换变量值
【发布时间】:2012-07-04 06:49:15
【问题描述】:

程序执行后,FileStream 使用的扩展名是 Class 标头中提供的默认扩展名,而不是 i 通过“set property”指定的扩展名。

怎么一直没变?

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

【问题讨论】:

  • 谁知道?也许在某些时候myWallet 被一个新实例替换,或者Save(你没有显示)在另一个实例上被调用。或者 Save 里面有 default.txt 硬编码。也许Save 被称为 before Form1_Load 运行。很多可能的问题,但我们无法告诉你是哪一个,因为你没有向我们展示这些代码。
  • (可能Save 使用其他对象进行实际保存,并且该对象已在Wallet 构造函数中初始化)
  • 这是一个只有一个 Wallet 实例的简单项目。没有其他地方可以操作holdFile。这让我感到困惑。我们引用holdFile的唯一地方是在holdFile的set属性和header声明上。除此之外,下一个声明是 FileStream 的声明,它将 holdFile 作为参数。
  • 我同意@Damien_The_Unbeliever。我认为另一个例子在这里起作用。您可以检查在钱包标头中添加 Shared InstanceID = 0,在构造函数中增加 1,如果 InstanceID 为 1,请检查 Save 方法
  • 无论问题出在哪里,它都似乎出现在您迄今为止发布的代码中。

标签: vb.net winforms visual-studio-2010


【解决方案1】:

是的 - 问题就在这里:

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

这将使用holdPath 的值初始化_fileWallet 实例被创建时。与其将_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

【讨论】:

    猜你喜欢
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    相关资源
    最近更新 更多