I'm creating a sales commission application. I have to use a Select....Case statement to implement the sales commission schedule. When the calculate button is clicked, three methods should be called. One to calculate gross sales, one to calcuate the commission percentage based on the commission schedule and one to calculate the sale's persons earnings. When i press the calculate button, my gross sales display, but the sales persons commission percentage and the sales persons earnings just display as zero....

Public Class SalesCommission

    'stores total of sales person earnings
    Dim salesEarnings As Decimal
    Dim commTotal As Decimal
    Dim items As Integer

    Private Property grossSales As Integer

    Private Property commpercentage As String

    'calculate and display number of items sold
    Public Sub calculateButton_Click(ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles calculateButton.Click

        'get number of items
        items = Convert.ToInt16(Val(ItemsSoldTextBox.Text))

        'Obtain gross sales
        Dim grossSales As Decimal = CalculateItems()
        Dim commTotal As Decimal = CommissionTotal()
        Dim commPercentage As Decimal = commPerc()

        ' display gross sales
        grossOutputLabel.Text = String.Format("{0:C}", grossSales)

        ' display total commission
        salesOutputLabel.Text = String.Format("{0:C}", commTotal)

        ' display commission percentage
        commOutputLabel.Text = String.Format(commPercentage & "%")

    End Sub

    ' calculates gross sales
    Function CalculateItems() As String
        grossSales = items * 10
        Return grossSales
    End Function

    ' Commission Percentage
    Function commPerc() As String

        Select Case commpercentage
            Case 0 To 50
                commpercentage = 6
            Case 51 To 100
                commpercentage = 7
            Case 101 To 150
                commpercentage = 8
            Case Is >= 151
                commpercentage = 9
        End Select

        Return commpercentage

    End Function

    Function CommissionTotal() As String
        commTotal = (commpercentage * grossSales) / 100
        Return commTotal
    End Function

 
End Class

There are a few things wrong here. The first thing is that your function return values do not agree in type with the values you are returning. Let's take

Function CommissionTotal() As String
    commTotal = (commpercentage * grossSales) / 100
    Return commTotal
End Function

You declared the function as returning a String but you are returning commTotal which is declared as Decimal. Let's not overlook the fact that you have declared commTotal as both a global (line 5) and a local (line 21). You set the global value in the above function then set the local as the function result but you are assigning it a String value when it is a Decimal (aaaarggghh).

You calculate commTotal which uses commpercentage (which hasn't been calculated yet). That's like saying

Dim x,y as Integer

x = y + 5
y = 17

Kind of putting the cart before the horse. You are calculating commPercentage based on the value of commPercentage. I presume this should be something more along the lines of

Select case items

Can you please describe the problem that was given to you rather than describing the code you have written and I'll walk you through it.

A couple of more comments:

When you define a function such as CalculateItems, try to give it a meaningful rather than a generic name. CalculateItems imparts almost zero information. The comment you included with it, "calculates gross sales" gives a little information but could be clearer as in "calculates gross sales as the product of items sold and something else". I don't know what that somethinig else is because you used a literal, 10.

Which brings me to my next point. Literals in almost all cases are bad. If I see a literal like 3.1415926535 in a calculation, especially one involving circles or spheres, I'm pretty sure I can figure out what it is. The literal, 10, on the other hand is essentially meaningless. You'd be better off with

Const DESCRIPTIVE_NAME = 10

You'll find this more useful later when you need the same literal in several places.

If I'm being picky it's because I assume this is for an assignment. I've been through that from both sides, as a student (a very long time ago), a marker (not quite as long but still a long time) and as a maintenance programmer where I have come close to tears and/or rage at some of the messes I've had to clean up. Better to beat it out of you now ;-)

commented: True. +8

I appreciate the criticsm, it is how we learn to do it right. I appreciate your help. I got it working. This is the code. I hope it's not to ugly!

'Ex. 6.10 Sales Commission
Public Class SalesCommission

    'stores total of sales person earnings

    Dim commPercentage As Integer


    'calculate and display number of items sold
    Public Sub calculateButton_Click(ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles calculateButton.Click

        'get number of items
        Dim itemsSold As Integer = Convert.ToInt16(Val(ItemsSoldTextBox.Text))
        Dim commItems As Integer = Convert.ToInt16(Val(ItemsSoldTextBox.Text))

        Select Case commItems
            Case 0 To 50
                commPercentage = 6
            Case 51 To 100
                commPercentage = 7
            Case 101 To 150
                commPercentage = 8
            Case Is >= 151
                commPercentage = 9

        End Select


        'Obtain gross sales
        Dim grossSales As Decimal = CalculateItems(itemsSold)
        Dim commTotal As Decimal = CommissionTotal(commItems)

        ' display gross sales
        grossOutputLabel.Text = String.Format("{0:C}", grossSales)

        ' display total commission
        salesOutputLabel.Text = String.Format("{0:C}", commTotal)

        ' display commission percentage
        commOutputLabel.Text = String.Format(commPercentage & "%")

    End Sub

    ' calculates gross sales
    Function CalculateItems(ByVal itemTotal As Decimal) As Decimal
        Const COST As Decimal = 10
        Return itemTotal * COST
    End Function

    ' Commission Percentage
    Function CommissionTotal(ByVal commAmount As Integer) As Integer
        Return commPercentage
    End Function

    Function CommissionTotal(ByVal grossSales As Decimal, ByVal commPercentage As Integer) As Decimal
        Return (commpercentage * grossSales) / 100

    End Function

    'clear text boxes
    Private Sub itemsSoldTextBox_TextChanged(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles ItemsSoldTextBox.TextChanged

        grossOutputLabel.Text = String.Empty
        commOutputLabel.Text = String.Empty
        salesOutputLabel.Text = String.Empty
    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.