我正在开发一个程序,应该遵循以下方向:
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
如果您能提供任何帮助,我们将不胜感激。如果您注意到与类、属性、属性或函数有关的任何错误,请告诉我,我以前从未使用过这些,所以不确定我是否处理得当。
class可以是它们所管理的数据的"主控器"。因此,无论Salaries.Show
和Names.Show
是什么(以及它们在哪里),它们都可以被内化到Project9类中。您几乎可以肯定地想要属性Public,以便可以访问它们。
一个提示,从VS2012开始,您不再需要以这种方式声明简单的道具:
Public Property Name As String
Public Property Salary as Decimal ' money almost always should be Decimal
由于VS为您创建了一个_Name变量,因此不再需要后备字段(例如strName)。当你有类似薪资资格的东西时,你需要完整/非自动道具声明。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",您可以读取每个循环交互的一行,将其拆分为","以获得数据。照原样,如果文件的名称没有薪水,它将失去同步并崩溃。