CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2015
    Posts
    8

    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:  Untitled.jpg
Views: 309
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
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

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

    Well you are not passing any values to your function nor are you assigning a return value so it is basically going to be multiplying 0*0 and returning 0 which is the default value for a decimal variable.

    decTotal is also never assigned a value so it is always going to be 0 even if the function were working.

    Don't use Call

    A function is supposed to return a value and you need to get that value when it is returned
    Code:
    Dim MyValue as Integer
    MyValue=SomeFunction(SomeValue1, SomeValue2)
    
    Private Function SomeFunction(Value1 as Integer, Value2 as Integer) as Integer
       return value1*value2
    End Function
    Last edited by DataMiser; April 2nd, 2015 at 07:26 PM.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Mar 2015
    Posts
    8

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

    ok I redid it and still having problems can you give me one more hint? my brain hurts
    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

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

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

    Always use [code][/code] tags when posting code.

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

Featured