"Simple" array with formula

d0ctorworm

Member
Joined
Nov 28, 2010
Messages
9
Location
UK
Programming Experience
Beginner
Hi everyone,

I've joined this forum in the hope that I can get some help with part of my code. It's very straightforward but I cannot seem to get the hang of arrays. I'm doing this as part of an electronic engineering course but I'm very new to programming and so my code is flaky...

I have to make a programme for a DVD shop where I enter the following in textboxes: ID Code, Title, Price and Quantity. Depending on the quantity there can be discounts if the DVDs are bought in bulk:

0-4 DVDs = No discount
5-9 DVDs = 12% discount
10-14 DVDs = 17% discount
Over 15 DVDs = 25% discount

The section of code below is supposed to create a dynamic array with 6 columns (textboxes, discounts and subtotals) and display it in a console (for testing). Later I plan to save the array to a .txt file.

Anyway, the array appears in the console but the discounts are wrong so I must have the array programmed incorrectly. Can anyone help me with this???


VB.NET:
  Private Sub Results_Array()


        'Dynamic two-dimensional array initialised as object to show different data types

        Dim Results_Array(0 To 5, 0 To 0) As Object
        ReDim Results_Array(0 To 5, 0 To 0)

        Results_Array(0, 0) = TextBox1.Text
        Results_Array(1, 0) = TextBox2.Text
        Results_Array(2, 0) = TextBox3.Text
        Results_Array(3, 0) = TextBox4.Text

        Dim IDCode As String = CStr(Results_Array(0, 0))
        Dim Title As String = CStr(Results_Array(1, 0))
        Dim Price As Single = CSng(Results_Array(2, 0))
        Dim QTY As Integer = CInt(Results_Array(3, 0))
        Dim Discount As Single = CSng(Results_Array(4, 0))
        Dim Subtotal As Single = CSng(Results_Array(5, 0))

        ReDim Preserve Results_Array(0 To 5, 0 To Results_Array.GetUpperBound(0))


        'Applying percentage discounts to entries

        If QTY >= 5 Then
            Discount = Price * QTY * 0.12

        ElseIf QTY >= 10 Then
            Discount = Price * QTY * 0.17

        ElseIf QTY >= 15 Then
            Discount = Price * QTY * 0.25

        Else : Discount = 0

        End If

        Subtotal = (Price * QTY) - Discount


        'Display Array in Console

        For i = 0 To 5
            For j = 0 To Results_Array.GetUpperBound(0)

                System.Console.Write(Results_Array(i, j) & " ")

            Next j
        Next i

        System.Console.Write(Discount & " ")
        System.Console.Write(Subtotal & vbNewLine)


        'Clear textboxes

        TextBox1.Text = Nothing
        TextBox2.Text = Nothing
        TextBox3.Text = Nothing
        TextBox4.Text = Nothing

    End Sub

Sorry for the long post. I don't use forums very often and hope I haven't broken any rules on the first post.

Take it easy...


Jake
 
Last edited:
You have to use a reverse order in the way you list the "elseIF" instructions.
This way, starting from the lower, the first instruction that returns true will get executed every time.
So if you buy 45 dvds and the first if is "if >= 5" this one returns true, gets executed, and then control is returned.
You have to reverse the order of the IFs in order to start from the least likely to be true (highest first) to the most likely to be true (lowest).
In this case it's simple because you have just 3 boundaries, but in case you had more rearranging all the lines would be frustrating.
In more complex cases I would advise to use double boundary IF instructions: IF >= num1 And < num 2 then
 
Kulrom,

I know this thread is getting old now but I need some more help with the programme!

I tried to use the IsNumeric function followed by a msgbox to make sure the Price and Qty textboxes contain numbers only and it didn't like it at all!

VB.NET:
   'Checks textbox data for Price and Quantity are numeric
        If Not IsNumeric(PriceTextBox.Text) Or Not IsNumeric(QuantityTextBox.Text) = True Then
            Dim msg = "Either Price or Quantity are of incorrect format - please try again."
            MsgBox(msg, vbCritical, Title:="Details Error")

        End If

end if

Can you tell me why this doesn't work and if there's a different way to do this?

Thanks,

Jake

Try this:

VB.NET:
       If Decimal.TryParse(PriceTextBox.Text, 0) = False OrElse Integer.TryParse(QuantityTextBox.Text, 0) = False Then
            Dim message As String = "Either Price or Quantity are of incorrect format - please try again."
            MessageBox.Show(message, "Details Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
 
Thanks everyone for all your help with this. The reason it didn't work was a combination of not including an "Else" statement and placing the code in the wrong part of the procedure. We live and learn.

Cheers again
Jake
 
Back
Top