[英]Reading a File in Visual Basic
I'm working on a program and am supposed to follow this set of directions: 我正在开发一个程序,应该遵循以下说明:
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.
I have a fairly decent portion of the code written so far and it works correctly for what is written (as far as I can tell anyway). 到目前为止,我已经编写了相当不错的代码,对于所写的内容,它可以正常工作(据我所知)。 The open file dialog only shows text files, when I select a file the correct menu options are un-greyed out (yes I know that's not a word ha), and clicking each of those menu items takes me to their corresponding forms.
打开文件对话框仅显示文本文件,当我选择一个文件时,正确的菜单选项未显示为灰色(是的,我知道这不是一个字ha),单击这些菜单项中的每个菜单项会将我带到它们对应的形式。 So I'm hoping that everything else will fall into place when I get this question answered.
因此,我希望当我回答此问题时,其他所有内容都可以使用。
The problem I'm having is reading in the file in such a way that only the names or only the salaries will be displayed in the correct listbox. 我遇到的问题是以这种方式读取文件,即在正确的列表框中仅显示名称或薪水。 I'd like to do this in the simplest way possible without having to change a big chunk of my current code if that's at all possible.
我想以最简单的方式做到这一点,而如果可能的话,不必更改当前代码的很大一部分。
Here's what I have so far: 这是我到目前为止的内容:
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
Here's my text file 这是我的文字档
Steve McGarret
1500.00
Danny Williams
1300.00
Matthew Casey
1700.00
Kelly Severide
1750.00
Any help would be greatly appreciated, also if you notice anything wrong pertaining to the classes, attributes, properties, or function please let me know, I've never worked with any of those before so am not sure that I have it done properly. 我们将不胜感激任何帮助,如果您发现与类,属性,属性或函数有关的任何错误,请告诉我,我之前从未与任何人合作过,所以不确定我是否能正确完成。
Class can be the 'masters' of the data they manage. 类可以是他们管理的数据的“主”。 As such, whatever
Salaries.Show
and Names.Show
are (and whereever they are), these can be internalized to the Project9 class. 这样,无论
Salaries.Show
和Names.Show
是什么(无论它们在哪里),都可以将它们内部化到Project9类中。 you almost certainly want the properties Public so you can access them. 您几乎可以肯定需要公共属性,以便可以访问它们。
One tip, as of VS2012, you no longer need to declare simple props that way: 一个提示,从VS2012开始,您不再需要以这种方式声明简单的道具:
Public Property Name As String
Public Property Salary as Decimal ' money almost always should be Decimal
The backing fields (eg strName) are no longer needed as VS creates a _Name variable for you. 由于VS为您创建了_Name变量,因此不再需要支持字段(例如strName)。 When you have something like the salary qualification, then you need the full/non auto prop declarations.
当您具有薪资资格时,则需要完整/非自动支柱声明。 The
computeSalary
function is also private so you cant call it externally. 该
computeSalary
函数也是私有的,因此您不能在外部调用它。
You can (should) also add a constructor to fill in the needed values when you create a new instance (avoids having to set the name and salary each time individually): 您还可以(应该)添加一个构造函数以在创建新实例时填写所需的值(避免每次都必须分别设置名称和薪水):
Public Sub New(n As String, s As Decimal)
strName = n
Salary = s ' use the code in the prop setter
End Sub
There is nothing in your code for reading the text file (we dont know its layout) nor for posting to the listbox, but here is a tip: 您的代码中没有读取文本文件(我们不知道其布局)或发布到列表框的内容,但是这里有个提示:
Public Overrides Function ToString() As String
Return strName & " " & decSalary.ToString
End Sub
Add that function to the Project9 class and modify it to return whatever you need. 将该函数添加到Project9类,并对其进行修改以返回所需的内容。 That way, to populate the listbox, you just add a Project9 instance to it:
这样,要填充列表框,只需向其中添加一个Project9实例:
Dim emp As New Project9("Beth", 1000) ' bad name
' creates an emp with the name and salary
ListBox.Items.Add(emp)
the listbox will use the ToString function for display/ To show just the Names or Salaries, use those Props 列表框将使用ToString函数进行显示/仅显示姓名或薪水,请使用这些道具
EDIT 编辑
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
Alternatively, if the text lines were "Ziggy Dunbar, 1500.00", you could read one line per loop interation, split it at "," to get the data. 或者,如果文本行是“ Ziggy Dunbar,1500.00”,则您可以为每个循环插入读取一行,将其拆分为“,”以获取数据。 As is, if the file has a name with no salary, it will get out of synch and crash.
照原样,如果文件的名称不带薪水,它将不同步并崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.