|
-
Apr 23rd, 2007, 12:14 AM
#1
Thread Starter
New Member
Help for a VB.net Noob
Good Evening! Please forgive me if a similar question has been asked before. I scoured the forum for hints, and I didn't find anything, so I don't think I am asking a redundant question. Here is the problem:
I have a project due tomorrow in which we have been given a program, and we need to alter the code so that it works using ByRef or ByVal subroutines instead of its present format. Here is the code:
Public Class Form1
Dim i As Integer = 0
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim name(3) As String
Dim rate(3) As Double
Dim hours(3) As Integer
Dim gross(3) As Double
Dim total As Double
Dim sr As IO.StreamReader = IO.File.OpenText("payroll.txt")
Dim format As String = "{0, -13}{1, -15:C2}{2, -16:N0}{3, -10:C2}"
Dim numberOfEmployees As Integer
'Input Section
Do While sr.Peek <> -1
i += 1
name(i) = sr.ReadLine
rate(i) = CDbl(sr.ReadLine)
hours(i) = CInt(sr.ReadLine)
Loop
numberOfEmployees = i
'Processing Section
For i = 1 To numberOfEmployees
If hours(i) <= 40 Then
gross(i) = hours(i) * rate(i)
Else
gross(i) = (rate(i) * 40) + ((rate(i) * 1.5) * (hours(i) - 40))
End If
total = total + gross(i)
Next i
'Output Section
For i = 1 To numberOfEmployees
'Display Report header
If i = 1 Then
With lstDisplay.Items
.Clear()
.Add("Payroll Report for week ending 03/23/07")
.Add(" ")
.Add(String.Format(format, "Employee", "Hourly Rate", "Hours Worked", "Gross Pay"))
.Add(" ")
End With
End If
'Issue Checks
lstDisplay.Items.Add(String.Format(format, name(i), " " & FormatCurrency(rate(i)), " " & hours(i), gross(i)))
Next i
lstDisplay.Items.Add(" ")
lstDisplay.Items.Add(String.Format(format, "Final Total", " ", " ", total))
End Sub
As you can see, it reads from a text file. The contents of this file are:
Al Adams
6.5
38
Bob Brown
5.7
50
Carol Coe
7
40
Please, please forgive me for being such a noob. I will accept any ridicule that you have to offer, as long as you might consider helping me. I am losing my mind!!!!!
Thank you!
-
Apr 23rd, 2007, 12:35 AM
#2
Addicted Member
Re: Help for a VB.net Noob
Do you not know how to create a ByVal or ByRef sub routine? Forgive me for asking, but can I see what you've done so far?
Last edited by Abrium; Apr 23rd, 2007 at 12:45 AM.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 23rd, 2007, 12:43 AM
#3
Re: Help for a VB.net Noob
You've got four sections by the look of it so you would need to create four methods. The first section is this:
vb Code:
'Input Section
Do While sr.Peek <> -1
i += 1
name(i) = sr.ReadLine
rate(i) = CDbl(sr.ReadLine)
hours(i) = CInt(sr.ReadLine)
Loop
numberOfEmployees = i
What are the inputs to that section? That would be the StreamReader and the arrays. They should be the arguments of the method. Actually, I would be inclined to create the StreamReader within the method because it's not needed afterwards. That codes a bit dodgy too because it doesn't close the reader.
That's what you need to do: break it up into sections and create a method for each section. Decide what the inputs to the section are and they become the method arguments. Give it a go and post back if you have specific issues because no-one will just provide the code for your homework, or shouldn't anyway.
-
Apr 23rd, 2007, 12:48 AM
#4
Thread Starter
New Member
Re: Help for a VB.net Noob
Well, I understand the concept of creating subroutines, but after hours of trial and error, I am really just running in circles. I have tried numerous avenues here, and I will show you the last iteration of what I have attempted. Mind you, this is filled with errors, dumb things, and most likely laughable to experts. Please be gentle. Here's what I tried last:
Public Class Form1
Dim sr As IO.StreamReader = IO.File.OpenText("payroll.txt")
Dim i As Integer = 0
Dim a As String, b, c, d, Total As Double
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim format As String = "{0, -13}{1, -15:C2}{2, -16:N0}{3, -10:C2}"
lstDisplay.Items.Clear()
With lstDisplay.Items
.Clear()
.Add("Payroll Report for week ending 03/23/07")
.Add(" ")
.Add(String.Format(Format, "Employee", "Hourly Rate", "Hours Worked", "Gross Pay"))
.Add(" ")
End With
Read(a, b, c)
Calc(b, c, d)
Disp(a, b, c, d, Total)
End Sub
Sub Read(ByVal a As String, ByRef b As Double, ByVal c As Double)
a = CStr(sr.ReadLine)
b = CDbl(sr.ReadLine)
c = CInt(sr.ReadLine)
End Sub
Sub Calc(ByRef b As Double, ByVal c As Double, ByVal d As Double)
Dim Total As Double
If c <= 40 Then
d = c * b
Else
d = (b * 40) + ((b * 1.5) * (c - 40))
End If
Total = Total + d
End Sub
Sub Disp(ByVal a As String, ByRef b As Double, ByVal c As Double, ByVal d As Double, ByVal Total As Double)
Dim format As String = "{0, -13}{1, -15:C2}{2, -16:N0}{3, -10:C2}"
lstDisplay.Items.Add(String.Format(format, a, " " & FormatCurrency(b), " " & c, d))
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
End Class
To most simply answer your question, I know what subroutines do, how they are supposed to work, but I have an instructor who hasn't done the greatest job in explaining them. Most of the learning I have had on this subject has occured in the last 45 minutes due to studying and scouring the internet. I am basically confused on how to start. If I could see the big forest instead of all of these unrelated trees, I could probably make sense of all this.
-
Apr 23rd, 2007, 12:56 AM
#5
Re: Help for a VB.net Noob
Firstly, I would suggest not starting anywhere but the beginning. The input section comes first so you should create an Input method first. Just leave everything else until you have the Input method working. Like I said, look at the section and work out what you need to bring into that block. Only once you know that can you hope to write a method declaration.
Now, with the Input section you have two choices. You can either call a method once and have the loop inside it or keep the loop in the event handler and call the same method multiple times inside it. I'm guessing that you're actually supposed to use the second option. In that case you'd need to pass in the StreamReader ByVal and the three array elements ByRef. The reader gets passed by value because you're not changing it and the others by reference because you are.
-
Apr 23rd, 2007, 02:47 AM
#6
Thread Starter
New Member
Re: Help for a VB.net Noob
Thanks alot guys! My compatriot successfully figured it out. After all is said and done, here's what we have:
Public Class Form1
Dim format As String = "{0, -13}{1, -15:C2}{2, -16:N0}{3, -10:C2}"
Dim sr As IO.StreamReader = IO.File.OpenText("payroll.txt")
Dim i As Integer = 0
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim Name As String = 0, Rate, Hours, Gross, Total As Double
Dim numberOfEmployees As Integer = 0
lstDisplay.Items.Clear()
With lstDisplay.Items
.Clear()
.Add("Payroll Report for week ending 03/23/07")
.Add(" ")
.Add(String.Format(Format, "Employee", "Hourly Rate", "Hours Worked", "Gross Pay"))
.Add(" ")
End With
Do While sr.Peek <> -1
Read(Name, Rate, Hours)
Calc(Rate, Hours, Gross, Total)
Disp(Name, Rate, Hours, Gross, Total)
Loop
lstDisplay.Items.Add(" ")
lstDisplay.Items.Add(String.Format(Format, "Final Total", " ", " ", Total))
End Sub
Sub Read(ByRef Name As String, ByRef Rate As Double, ByRef Hours As Double)
Name = sr.ReadLine
Rate = CDbl(sr.ReadLine)
Hours = CInt(sr.ReadLine)
End Sub
Sub Calc(ByRef Rate As Double, ByRef Hours As Double, ByRef Gross As Double, ByRef total As Double)
If Hours <= 40 Then
Gross = Hours * Rate
Else
Gross = (Rate * 40) + ((Rate * 1.5) * (Hours - 40))
End If
total = total + Gross
End Sub
Sub Disp(ByRef Name As String, ByRef Rate As Double, ByRef Hours As Double, ByRef Gross As Double, ByRef Total As Double)
lstDisplay.Items.Add(String.Format(format, Name, " " & FormatCurrency(Rate), " " & Hours, Gross))
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
End Class
The program works like it is supposed to. Only question:
Any suggestions as far as changing whether some of the variables are passed ByRef or ByVal? Like I say, the program works, but this exercise was mainly designed to make us understand which scenarios to pass ByVal or ByRef. As I stated earlier, our instructor is not the best, and I am not quite sure whether or not I used the parameters that I should have.
Again, thanks for all your help, and thanks for not being mean and insulting to a beginner. Really means alot.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|