在Visual Basic中读取文件

时间:2014-04-24 00:25:52

标签: vb.net file class properties attributes

我正在制作一个节目,我应该遵循这套指示:

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. 

到目前为止,我已经编写了相当不错的代码部分,它可以正确地处理所写的内容(据我所知)。打开文件对话框只显示文本文件,当我选择一个文件时,正确的菜单选项是不显示的(是的,我知道这不是一个单词ha),点击每个菜单项会带我到相应的形式。因此,当我得到这个问题的答案时,我希望其他一切都会落实到位。

我遇到的问题是在文件中读取的方式是只在正确的列表框中显示名称或仅显示工资。我想以最简单的方式做到这一点,而不必改变现有代码的大部分,如果可能的话。

这是我到目前为止所拥有的:

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

如果您发现有关课程,属性,属性或功能的任何错误,我们将非常感谢您的帮助。请告诉我,我之前从未与其中任何人合作过,因此我不确定做得好。

1 个答案:

答案 0 :(得分:0)

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

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

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”,则每个循环交互可以读取一行,将其拆分为“,”以获取数据。因此,如果文件的名称没有工资,它将失去同步并崩溃。