Search code examples
vb.netdateprocedure

Procedure comparing dates in vb.net doesn't appear to do anything


Pretty much just the title, I've been learning vb for my computer science A-level and ran into some trouble with this exercise. I made a procedure to affect the final cost of an invoice by comparing the current date to the due date, but the due date that I input does not seem to have any effect on the final cost.

Form:

Invoice Form

Task:

Write a program that processes invoices for a company selling a variety of products. Ask the user to enter the unit cost of the product, how many were sold and the date the invoice had to be paid by. A check box should be used to indicate if the product is VAT rated. When these details have been entered the user should click a button. This event should call two general procedures. The first should calculate and return the basic cost of the invoice, including VAT. The second should reduce the basic cost by 10% if the invoice has been paid on time. The final cost should be displayed by the Click event of the button.

Code:

Public Class Form1
    Dim invoice As Integer
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim unitCost As Integer = txtCost.Text 'Input cost of product in textbox
        Dim unitsSold As Integer = txtUnits.Text 'Input units sold in textbox
        Dim dueDate As Date = dtpDueDate.Value 'Input date in date and time picker
        Dim VATCheck As Boolean = chkVAT.Checked 'Input VAT rating in checkbox

        Call InvoiceProcess(unitCost, unitsSold, VATCheck)
        Call DueCheck(dueDate, invoice)
        MsgBox(invoice)
    End Sub
    Sub InvoiceProcess(ByRef price As Integer, ByRef units As Integer, ByRef VAT As Boolean)
        If VAT = True Then
            invoice = 1.2 * price * units
        Else
            invoice = price * units
        End If
    End Sub
    Sub DueCheck(ByRef dateDue As Date, ByVal invoice As Integer)
        Dim todayDate As Date = Today.Date 'Current date
        Dim overDue As Integer = DateTime.Compare(todayDate, dateDue.Date)
        If overDue <= 0 Then
            invoice = invoice * 0.9
        End If
    End Sub
End Class

Solution

  • The question says "This event should call two general procedures. The first should calculate and return..." - notice it says "return" - that means that it needs to be a function, not a sub.

    Once you've got that fixed, the invoice value can be passed from one method to another in the parameters, so you can remove the Dim invoice As Integer from where it is because it is currently scoped to entire class, which you probably don't want.

    Also, invoice should be a Decimal, not an Integer.