-
Apr 2nd, 2015, 12:01 PM
#1
Thread Starter
New Member
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
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
-
Apr 2nd, 2015, 12:37 PM
#2
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:
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
-
Apr 2nd, 2015, 12:42 PM
#3
Re: Can anyone help me to figure out why my function wont work
Originally Posted by karodhill
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
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
-
Apr 3rd, 2015, 12:36 AM
#4
Thread Starter
New Member
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
-
Apr 3rd, 2015, 01:07 AM
#5
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.
-
Apr 3rd, 2015, 11:56 AM
#6
Thread Starter
New Member
Re: Can anyone help me to figure out why my function wont work
Originally Posted by DataMiser
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
-
Apr 3rd, 2015, 12:21 PM
#7
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
-
Apr 3rd, 2015, 10:23 PM
#8
Thread Starter
New Member
Re: Can anyone help me to figure out why my function wont work
Originally Posted by techgnome
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
-
Apr 3rd, 2015, 11:23 PM
#9
Re: Can anyone help me to figure out why my function wont work
Originally Posted by karodhill
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.
-
Apr 4th, 2015, 12:07 AM
#10
Re: Can anyone help me to figure out why my function wont work
Originally Posted by karodhill
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
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
|