Results 1 to 10 of 10

Thread: Can anyone help me to figure out why my function wont work

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Can anyone help me to figure out why my function wont work

    I am having issues with a function. I need to write a function so that when a customer checks the business or residential box in my program it will calculate the rates. I declared my constants and dim statement for the choices. I could not get my function to work any tips would be appreciated. attached is a picture of the layout and specific code

    Name:  auum3r.jpg
Views: 193
Size:  29.6 KB


    Code:
     Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            If Data_Validated_ok() = False Then Exit Sub
    
    
            Const decResidentialProcess_Fee As Decimal = CDec(4.5)
            Const intResidentialBasicService_Fee As Integer = 30
            Const intResidentialPremiumChannel_Fee As Integer = 5
            Const decBusinnesProcess_Fee As Decimal = CDec(16.5)
            Const intBusinessBasicService_Fee As Integer = 80
            Const intBusinessPremiumChannel_Fee As Integer = 50
    
            Dim decPremium As Decimal
            Dim decConnections As Decimal
            Dim decTotal As Decimal
    
            If radBusiness.Checked = True Then
                Call CalcBusinessGross1()
                lblTotal.Text = decTotal.ToString("C2")
            End If
    
    
     End Sub
        Private Function CalcBusinessGross1 As Decimal (decPremiumchannels As Decimal, decConnectionsrate As Decimal, ByRef decTotaldue As Decimal)
            decTotaldue = decConnectionsrate * decPremiumchannels
    
        End Function

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,678

    Re: Can anyone help me to figure out why my function wont work

    You are not returning anything in your function. Add this line at the end of your function:
    Code:
    Return decTotaldue
    Edit - I reread your function and you have the syntax wrong as well:
    Code:
    Private Function CalcBusinessGross1 (decPremiumchannels As Decimal, decConnectionsrate As Decimal, ByRef decTotaldue As Decimal) As Decimal
        Return decConnectionsrate * decPremiumchannels
    End Function

  3. #3
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Can anyone help me to figure out why my function wont work

    Quote Originally Posted by karodhill View Post
    I am having issues with a function. I need to write a function so that when a customer checks the business or residential box in my program it will calculate the rates. I declared my constants and dim statement for the choices. I could not get my function to work any tips would be appreciated. attached is a picture of the layout and specific code

    Name:  auum3r.jpg
