?
Solved

How to properly create and call a sub procedure

Posted on 2011-05-03
6
Medium Priority
?
919 Views
Last Modified: 2012-05-11
I have to create this program using sub procedures and being able to call those procedures to display the total gallons used and total price of a water bill, also I need to create a sub procedure and Call it to calculate. The program is working with the call procedures, but it does so without them as well. I am sure somethings not right. Here is my code:
Public Class frmMain
    Private Sub display(ByVal intnewmeter As Integer, ByVal intpreviousmeter As Integer)

    End Sub
    Private Sub Calculate(ByRef intNewmeter As Integer, ByRef intpreviousmeter As Integer)

    End Sub

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        ' calculates and displays the total gallons used and amount due


        Dim intnewmeter As Integer
        Dim intpreviousmeter As Integer
        Dim intgallonsused As Integer
        Dim decTotal As Decimal

        ' assign new meter and previous meter to variables
        Integer.TryParse(txtNewmeter.Text, intnewmeter)
        Integer.TryParse(txtpreviousmeter.Text, intpreviousmeter)

        ' calculate gallons used and total amount
        Call Calculate(intnewmeter, intpreviousmeter)
        If intnewmeter >= intpreviousmeter Then
            intgallonsused = (intnewmeter - intpreviousmeter)

        Else : intgallonsused = -1
        End If

        If intgallonsused = -1 Then
            decTotal = -1
        Else : decTotal = intgallonsused * 0.00205

        End If

        ' display total and gallons used
        Call display(intnewmeter, intpreviousmeter)
        lblgallons.Text = intgallonsused.ToString("N0")

        lbltotal.Text = decTotal.ToString("C2")

    End Sub


End Class

Open in new window

0
Comment
Question by:Mkelliny
  • 4
6 Comments
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 35514628
I may be stating the obvious here but...

Your subroutines don't do anything.

Therefore removing all reference to them will not change the behaviour of the program.

If you remove the definitions (Sub Calculate....) but leave the calls,  you should be getting a compile time error.

There is nothing wrong with the way you are calling them though some of the syntax is a little unusual.   e.g. most developers don't use the Call keyword to invoke a subroutine,  using a : to allow the else clause on the same line as the Else keyword....
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 35514797
Basically speaking, all the code below the calls should be inside the subroutines definde above.

BUT, your subroutines should either be given a reference to the variable you want to calculate OR your subroutines should be functions that return a value.

While I agree with Paul's comments about pieces of an if being on the same line, I disagree about the use of the key word Call.  While it is true that the key word is optional, there have been a few (very remote instances) where a subroutine didn't excute correctly, and the cause was leaving out the Call keyword.  In otherword, I had a subroutine written that passed a variable by reference.  Even though the subroutine was supposed to update the value of this variable, the updated value was not reflected back into the variable that was passed by reference to the subroutine.  The correction for this VERY obscure bug (can't say how many hours I spent tracking it down) was to include the optional Call keyword.
Private Sub Calculate(ByRef intNewmeter As Integer, ByRef intpreviousmeter As Integer, ByRef decTotal As Decimal)
        If intnewmeter >= intpreviousmeter Then
            intgallonsused = (intnewmeter - intpreviousmeter)
        Else  
            intgallonsused = -1
        End If

        If intgallonsused = -1 Then
            decTotal = -1
        Else 
            decTotal = intgallonsused * 0.00205
        End If
    End Sub

    Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        ' calculates and displays the total gallons used and amount due
        Dim intnewmeter As Integer
        Dim intpreviousmeter As Integer
        Dim intgallonsused As Integer
        Dim decTotal As Decimal

        ' assign new meter and previous meter to variables
        Integer.TryParse(txtNewmeter.Text, intnewmeter)
        Integer.TryParse(txtpreviousmeter.Text, intpreviousmeter)

        ' calculate gallons used and total amount
        Call Calculate(intnewmeter, intpreviousmeter, decTotal)

        ' display total and gallons used
        Call display(intnewmeter, intpreviousmeter)
        lblgallons.Text = intgallonsused.ToString("N0")
        lbltotal.Text = decTotal.ToString("C2")
    End Sub
End Class

Open in new window

0
 

Author Comment

by:Mkelliny
ID: 35514832
So what would the proper way be to invoke the call sub procedure? That is where my problem lies. I am new to programming, and the book I have is not the clearest. I have read this chapter 4 times and I still am not sure how to implement this. The : was a mistype.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:Mkelliny
ID: 35514980
Alright I did as you said HooKooDooKu and changed my code its almost fully functional now, and It displays the total price, but the gallons used does not display stays at 0.  Here is the changed code
Public Class frmMain
    Private Sub display(ByVal intgallonsused As Integer, ByVal dectotal As Decimal)

    End Sub
  

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
 
    Private Sub Calculate(ByRef intNewmeter As Integer, ByRef intpreviousmeter As Integer, ByRef decTotal As Decimal)
       
        Dim intgallonsused As Integer

        If intNewmeter >= intpreviousmeter Then
            intgallonsused = (intNewmeter - intpreviousmeter)
        Else
            intgallonsused = -1
        End If

        If intgallonsused = -1 Then
            decTotal = -1
        Else
            decTotal = intgallonsused * 0.00205
        End If
    End Sub

    Private Sub btnCalc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCalc.Click
        ' calculates and displays the total gallons used and amount due
        Dim intnewmeter As Integer
        Dim intpreviousmeter As Integer
        Dim decTotal As Decimal
        Dim intgallonsused As Integer

        ' assign new meter and previous meter to variables
        Integer.TryParse(txtNewmeter.Text, intnewmeter)
        Integer.TryParse(txtpreviousmeter.Text, intpreviousmeter)

        ' calculate gallons used and total amount
        Call Calculate(intnewmeter, intpreviousmeter, decTotal)

        ' display total and gallons used
        Call display(intgallonsused, decTotal)
        lblgallons.Text = intgallonsused.ToString("N0")
        lbltotal.Text = decTotal.ToString("C2")

    End Sub

Open in new window

0
 

Author Comment

by:Mkelliny
ID: 35515216
Figured it out, Thank you very much  HooKooDooKu and Paul_Harris_Fusion, I had to change

Private Sub Calculate(ByRef intNewmeter As Integer, ByRef intpreviousmeter As Integer, ByRef decTotal As Decimal)
       
        Dim intgallonsused As Integer


        If intNewmeter >= intpreviousmeter Then
            intgallonsused = (intNewmeter - intpreviousmeter)


to Private Sub Calculate(ByRef intNewmeter As Integer, ByRef intpreviousmeter As Integer, ByRef decTotal As Decimal, ByRef intgallonsused As Integer)


        If intNewmeter >= intpreviousmeter Then
            intgallonsused = (intNewmeter - intpreviousmeter)
0
 

Author Closing Comment

by:Mkelliny
ID: 35515229
Thank you so much!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question