【问题标题】:Reading a File in Visual Basic在 Visual Basic 中读取文件
【发布时间】:2014-06-09 01:46:53
【问题描述】:

我正在开发一个程序,并且应该遵循这组指示:

1. Create a class with the following (This may or may not be done correctly, I'm not sure)
     Attributes
        strName As String
        dblSalary As Double
     Properties
       name- can be any string
       salary- can only be positive numbers, if the number is negative, set it to equal 10
     computeSalary(intMonths As Integer)
        When called, the salary * number of months is returned.
2. Read in a file selected by the user. (I think I have this part done correctly)
     Must be a txt file
     Filter open file dialog to only show txt files
3. Once the file is read in, the following menu items are available 
     Show Employee Names
         Another form pops up displaying all the employee names read from the file in  a listbox
     Show Employee Salaries
         Another form pops up displaying all the employee salaries read from the file in a listbox
     Show an Employee
         Another form pops up displaying all the employees names read from the file in a listbox
         When a name is selected from the listbox, labels are filled showing that employees' name and salary.
         In this form there's an option to calculate an employees' salary for a given number of months
             When this button is pushed, an input pop up is shown and the user is asked to enter the number of months for which they'd like the salary to be calculated and displays the salary for the given number of months. 
                For example, if an employee makes $1,000 per month and the user enters a 3, $3,000 would be displayed. 

到目前为止,我已经编写了相当不错的部分代码,并且它对于所写的内容都可以正常工作(据我所知)。打开的文件对话框仅显示文本文件,当我选择一个文件时,正确的菜单选项未显示为灰色(是的,我知道这不是一个词哈),然后单击每个菜单项将我带到相应的表单。所以我希望当我回答这个问题时,其他一切都会到位。

我遇到的问题是以这样一种方式读取文件,即只有名称或只有薪水才会显示在正确的列表框中。如果可能的话,我想以最简单的方式做到这一点,而不必更改我当前代码的一大块。

这是我目前所拥有的:

Option Strict On
Imports System.IO

Public Class Main

    Private Sub open_Click(sender As Object, e As EventArgs) Handles open.Click
        Dim open As New OpenFileDialog
        open.Filter = "text files |*.txt|All Files|*.*"
        open.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

        If open.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim selectedFileName As String = System.IO.Path.GetFileName(open.FileName)
            showNames.Enabled = True
            showSalaries.Enabled = True
            showEmployee.Enabled = True
        End If

        Dim line As String
        Using reader As New StreamReader(open.OpenFile)
            While Not reader.EndOfStream
                line = reader.ReadLine
                Console.WriteLine(line)
            End While
        End Using

    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
        Names.Close()
        Salaries.Close()
        frmTotal.Close()
    End Sub

    Private Sub showNames_Click(sender As Object, e As EventArgs) Handles showNames.Click
        Names.Show()
    End Sub

    Private Sub showSalaries_Click(sender As Object, e As EventArgs) Handles showSalaries.Click
        Salaries.Show()
    End Sub

    Private Sub showEmployee_Click(sender As Object, e As EventArgs) Handles showEmployee.Click
        frmTotal.Show()
    End Sub
End Class

Public Class Project9

    Dim strName As String
    Dim dblSalary As Double

    Private Property Name() As String
        Get
            Return strName
        End Get
        Set(value As String)
            strName = value
        End Set
    End Property

    Private Property Salary() As Double
        Get
            Return dblSalary
        End Get
        Set(value As Double)
            If dblSalary < 0 Then
                dblSalary = 10
            End If
            dblSalary = value
        End Set

    End Property

    Private Function computeSalary(intMonths As Integer) As Double
        Dim dblTotal As Double = dblSalary * intMonths

        Return dblTotal
    End Function

End Class

这是我的文本文件

Steve McGarret  
1500.00
Danny Williams 
1300.00
Matthew Casey
1700.00
Kelly Severide
1750.00

任何帮助将不胜感激,如果您发现与类、属性、属性或函数有关的任何错误,请告诉我,我以前从未使用过任何这些,所以不确定我是否拥有它正确完成。

【问题讨论】:

  • 在不知道文本文件内容的情况下很难回答这个问题。能否提供样本数据?
  • 抱歉,刚刚用我的文本文件更新了我的问题
  • 文本文件是成对的,所以读取一行到 Name var 和旁边的薪水 var 并使用它们创建一个 Project9 实例(修复它之后,现在你无法获取或设置姓名或薪水)。所以展开streamreader循环。更大的问题是 emps 的存储位置...考虑List(Of Project9)
  • 感谢您的帮助,问题是我对这方面真的很陌生,不知道该怎么做哈。
  • 不是一个单一的Line 变量,而是读取一个名称,然后是一个薪水,用于创建一个 Project9 实例。不过,有几件事要先在课堂上解决

标签: vb.net file class properties attributes


【解决方案1】:

类可以是他们管理的数据的“主人”。因此,无论Salaries.ShowNames.Show 是什么(以及无论它们在哪里),它们都可以内化到 Project9 类中。您几乎肯定想要属性 Public,以便您可以访问它们。

一个提示,从 VS2012 开始,您不再需要以这种方式声明简单的 props:

Public Property Name As String
Public Property Salary as Decimal       ' money almost always should be Decimal

不再需要支持字段(例如 strName),因为 VS 会为您创建一个 _Name 变量。当你有类似工资资格的东西时,你需要完整/非自动道具声明。 computeSalary 函数也是私有的,所以你不能在外部调用它。

您还可以(应该)添加一个构造函数来在创建新实例时填写所需的值(避免每次都单独设置名称和薪水):

Public Sub New(n As String, s As Decimal)
     strName = n
     Salary = s      ' use the code in the prop setter
End Sub

您的代码中没有任何内容可用于读取文本文件(我们不知道其布局)或发布到列表框,但这里有一个提示:

Public Overrides Function ToString() As String
     Return strName &  "   "   & decSalary.ToString
End Sub

将该函数添加到 Project9 类并修改它以返回您需要的任何内容。这样,要填充列表框,您只需向其中添加一个 Project9 实例:

 Dim emp As New Project9("Beth", 1000)       ' bad name
 ' creates an emp with the name and salary
 ListBox.Items.Add(emp)

列表框将使用 ToString 函数进行显示/要仅显示姓名或薪水,请使用这些道具

编辑

    Dim newName As String
    Dim newSal As string
    Dim emp As Project9      ' which is crying out for a new name
    Using reader As New StreamReader(open.OpenFile)
        While Not reader.EndOfStream
            newName = reader.ReadLine
            newSal = reader.ReadLine

            emp = New Project9(newName, convert.ToDecimal(newSal))
            ' now, you need somewhere, something to store emp in               

            Console.WriteLine(line)
        End While
    End Using    

或者,如果文本行是“Ziggy Dunbar,1500.00”,您可以在每个循环交互中读取一行,将其拆分为“,”以获取数据。照原样,如果文件的名称没有薪水,它将不同步并崩溃。

【讨论】:

  • 那么关于属性,您是说不需要 Get 和 Set 部分(抱歉,直到本周我才知道属性是什么,所以从未使用过它们)?另外,在您回答的 Dim emp 部分,它的目的是什么,因为我正在从文件中读取信息,所以实际上并没有添加任何内容?
  • 存储emp,是像listbox,还是像数组?
  • 显示员工和显示员工姓名是其中的两种形式,显示员工是第四种形式,用户可以选择员工并根据一定的月数计算工资,显示员工姓名只是在不可选择的列表框中显示文件中的名称。
  • 这比我希望的要复杂得多哈哈。
  • 好的,我已经到了 C,我将如何修复这些属性?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多