附加文本文件附有新数据并更新变量值的问题



这是我的代码:

Imports System.IO
Public Class HomeUtilityAudProgram
Private Structure HomeUtility
    Public kwCostDec As Decimal
    Public dailyHoursInt As Integer
    Public KwPowerInt As Integer
    Public waterGalCostDec As Decimal
    Public waterGalUsedInt As Integer
    Public decOperatingCost As Decimal
End Structure
Private objHomeUtility As HomeUtility
Public Property lstWords As Object
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles TV.Click
    'Display TV
    lblMessage.Text = "TV"
End Sub
Private Sub decCostPerKw_TextChanged(sender As Object, e As EventArgs) Handles decCostPerKw.TextChanged
End Sub
Private Sub intPowerPerKw_TextChanged(sender As Object, e As EventArgs) Handles intPowerPerKw.TextChanged
End Sub
Private Sub intHoursPerDay_TextChanged(sender As Object, e As EventArgs) Handles intHoursPerDay.TextChanged
End Sub
Private Sub DecBtnlCalcCost_Click(sender As Object, e As EventArgs) Handles DecBtnCalcCost.Click
    Try
        'Array and variable declarations
        objHomeUtility.kwCostDec = Decimal.Parse(decCostPerKw.Text, Globalization.NumberStyles.Currency)
        objHomeUtility.dailyHoursInt = Integer.Parse(intHoursPerDay.Text, Globalization.NumberStyles.Number)
        objHomeUtility.waterGalCostDec = Decimal.Parse(decCostPerGal.Text, Globalization.NumberStyles.Currency)
        objHomeUtility.waterGalUsedInt = Integer.Parse(intNumGalUsed.Text, Globalization.NumberStyles.Number)
        Dim sr As New StreamReader("applianceWatt.txt")
        objHomeUtility.KwPowerInt = CInt(Val("")) '<< Add Val() to handle non-number strings
        Dim KwsPowerInt(-1) As Integer '<< add -1 inside brackets to initialise the array 
        Dim i As Integer = 0
        Do Until sr.Peek = -1
            'grab one value at a time from text file
            objHomeUtility.KwPowerInt = CInt(Val(sr.ReadLine())) '<< Add Val() to handle non-number strings
            i += 1 '<< increment your counter
        Loop
        sr.Dispose() '<< close the file
        If objHomeUtility.KwPowerInt <= 6000 And objHomeUtility.KwPowerInt >= 10 Then
            objHomeUtility.decOperatingCost = objHomeUtility.kwCostDec * objHomeUtility.KwPowerInt * objHomeUtility.dailyHoursInt + objHomeUtility.waterGalCostDec * objHomeUtility.waterGalUsedInt
            lblMessage.Text &= " using " + objHomeUtility.KwPowerInt.ToString("G") + "Kw of power " + " for " + objHomeUtility.kwCostDec.ToString("C") + " an hour for " + objHomeUtility.dailyHoursInt.ToString("G") + " hours and " + objHomeUtility.waterGalCostDec.ToString("C") + " per gallon of water for " + objHomeUtility.waterGalUsedInt.ToString("G") + " gallons is " + objHomeUtility.decOperatingCost.ToString("C")
        Else
            lblMessage.Text = "Invalid wattage entered."
        End If
        Dim Result = MsgBox("Do you want to change the default wattage value?", MsgBoxStyle.YesNo, "Append Wattage")
        If Result = MsgBoxResult.Yes Then
            intPowerPerKw.Visible = True
            objHomeUtility.KwPowerInt = File.CreateText.StreamReader("applianceWatt.txt")
            If objHomeUtility.KwPowerInt <= 6000 And objHomeUtility.KwPowerInt >= 10 Then
                objHomeUtility.decOperatingCost = objHomeUtility.kwCostDec * objHomeUtility.KwPowerInt * objHomeUtility.dailyHoursInt + objHomeUtility.waterGalCostDec * objHomeUtility.waterGalUsedInt
                lblMessage.Text &= " using " + objHomeUtility.KwPowerInt.ToString("G") + "Kw of power " + " for " + objHomeUtility.kwCostDec.ToString("C") + " an hour for " + objHomeUtility.dailyHoursInt.ToString("G") + " hours and " + objHomeUtility.waterGalCostDec.ToString("C") + " per gallon of water for " + objHomeUtility.waterGalUsedInt.ToString("G") + " gallons is " + objHomeUtility.decOperatingCost.ToString("C")
            Else
                lblMessage.Text = "Invalid wattage entered."
            End If
        End If
    Catch ex As Exception
        MessageBox.Show("Error in quantity on hand or product cost", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
Private Sub Fridge_Click(sender As Object, e As EventArgs) Handles Fridge.Click
    'Display Refrigerator
    lblMessage.Text = "Refrigerator"
End Sub
Private Sub Heater_Click(sender As Object, e As EventArgs) Handles Heater.Click
    'Display Heater
    lblMessage.Text = "Heater"
End Sub
Private Sub Fan_Click(sender As Object, e As EventArgs) Handles Fan.Click
    'Display Fan
    lblMessage.Text = "Fan"
End Sub
Private Sub Dryer_Click(sender As Object, e As EventArgs) Handles Dryer.Click
    'Display Dryer
    lblMessage.Text = "Dryer"
End Sub
Private Sub Oven_Click(sender As Object, e As EventArgs) Handles Oven.Click
    'Display Oven
    lblMessage.Text = "Oven"
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    decCostPerKw.Clear()
    intPowerPerKw.Clear()
    intHoursPerDay.Clear()
    decCostPerGal.Clear()
    intNumGalUsed.Clear()
    lblMessage.Text = String.Empty
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
    ' Close the form.
    Me.Close()
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles Washer.Click
    'Display Washer
    lblMessage.Text = "Washer"
End Sub
Private Sub Label5_Click(sender As Object, e As EventArgs) Handles Label5.Click
End Sub
Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click
End Sub
Private Sub intNumGalUsed_TextChanged(sender As Object, e As EventArgs) Handles intNumGalUsed.TextChanged
End Sub
End Class

我以前工作过,但是任务是在代码中添加数组或结构,这就是为什么它存在。我已经成功添加了一个结构,并访问了文本文件并将其加载到变量中。我当前的问题是附加变量的值并更新文本文件。

不确定应该是哪种类型的lstwords。尝试List(Of Integer)

Public lstWords As New List(Of Integer)

至于您的文本文件阅读,您没有为新条目腾出空间:

Dim sr As New StreamReader("applianceWatt.txt")
Dim KwPowerInt As Integer = CInt(Val("")) '<< Add Val() to handle non-number strings
Dim KwsPowerInt(-1) As Integer '<< add -1 inside brackets to initialise the array 
Dim i As Integer = 0
Do Until sr.Peek = -1
  'grab one value at a time from text file
  KwPowerInt = CInt(Val(sr.ReadLine())) '<< Add Val() to handle non-number strings
  'Place value into array
  ReDim Preserve KwsPowerInt(i) '<< Add Redim Preserve to make space for the new result
  KwsPowerInt(i) = KwPowerInt
  'Output
  lstWords.Add(KwsPowerInt(i)) '<< add to List(Of Integer)
  i += 1 '<< increment your counter
Loop
sr.Dispose() '<< close the file

相关内容

  • 没有找到相关文章

最新更新