【发布时间】: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