Views: 193
Size:  29.6 KB


    Code:
     Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            If Data_Validated_ok() = False Then Exit Sub
    
    
            Const decResidentialProcess_Fee As Decimal = CDec(4.5)
            Const intResidentialBasicService_Fee As Integer = 30
            Const intResidentialPremiumChannel_Fee As Integer = 5
            Const decBusinnesProcess_Fee As Decimal = CDec(16.5)
            Const intBusinessBasicService_Fee As Integer = 80
            Const intBusinessPremiumChannel_Fee As Integer = 50
    
            Dim decPremium As Decimal
            Dim decConnections As Decimal
            Dim decTotal As Decimal
    
            If radBusiness.Checked = True Then
                Call CalcBusinessGross1()
                lblTotal.Text = decTotal.ToString("C2")
            End If
    
    
     End Sub
        Private Function CalcBusinessGross1 As Decimal (decPremiumchannels As Decimal, decConnectionsrate As Decimal, ByRef decTotaldue As Decimal)
            decTotaldue = decConnectionsrate * decPremiumchannels
    
        End Function
    Why would you expect this to work at all? You dont pass any parameters and your function returns no value. Your function call is also incorrect as those are not optional parameters and the entire line is not in the correct syntactical form to make the call.


    Code:
        Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
    
            If Not Data_Validated_ok() Then Exit Sub
    
            Const decResidentialProcess_Fee As Decimal = 4.5
            Const intResidentialBasicService_Fee As Integer = 30
            Const intResidentialPremiumChannel_Fee As Integer = 5
            Const decBusinnesProcess_Fee As Decimal = 16.5
            Const intBusinessBasicService_Fee As Integer = 80
            Const intBusinessPremiumChannel_Fee As Integer = 50
    
            Dim decPremium As Decimal = 15
            Dim decConnections As Decimal = 20
    
    
            If radBusiness.Checked Then
                Dim decTotal As Decimal = CalcBusinessGross1(decPremium, decConnections)
                lblTotal.Text = decTotal.ToString("C2")
            End If
    
    
        End Sub
        Private Function CalcBusinessGross1(ByVal decPremiumchannels As Decimal, ByVal decConnectionsrate As Decimal) As Decimal
            Return (decPremiumchannels * decConnectionsrate)
        End Function
    I don't see a point to that function unless you plan to reuse it or there will be a ton of calls later to it as its a pretty simple calculation

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Can anyone help me to figure out why my function wont work

    ok I studied and retweeked it and now it still doesn't work. Smh
    Code:
    Option Strict On
    
    
    
    
    Public Class Main
        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            If Data_Validated_ok() = False Then Exit Sub
    
            Dim charge As Decimal
            Dim premium As Decimal
            Dim connection As Decimal
            Dim servicefee As Decimal
            Dim processingfee As Decimal
            Dim totaldue As Decimal
            If radResidential.Checked = True Then
                processingfee = 4.5D
                servicefee = 30D
                totaldue = calcResidentialTotal(charge, premium)
            Else
                processingfee = 16.5D
                servicefee = 80D
    
    
                totaldue = calcBusinessTotal(charge, connection)
            End If
    
    
    
        End Sub
        Private Function calcResidentialTotal(ByVal connections As Decimal, ByVal premium As Decimal) As Decimal
            ' Residential customer bill
    
            Dim Premium_Channels As Decimal
            Dim Charge As Decimal
    
    
            Premium_Channels = Convert.ToDecimal(lstPremium.SelectedItem)
            connections = Convert.ToDecimal(lstConnections.SelectedItem)
            ' five dollars per premium channel
            Charge = Premium_Channels * 5
            Return Charge
        End Function
        Private Function calcBusinessTotal(ByVal connections As Decimal, ByVal premium As Decimal) As Decimal
            ' business customer bill
    
            Const basicfee As Decimal = 80
            Const basicadd As Decimal = 4
            Dim businessbasic As Decimal
            Dim charge As Decimal
            Dim premiumchannels As Decimal
            connections = Convert.ToDecimal(lstConnections.SelectedItem)
            premiumchannels = Convert.ToDecimal(lstPremium.SelectedItem)
    
            If connections <= 10 Then
                businessbasic = basicfee
            Else
                businessbasic = basicfee + basicadd
            End If
            charge = (basicfee * basicadd) +
                premiumchannels
    
            Return charge
        End Function

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,281

    Re: Can anyone help me to figure out why my function wont work

    totalDue is a local variable that is getting the return value from your function but then you do not do anything with it. I assume you meant to display that in a label or a textbox somewhere.

    Also for future reference you should state what is or is not happening and/or any error messages you are getting and where they occur. Simply saying it doesn't work does not tell us much and makes it harder to help you.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Can anyone help me to figure out why my function wont work

    Quote Originally Posted by DataMiser View Post
    totalDue is a local variable that is getting the return value from your function but then you do not do anything with it. I assume you meant to display that in a label or a textbox somewhere.

    Also for future reference you should state what is or is not happening and/or any error messages you are getting and where they occur. Simply saying it doesn't work does not tell us much and makes it harder to help you.
    I apologize for not being clear. Yes I wanted totaldue to appear in a textbox. When I hit calculate nothing appears. I don't understand what you mean when you say im not doing anything with totaldue.I am new to functions but I thought when you say a= b+c that a would be the return

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,955

    Re: Can anyone help me to figure out why my function wont work

    Right... a is the return... but then unless youy DISPLAY it somehow, it never gets seen. PRogramming isn't magic (although, let's all admit, we pretend it is sometimes) ... so you assigned the result to totalDue ... now what? do you want it to display in a textbox? Or a MessageBox? Or store it in a database? Or ... ??? What do you want to do with it? You have to write more code to tell the program what to do with it.

    Consider this: a magician comes up to you fans out a deck of cards, tells you to pick one, which you do.... then walks away... you're reaction would be "ooooh kaaaaay.... now what?" That's exactly what's happening here. You picked out a card (captured the return value from your funcitons) ... ok, now what?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    9

    Re: Can anyone help me to figure out why my function wont work

    Quote Originally Posted by techgnome View Post
    Right... a is the return... but then unless youy DISPLAY it somehow, it never gets seen. PRogramming isn't magic (although, let's all admit, we pretend it is sometimes) ... so you assigned the result to totalDue ... now what? do you want it to display in a textbox? Or a MessageBox? Or store it in a database? Or ... ??? What do you want to do with it? You have to write more code to tell the program what to do with it.

    Consider this: a magician comes up to you fans out a deck of cards, tells you to pick one, which you do.... then walks away... you're reaction would be "ooooh kaaaaay.... now what?" That's exactly what's happening here. You picked out a card (captured the return value from your funcitons) ... ok, now what?


    -tg
    I said I wanted it in a textbox but ok I will figure this out on my own sorry that I was unclear but the sarcasm is getting me nowhere. thanks all I will figure this out like I always do

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,281

    Re: Can anyone help me to figure out why my function wont work

    Quote Originally Posted by karodhill View Post
    I apologize for not being clear. Yes I wanted totaldue to appear in a textbox.
    which means you have to assign that to the text property of the desired text box.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,955

    Re: Can anyone help me to figure out why my function wont work

    Quote Originally Posted by karodhill View Post
    I said I wanted it in a textbox but ok I will figure this out on my own sorry that I was unclear but the sarcasm is getting me nowhere. thanks all I will figure this out like I always do
    IT wasnt sarcasm, but an attempt at humor... you told US where you wanted it to go (the textbox) but you didn't tell the program that... that was the point I was trying to drive home. I don't believe in just handing out code, I'd prefer to hand out a roadmap and let you figure it out. Silly me (now that was sarcasm).

    Since the roadmap was insufficient for your needs, here's the code:
    Code:
    Textbox1.Text = totalDue.ToString("c")
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width