Results 1 to 27 of 27

Thread: Help with Vb program

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Help with Vb program

    Im currently taking a Vb class and am having difficulty writing a function portion of this assignment. is it possible that someone could help me?

    A salesperson earns a weekly base salary plus a commission when sales
    are at or above quota. Create a project that allows the user to input the
    weekly sales and the salesperson name. calculates the commission. and
    displays summary information.
    Form: The form should have text boxes for the sales person's name and his
    or her weekly sales.
    Use constants to establish the base pay, the quota, and the commission
    rate.
    The Pay menu item calculates and displays the commission and the total
    pay for that person. However. if there is no commission. do not display
    the commission amount (do not display a zero-commission amount).
    Write a function procedure to calculate the commission. The function
    must compare sales to the quota. When the sales a re equal to or greater
    than the quota, calculate the commission by multiplying sales by the
    commission rate.
    Each sales person receives the base pay plus the commission (if one
    has been earned). Formal the dollar amounts to two decimal places; do
    not display a dollar sign.
    The Summary menu item displays a message box that holds total sales,
    total commissions. and total pay for all salespersons. Display the numbers
    with two decimal places and dollar signs.

    Test Data: Quota = 1000: Commission rate = .15: and Base pay = $250

    I have screen shots of the code i have so far
    Attached Images Attached Images   

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help with Vb program

    Please don't use screen shots of code. They're virtually unreadable and, of course completely useless for copying and editing. If there is a relevant piece of code please give it in text, preferably formatted using the tags.

    Also please give us the specific problem you are having not just a blanket coverage of the project. If there are errors then please give the full error message and indicate which line it occurs on. If something doesn't do what you expect it to please give the relevant code and both the results you want and the results you get.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    ok I will edit the post

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    The issue i am having for my project is writing the code and function to calculate information. I have to calculate the commission and the total pay for a sales person. However if there is no commission, do not display the commission amount.
    The code I have for this portion is as follows:
    Dim SaleDecimal, CommissionDecimal, PayDecimal, TotalPayDecimal As Decimal
    SaleDecimal = Decimal.Parse(SaleTextBox.Text)

    CommissionDecimal = SaleDecimal * COMMISSION_RATE_Decimal
    PayDecimal = (CommissionDecimal + BasePay_Decimal)
    TotalPayDecimal += PayDecimal

    CommissionTextBox.Text = CommissionDecimal.ToString("C")
    IndPayTextBox.Text = PayDecimal.ToString("C")
    PayTextBox.Text = TotalPayDecimal.ToString("C")


    Additionally, i have to write a function that must compare sales to the quote. when the sales are equal to or greater than the quota, calculate the commission by multiplying sales by the commission. The constants are quota = 1000, commission rate .15 and base bay $250. (I'm currently stuck and confused as how to write this out)

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    For the Function portion of the project I getting the error "Statement is not valid in namespace for the first line"

    Private Function Commission(ByVal SalesDecimal As Decimal) As Decimal
    If SalesAmountDecimal < QUOTA_Decimal Then
    Commission = OD
    Elself SalesAmountDecimal <= QUOTA_Decimal Then
    Commission(0.15 * SalesDecimal)
    Else
    Commission()
    End If
    End Function

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Help with Vb program

    Where do you have that function? The error suggests that it is in the wrong place. It appears that it should just be a function in the class, but it also brings up a question as to where the other chunk of code is located.

    A couple points, though:

    1) Format the code by wrapping it in code tags. You can do this by editing the post, going Advanced, select the code and press the CODE button. Alterantively, you could add the tags by hand with CODE and \CODE, each wrapped in square braces. That will make it look nicer and is easier to read.

    2) Decimal.Parse is not the way to convert user input, as it will throw an exception if the value can't be converted. Instead, look into .TryParse, which is the right method, but isn't just a drop in replacement for Parse, as it works differently.

    3) In the Commission method you are calling Commission with no argument. That should cause an error unless Commission is overloaded in some odd way. If it is, it probably isn't done on purpose.

    EDIT: And right before you call Commission with no arguments, you call it this way:

    Commission(0.15 * SalesDecimal)

    which ends up being a recursive call that will crash your program in short order when the stack overflows.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    Quote Originally Posted by Shaggy Hiker View Post

    1) Format the code by wrapping it in code tags. You can do this by editing the post, going Advanced, select the code and press the CODE button. Alterantively, you could add the tags by hand with CODE and \CODE, each wrapped in square braces. That will make it look nicer and is easier to read.
    I am using microsoft visual basic 2010 express and i am not able to do that.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Help with Vb program

    Quote Originally Posted by vltor View Post
    I am using microsoft visual basic 2010 express and i am not able to do that.
    The code tags have nothing to do with vb 2010. It has to do with this forum. In your last post there were quote tags. It had QUOTE wrapped with []. Do the same thing with your code:
    Code:
     CommissionDecimal = SaleDecimal * COMMISSION_RATE_Decimal
     PayDecimal = (CommissionDecimal + BasePay_Decimal)
     TotalPayDecimal += PayDecimal
    
     CommissionTextBox.Text = CommissionDecimal.ToString("C")
     IndPayTextBox.Text = PayDecimal.ToString("C")
     PayTextBox.Text = TotalPayDecimal.ToString("C")
    
    Private Function Commission(ByVal SalesDecimal As Decimal) As Decimal
     If SalesAmountDecimal < QUOTA_Decimal Then
     Commission = OD
     Elself SalesAmountDecimal <= QUOTA_Decimal Then
     Commission(0.15 * SalesDecimal)
     Else
     Commission()
     End If
     End Function
    If you're still unsure on how to do that, in the quick reply click on the button to the left of the ABC with the check mark. That generates the quote tags. Replace the word QUOTE with CODE.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    Quote Originally Posted by Shaggy Hiker View Post
    Where do you have that function? The error suggests that it is in the wrong place. It appears that it should just be a function in the class, but it also brings up a question as to where the other chunk of code is located.
    here is the entire code.

    Code:
    Public Class SalaryCalcForm
        'Decalare module-level variables
        Private CommissionDecimal, PayDecimal, TotalPayDecimal As Decimal
        'Declare constants.
        Const COMMISSION_RATE_Decimal As Decimal = 0.15D
        Const QUOTA_Decimal As Decimal = 1000D
        Const BasePay_Decimal As Decimal = 250D
    
        Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
            'Display the About message box.
            Dim MessageString As String
            MessageString = "Salary Calculator" & Environment.NewLine & "Programmed by Andy Wong"
            MessageBox.Show(MessageString, "About Salary Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub
    
        Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
            'Allow the user to select a new font for the total pay text box.
    
            With FontDialog1
                .Font = PayTextBox.Font
                .ShowDialog()
                PayTextBox.Font = .Font
            End With
        End Sub
    
        Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
            'Allow the user to select a new color for the text of the total pay text box
    
            With ColorDialog1
                .ShowDialog()
                PayTextBox.ForeColor = .Color
            End With
        End Sub
    
        Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
            'Clears all data input.
    
            SaleTextBox.Clear()
            CommissionTextBox.Clear()
            PayTextBox.Clear()
            With NameTextBox
                .Clear()
                .Focus()
            End With
        End Sub
    
        Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
            'Exits the program
    
            Me.Close()
        End Sub
    
        Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click
            'Calculates and displays the commission and the total pay.
            Dim SaleDecimal, CommissionDecimal, PayDecimal, TotalPayDecimal As Decimal
            SaleDecimal = Decimal.Parse(SaleTextBox.Text)
    
            CommissionDecimal = SaleDecimal * COMMISSION_RATE_Decimal
            PayDecimal = (CommissionDecimal + BasePay_Decimal)
            TotalPayDecimal += PayDecimal
    
            CommissionTextBox.Text = CommissionDecimal.ToString("C")
            IndPayTextBox.Text = PayDecimal.ToString("C")
            PayTextBox.Text = TotalPayDecimal.ToString("C")
        End Sub
    End Class
    Private Function Commission(ByVal SalesDecimal As Decimal) As Decimal
        If SalesAmountDecimal < QUOTA_Decimal Then
            Commission = OD
    Elself SalesAmountDecimal <= QUOTA_Decimal Then
            Commission(0.15 * SalesDecimal)
        Else
            Commission()
        End If
    End Function

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help with Vb program

    Move End Class to be the last line!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    That does not fix the problem I get more errors when i do that

  12. #12
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Help with Vb program

    I'm sure you do. Nevertheless that's where End Class belongs. Now you can set about solving the remaining problems.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Help with Vb program

    Quote Originally Posted by vltor View Post
    That does not fix the problem I get more errors when i do that
    What errors do you get now?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  14. #14

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    Code:
    Public Class SalaryCalcForm
        'Decalare module-level variables
        Private CommissionDecimal, PayDecimal, TotalPayDecimal As Decimal
        'Declare constants.
        Const COMMISSION_RATE_Decimal As Decimal = 0.15D
        Const QUOTA_Decimal As Decimal = 1000D
        Const BasePay_Decimal As Decimal = 250D
    
        Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
            'Display the About message box.
            Dim MessageString As String
            MessageString = "Salary Calculator" & Environment.NewLine & "Programmed by Andy Wong"
            MessageBox.Show(MessageString, "About Salary Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub
    
        Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
            'Allow the user to select a new font for the total pay text box.
    
            With FontDialog1
                .Font = PayTextBox.Font
                .ShowDialog()
                PayTextBox.Font = .Font
            End With
        End Sub
    
        Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
            'Allow the user to select a new color for the text of the total pay text box
    
            With ColorDialog1
                .ShowDialog()
                PayTextBox.ForeColor = .Color
            End With
        End Sub
    
        Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
            'Clears all data input.
    
            SaleTextBox.Clear()
            CommissionTextBox.Clear()
            PayTextBox.Clear()
            With NameTextBox
                .Clear()
                .Focus()
            End With
        End Sub
    
        Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
            'Exits the program
    
            Me.Close()
        End Sub
    
        Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click
            'Calculates and displays the commission and the total pay.
            Dim SaleDecimal, CommissionDecimal, PayDecimal, TotalPayDecimal As Decimal
            SaleDecimal = Decimal.Parse(SaleTextBox.Text)
    
            CommissionDecimal = SaleDecimal * COMMISSION_RATE_Decimal
            PayDecimal = (CommissionDecimal + BasePay_Decimal)
            TotalPayDecimal += PayDecimal
    
            CommissionTextBox.Text = CommissionDecimal.ToString("C")
            IndPayTextBox.Text = PayDecimal.ToString("C")
            PayTextBox.Text = TotalPayDecimal.ToString("C")
        End Sub
    Private Function Commission(ByVal SalesDecimal As Decimal) As Decimal
        If SalesAmountDecimal < QUOTA_Decimal Then
            Commission = OD
    Elself SalesAmountDecimal <= QUOTA_Decimal Then
            Commission(0.15 * SalesDecimal)
        Else
            Commission()
        End If
    End Function
    End Class
    notice the end class is on the last line. i get the following errors listed in the picture.
    Attached Images Attached Images  

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Help with Vb program

    The same thing as in post #2, don't post a screenshot of the error. Copy and paste the error 's you're getting, because I can't even read the descriptions, let alone the file name.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  16. #16

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    to view the text in the picture you can press ctrl and the + buttons together to increase the zoom. I was able to read the text through this method.

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Help with Vb program

    Well, I didn't want to have to go through that trouble... but... for error 1, simply declare SalesDecimalAmount and OD at the form level, removing them where ever else they're declared. Errors 3,4, and 6 should go away when you do that. Error 7, your logic is broken. Commission is a function that returns a decimal, but you're trying to call it as if it's a sub. What I would do is take a look at this site, specifically chapter 10, to get a better understanding of subs vs. functions.

    Edit -
    By the way, next time you should wrap any errors in code or quote tags... That away it's easier to read for those that want to help.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Help with Vb program

    OD is probably zeroD, where you were trying to enter 0 as a decimal type. That D isn't necessary, since the variable is a Decimal type, so anything in it will also be a Decimal type.

    However, the function is pretty thoroughly hosed due to the things I mentioned in #3. Once you fix all the errors, it will do one of two things: Nothing, or crash. You are calling the function itself from within the function in the first else. That's a recursive call, which isn't necessarily bad to do, as long as it ends somewhere, but this one won't. It will keep recursing until the stack overflows. If you had a multiplier greater than 1, it might also exceed the size of Decimal prior to the stack overflowing, but since your multiplier is less than 1, it will converge on zero and crash when the stack overflows.

    Furthermore, in the final else, you appear to call the method without an argument, which is what error #7 is talking about. If you gave it an argument, it would still crash, so making the error go away isn't a solution.

    Essentially, the function started off well, since you checked for the condition where you should be returning 0, and did. As a general rule, in .NET, people don't return values by assigning them to the name of the method. That was how it was done in classic VB, and it still works in .NET, but people don't do that. Instead, just use the Return statement, so you would just Return 0. After that first check, the rest is problematic. In the first else, I think you wanted to return 1.5 * SalesDecimal, and you just called the method rather than assigning it, which is a good reason to use the Return rather than assigning to the method name.

    I'm not sure what you wanted to do with the third condition. I didn't read the problem in sufficient detail I suppose, but I thought that you either got a commission or not. I don't think you even want that Else in there.

    There is one other problem. The first condition checks for whether the value is less than the quota, but the second condition also checks whether the value is less than (or equal to) the quota. So, you need to reverse the sign on that one.
    My usual boring signature: Nothing

  19. #19
    Hyperactive Member
    Join Date
    May 2009
    Posts
    274

    Re: Help with Vb program

    IMO you seem to have massively over complicated your solution to the problem.

    First off, as the task in hand is purely to take a text input for the salesperson's name and their weekly sales to work out whether or not they get any commission and to display the data.

    As it has already been stated, declare all your variables and Consts outside of any function (i.e just below your "Public Class" line and before any other code)

    Then have you put your calculate function.

    Within the function all you need to do is take the input from the total sales textbox and compare it to the sales quota variable. The key to this is in the task "When the sales are equal to or greater than the quota, calculate the commission by multiplying sales by the commission rate", use either an If statement or a Select Case statement that evaluates this.

    This bit is simple, if the total sales is equal to or greater than the sales quota the commission will be the total sales multiplied by the commission rate. If not the commission will be nothing.

    It's simple maths being put into logical programming.

  20. #20

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    I have managed to insert to create the private function but for some reason i am not getting the results i want.
    I am supposed to calculate the commission for each person. If there is no commission, do not display the commission amount. Then, write a function procedure to calculate the commission. the function must compare sales to the quota (qutoa=1000). when the sales are equal to or greater than the quota, calculate the commission by multiplying the commission rate (commission rate = .15). Each person receives the base pay plus the commission (if one has been earned). Format the dollar amounts to two decimals and do not display a dollar sign.
    i am given the following test data

    Sales
    1000.00
    999.99
    2000.00

    total should be
    $3999.99
    450.00
    1200.00

    Code:
    Public Class SalaryCalcForm
        'Decalare module-level variables
        Private CommissionDecimal, PayDecimal, TotalPayDecimal As Decimal
        'Declare constants.
        Const COMMISSION_RATE_Decimal As Decimal = 0.15D
        Const QUOTA_Decimal As Decimal = 1000D
        Const BasePay_Decimal As Decimal = 250D
    
        Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
            'Display the About message box.
            Dim MessageString As String
            MessageString = "Salary Calculator" & Environment.NewLine & "Programmed by Andy Wong"
            MessageBox.Show(MessageString, "About Salary Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Sub
    
        Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
            'Allow the user to select a new font for the total pay text box.
    
            With FontDialog1
                .Font = PayTextBox.Font
                .ShowDialog()
                PayTextBox.Font = .Font
            End With
        End Sub
    
        Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
            'Allow the user to select a new color for the text of the total pay text box
    
            With ColorDialog1
                .ShowDialog()
                PayTextBox.ForeColor = .Color
            End With
        End Sub
    
        Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
            'Clears all data input.
    
            SaleTextBox.Clear()
            CommissionTextBox.Clear()
            PayTextBox.Clear()
            With NameTextBox
                .Clear()
                .Focus()
            End With
        End Sub
    
        Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
            'Exits the program
    
            Me.Close()
        End Sub
        Private Function Commission(ByVal SalesDecimal As Decimal) As Decimal
            If SalesDecimal < QUOTA_Decimal Then
                Commission = 0D
            ElseIf SalesDecimal >= QUOTA_Decimal Then
                Commission = (0.15 * SalesDecimal)
            Else : Commission = (0.15 * SalesDecimal)
            End If
        End Function
        Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click
            'Calculates and displays the commission and the total pay.
            Dim SaleDecimal, CommissionDecimal, PayDecimal, TotalPayDecimal As Decimal
            SaleDecimal = Decimal.Parse(SaleTextBox.Text)
    
            CommissionDecimal = SaleDecimal * COMMISSION_RATE_Decimal
            PayDecimal = (CommissionDecimal + BasePay_Decimal)
            TotalPayDecimal += PayDecimal
    
            CommissionTextBox.Text = CommissionDecimal.ToString("C")
            IndPayTextBox.Text = PayDecimal.ToString("C")
            PayTextBox.Text = TotalPayDecimal.ToString("C")
        End Sub
    End Class

  21. #21

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    I forgot to clarify on something
    the totals should be the following after i have clicked the summary button.
    sales $3999.99
    commissions 450.00
    pay 1200.00

  22. #22

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    12

    Re: Help with Vb program

    I'm becoming quite desperate in completing this project, I'm willing to pay someone to help me complete this as I can send money through paypal. Please pm me if you are interested.

  23. #23
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    Re: Help with Vb program

    I made this with buttons instead of menu items. I think it works fine so far, and I'm positive it can be improved a lot (noob here too ) but anyway try to understand what the code is doing. I tried to make it as simple as possible, and like I said, I'm sure it could be done better.

    Controls and names used:
    - 2 labels
    - 2 textboxes (tbSeller and tbWeeklySales
    - 1 listbox (ListBox1)
    - 3 buttons (btnAddSeller, btnPay, btnSummary)
    - 2 groupboxes

    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class Form1
    5.  
    6.     'Constants
    7.     Const QUOTA As Double = 1000
    8.     Const COMM_RATE As Double = 0.15
    9.     Const BASE_PAY As Double = 250
    10.  
    11.     Public Function CalculateCommission(ByVal seller As Seller) As Double
    12.  
    13.         'If the seller's weekly sales are bigger or equal to the quota, calculates and returns the commission
    14.         'Else it returns 0
    15.         If seller.WeeklySales >= QUOTA Then
    16.             Dim commission As Double = seller.WeeklySales * COMM_RATE
    17.             Return commission
    18.         Else
    19.             Return 0
    20.         End If
    21.  
    22.     End Function
    23.  
    24.     Private Sub btnPay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPay.Click
    25.  
    26.         'This button displays the listbox selected seller's info on a Messagebox
    27.         Dim strMessage As String
    28.  
    29.         'If there is a selected item it proceeds, if not it shows an error message
    30.         If ListBox1.SelectedIndex > -1 Then
    31.  
    32.             Dim currentSeller As Seller = DirectCast(ListBox1.SelectedItem, Seller)
    33.  
    34.             Dim commission As Double = CalculateCommission(currentSeller)
    35.  
    36.             If commission > 0 Then
    37.                 strMessage = "Seller's name: " & currentSeller.Name & Environment.NewLine & _
    38.                              "Weekly sales: " & currentSeller.WeeklySales.ToString & Environment.NewLine & _
    39.                              "Commission: " & commission.ToString("0.00") & Environment.NewLine & _
    40.                              "Total pay: " & (BASE_PAY + commission).ToString("0.00")
    41.                 MessageBox.Show(strMessage, currentSeller.Name, MessageBoxButtons.OK, MessageBoxIcon.Information)
    42.  
    43.             Else
    44.                 strMessage = "Seller's name: " & currentSeller.Name & Environment.NewLine & _
    45.                              "Weekly sales: " & currentSeller.WeeklySales.ToString & Environment.NewLine & _
    46.                              "Total pay: " & BASE_PAY.ToString("0.00")
    47.                 MessageBox.Show(strMessage, currentSeller.Name, MessageBoxButtons.OK, MessageBoxIcon.Information)
    48.  
    49.             End If
    50.  
    51.         Else
    52.             MessageBox.Show("No seller selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand)
    53.  
    54.         End If
    55.  
    56.     End Sub
    57.  
    58.     Private Sub btnAddSeller_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSeller.Click
    59.  
    60.         Dim wsales As Double
    61.  
    62.         'If there is a seller name AND there is a valid weekly sales number,
    63.         'create a new seller, assign its Name and WeeklySales properties and add it to the listbox
    64.         If tbSeller.Text <> "" AndAlso Double.TryParse(tbWeeklySales.Text, wsales) Then
    65.  
    66.             Dim newSeller As New Seller
    67.             With newSeller
    68.                 .Name = tbSeller.Text
    69.                 .WeeklySales = wsales
    70.             End With
    71.  
    72.             ListBox1.Items.Add(newSeller)
    73.  
    74.             tbSeller.Clear()
    75.             tbWeeklySales.Clear()
    76.             tbSeller.Select()
    77.  
    78.         Else
    79.             MessageBox.Show("You must enter valid data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand)
    80.  
    81.         End If
    82.  
    83.     End Sub
    84.  
    85.     Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click
    86.  
    87.         'Displays a MessageBox that holds total sales, total commissions and total pay for all salespersons.
    88.         'Displays the numbers with two decimal places and dollar signs.
    89.  
    90.         Dim totalSales, currSellerComm, totalCommissions, totalPay As Double
    91.  
    92.         For i As Integer = 0 To ListBox1.Items.Count - 1
    93.  
    94.             Dim currSeller As Seller = DirectCast(ListBox1.Items(i), Seller)
    95.  
    96.             totalSales += currSeller.WeeklySales
    97.  
    98.             currSellerComm = CalculateCommission(currSeller)
    99.  
    100.             If currSellerComm > 0 Then
    101.                 totalCommissions += currSellerComm
    102.                 totalPay += (BASE_PAY + currSellerComm)
    103.             Else
    104.                 totalPay += BASE_PAY
    105.             End If
    106.  
    107.         Next i
    108.  
    109.         Dim strSummary As String
    110.  
    111.         strSummary = "Total sales: " & totalSales.ToString & Environment.NewLine & _
    112.                      "Total commissions: " & totalCommissions.ToString("c") & Environment.NewLine & _
    113.                      "Total pay: " & totalPay.ToString("c")
    114.  
    115.         MessageBox.Show(strSummary, "Summary", MessageBoxButtons.OK, MessageBoxIcon.Information)
    116.  
    117.     End Sub
    118. End Class
    119.  
    120. Public Class Seller
    121.  
    122.     Public Property Name As String
    123.     Public Property WeeklySales As Double
    124.  
    125.     Public Overrides Function ToString() As String
    126.  
    127.         Return Me.Name
    128.  
    129.     End Function
    130.  
    131. End Class

    Name:  shot1.JPG
Views: 2283
Size:  17.4 KBName:  shot2.JPG
Views: 2180
Size:  23.2 KBName:  shot3.JPG
Views: 2342
Size:  21.0 KB

    Edit: just realized I used Double instead of Decimal
    Last edited by MetalInquisitor; Apr 16th, 2013 at 10:50 PM. Reason: qwerty

  24. #24
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Re: Help with Vb program

    Hello there,

    can anybody help with MetalInquisitor's coding?
    I followed the instructions and found 9 errors (http://postimg.org/image/mwm4b3a57/) & (http://postimg.org/image/6gpvhv7ux/)
    Any idea how to solve this?
    Thanks.

  25. #25
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Help with Vb program

    Since you're using VS2008 you can't use auto properties. Change the Seller class to this:
    Code:
    Public Class Seller
        Private _name As String    
        Public Property Name As String
            Get
                Return _name
            End Get
            Set(value As String)
                _name = value
            End Set
        End Property
    
        private _weeklySales As Double
        Public Property WeeklySales As Double
            Get
                Return _weeklySales
            End Get
            Set(value As Double)
                _weeklySales = value
            End Set
        End Property
     
        Public Overrides Function ToString() As String 
            Return Me.Name
        End Function
    End Class

  26. #26
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Help with Vb program

    Quote Originally Posted by vltor View Post
    Code:
    Private Function Commission(ByVal SalesDecimal As Decimal) As Decimal
        If SalesAmountDecimal < QUOTA_Decimal Then
            Commission = OD
    Elself SalesAmountDecimal <= QUOTA_Decimal Then
            Commission(0.15 * SalesDecimal)
        Else
            Commission()
        End If
    End Function
    notice the end class is on the last line. i get the following errors listed in the picture.
    As far as I can see you don't have any variable called SalesAmountDecimal.

  27. #27
    New Member
    Join Date
    Apr 2013
    Posts
    2

    Re: Help with Vb program

    Woah, it worked. Thanks a lot!!

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