Function procedures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michelle Monty
    New Member
    • Oct 2007
    • 24

    #1

    Function procedures

    I'm creating an application where a user creates a cable bill. The application calculates the bill based on whether the customer is a residential or business customer. I've finished the interface and all of the calculations. However, the assignment is to use one or more sub or function procedures. I have several sub procedures but I don't have any function procedures and I'm not sure how to include one in my program without messing up what I've created. Any advice would be greatly appreciated. Thanks! Here's my code:

    'Project Name: Tammy Seilheimer
    'Project Purpose: This project calculates a customer's bill for cable services
    'Created By: Tammy Seilheimer

    [code=vbnet]
    Option Explicit On

    Public Class Form1

    Private Sub residentialButt on_CheckedChang ed(ByVal sender As Object, ByVal e As System.EventArg s) Handles residentialbutt on.CheckedChang ed
    connectionstext box.Visible = False
    connectionsquan titytextbox.Vis ible = False
    TextBox4.Visibl e = False
    Label7.Visible = False
    Label8.Visible = False

    End Sub

    Private Sub businessbutton_ checkedchanged( ByVal sender As Object, ByVal e As System.EventArg s) Handles businessbutton. CheckedChanged
    connectionstext box.Visible = True
    connectionsquan titytextbox.Vis ible = True
    TextBox4.Visibl e = True
    Label7.Visible = True
    Label8.Visible = True
    End Sub


    Private Sub calculatebutton _Click(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles calculatebutton .Click

    If residentialbutt on.Checked Then
    Dim channelprice As Decimal
    Dim residentialnumb erofchannels As Integer
    Dim residentialchan neltotalprice As Decimal
    Dim residentialamou ntdue As Decimal
    Dim processingfee As Decimal
    Dim basicfee As Decimal
    Dim isconverted As Boolean
    Dim isconverted2 As Boolean
    Dim isconverted3 As Boolean
    Dim isconverted4 As Boolean
    isconverted = Integer.TryPars e(channelpricet extbox.Text, channelprice)
    isconverted2 = Integer.TryPars e(processingtex tbox.Text, processingfee)
    isconverted3 = Integer.TryPars e(basicfeetextb ox.Text, basicfee)
    isconverted4 = Integer.TryPars e(premiumquanti tytextbox.Text, residentialnumb erofchannels)
    'connectionstex tbox.Visible = False
    'connectionsqua ntitytextbox.Vi sible = False
    ' TextBox4.Visibl e = False
    'Label7.Visible = False
    'Label8.Visible = False


    If isconverted4 = True Then
    processingfee = 4.5
    basicfee = 30
    channelprice = 5
    premiumquantity textbox.Text = residentialnumb erofchannels
    channelpricetex tbox.Text = channelprice.To String("C2")
    residentialchan neltotalprice = (residentialnum berofchannels * channelprice)
    channeltextbox. Text = residentialchan neltotalprice.T oString("C2")
    processingtextb ox.Text = processingfee.T oString("C2")
    basicfeetextbox .Text = basicfee.ToStri ng("C2")
    residentialamou ntdue = (processingfee + basicfee + residentialchan neltotalprice)
    amountduetextbo x.Text = residentialamou ntdue.ToString( "C2")

    End If
    End If

    If businessbutton. Checked Then

    Dim businessnumbero fchannels As Integer
    Dim businessnumbero fconnections As Integer
    Dim businesschannel totalprice As Decimal
    Dim businessconnect ionstotalprice As Decimal
    Dim businessamountd ue As Decimal
    Dim channelprice As Decimal
    Dim isconverted5 As Boolean
    Dim isconverted6 As Boolean
    Dim processingfee As Decimal
    Dim basicfee As Decimal
    Dim connectionprice As Decimal
    isconverted5 = Integer.TryPars e(connectionsqu antitytextbox.T ext, businessnumbero fconnections)
    isconverted6 = Integer.TryPars e(premiumquanti tytextbox.Text, businessnumbero fchannels)

    If isconverted5 = True Then
    processingfee = 16.5
    basicfee = 80
    channelprice = 50
    connectionprice = 4
    businessconnect ionstotalprice = businessnumbero fconnections * 4
    businessnumbero fconnections = connectionsquan titytextbox.Tex t
    channelpricetex tbox.Text = channelprice.To String("C2")
    connectionstext box.Text = businessconnect ionstotalprice. ToString("C2")
    TextBox4.Text = connectionprice .ToString("C2")
    processingtextb ox.Text = processingfee.T oString("C2")
    basicfeetextbox .Text = basicfee.ToStri ng("C2")
    businesschannel totalprice = businessnumbero fchannels * 50
    channeltextbox. Text = businesschannel totalprice.ToSt ring("C2")
    businessnumbero fchannels = premiumquantity textbox.Text
    businessamountd ue = (businesschanne ltotalprice + processingfee + basicfee + businessconnect ionstotalprice)
    amountduetextbo x.Text = businessamountd ue.ToString("C2 ")

    End If
    End If


    End Sub


    Private Sub exitbutton_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles Exitbutton.Clic k
    Me.Close()
    End Sub

    Private Sub clearbutton_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles clearbutton.Cli ck
    processingtextb ox.Text = String.Empty
    basicfeetextbox .Text = String.Empty
    connectionsquan titytextbox.Tex t = String.Empty
    connectionstext box.Text = String.Empty
    premiumquantity textbox.Text = String.Empty
    channelpricetex tbox.Text = String.Empty
    channeltextbox. Text = String.Empty
    amountduetextbo x.Text = String.Empty
    TextBox4.Text = String.Empty
    End Sub


    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

    End Sub
    End Class[/code]
    Last edited by debasisdas; Nov 4 '07, 10:34 AM. Reason: Formatted using code tags.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Functions are created mainly to return a value. If you want only a sub routine and have nothing to return then procedure is better.

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Originally posted by debasisdas
      Functions are created mainly to return a value. If you want only a sub routine and have nothing to return then procedure is better.
      Something that always astounds me with computer courses (at least in my experience) is that they never really teach code reuse and efficiency of code structure...it' s like they put a bunch of concepts out there and tell the students to get on with it. My university course was the exact same way all those years ago - and I've had a lot of on the job learning to do since then.

      Obviously you're aware that a sub(routine) (which equates to a procedure) is just a code block that doesn't need to return any values. A function is similar except it returns values.
      Code:
       
      Sub MyDemoSub(ByVal Param1 As String, ByVal Param2 As String)
      'Do something that requires knowing Param1 and Param2 to
      'achieve the objective. 
      End Sub
       
      Function MyDemoFunction(ByVal Param1 As String, ByVal Param2 As String) As Boolean
      'Do something that requires knowing Param1 and Param2 to
      'achieve the objective and return some value.
      Return Param1 = Param2
      End Function
      As you can see, the demo function just compares the values passed into parameter 1 and parameter 2 and if they are the same, the function returns the value true. Obviously your subs and functions can have as few or as many parameters as you need - but the purpose of them is to allow code re-use...the ability to run through a procedure with a single line of code.
      Code:
      Dim MyValue1 As String = "Hello" 
      Dim MyValue2 As String = "World"
      'We want to see if the values in MyValue1 and MyValue2 are the same
      Dim MyBoolean As Boolean = MyDemoFunction(MyValue1, MyValue2)
      'At this point MyBoolean = False
      Now for the purpose of this demo, the code example is actually longer than the code it would've taken to do:
      Dim MyBool As Boolean = (MyValue1 = MyValue2)
      But you can easily see that any procedure as complex as needed could be contained within the bounds of the function and any data types could be passed as parameters and any data types could be returned.

      It would be fairly simple to include a few functions - I'll start you off with one very basic one - you would probably want something a little more complex to make your code more usable but this will give you an idea of what you're looking for:
      Code:
      Function ResidentialTotal(ByVal NumberOfChannels As Integer, ByVal ChannelPrice As Decimal) As Decimal
        Return NumberOfChannels * ChannelPrice 
      End Function
      You would call this from your main procedure in the following manner:
      Code:
      residentialchanneltotalprice = ResidentialTotal(residentialnumberofchannels, channelprice)
      I started you off with such a simple one because as this is an assignment you should really be learning the benefit of this type of code structure so you should really get to grips with using it. Using that example directly in your code probably won't earn you the best marks because it uses more code than your original - it just demonstrates the concept.

      Comment

      • Michelle Monty
        New Member
        • Oct 2007
        • 24

        #4
        Thanks, that helped a lot.


        Originally posted by balabaster
        Something that always astounds me with computer courses (at least in my experience) is that they never really teach code reuse and efficiency of code structure...it' s like they put a bunch of concepts out there and tell the students to get on with it. My university course was the exact same way all those years ago - and I've had a lot of on the job learning to do since then.

        Obviously you're aware that a sub(routine) (which equates to a procedure) is just a code block that doesn't need to return any values. A function is similar except it returns values.
        Code:
         
        Sub MyDemoSub(ByVal Param1 As String, ByVal Param2 As String)
        'Do something that requires knowing Param1 and Param2 to
        'achieve the objective. 
        End Sub
         
        Function MyDemoFunction(ByVal Param1 As String, ByVal Param2 As String) As Boolean
        'Do something that requires knowing Param1 and Param2 to
        'achieve the objective and return some value.
        Return Param1 = Param2
        End Function
        As you can see, the demo function just compares the values passed into parameter 1 and parameter 2 and if they are the same, the function returns the value true. Obviously your subs and functions can have as few or as many parameters as you need - but the purpose of them is to allow code re-use...the ability to run through a procedure with a single line of code.
        Code:
        Dim MyValue1 As String = "Hello" 
        Dim MyValue2 As String = "World"
        'We want to see if the values in MyValue1 and MyValue2 are the same
        Dim MyBoolean As Boolean = MyDemoFunction(MyValue1, MyValue2)
        'At this point MyBoolean = False
        Now for the purpose of this demo, the code example is actually longer than the code it would've taken to do:
        Dim MyBool As Boolean = (MyValue1 = MyValue2)
        But you can easily see that any procedure as complex as needed could be contained within the bounds of the function and any data types could be passed as parameters and any data types could be returned.

        It would be fairly simple to include a few functions - I'll start you off with one very basic one - you would probably want something a little more complex to make your code more usable but this will give you an idea of what you're looking for:
        Code:
        Function ResidentialTotal(ByVal NumberOfChannels As Integer, ByVal ChannelPrice As Decimal) As Decimal
          Return NumberOfChannels * ChannelPrice 
        End Function
        You would call this from your main procedure in the following manner:
        Code:
        residentialchanneltotalprice = ResidentialTotal(residentialnumberofchannels, channelprice)
        I started you off with such a simple one because as this is an assignment you should really be learning the benefit of this type of code structure so you should really get to grips with using it. Using that example directly in your code probably won't earn you the best marks because it uses more code than your original - it just demonstrates the concept.

        Comment

        Working...