Results 1 to 2 of 2

Thread: Need help with calculation for VB express

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    2

    Exclamation Need help with calculation for VB express

    First of all thanks for taking a look, this is a class assignment and I'm about a month in and seeing as how it's fast approaching 3am I dont think my professor is going to want to get a call.

    That being said, I'm trying to write a basic program that will take a weight and distance and calculate from it a total shipping charge. I'm having several problems with it but first and foremost I need to get the calculation itself working correctly (formatting for currency I assume will be solved as a byproduct but has been removed from the below code due to runtime errors created by its presence). The net result of what is happening is while as far as I can tell the coding is set up correctly the only result I can get is "0", so clearly I've gone wrong somewhere. Here is the coding for the click event to calculate the cost.

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

    'Declaration of and definition of variables
    Dim weight As Single
    Dim distance As Single
    Dim charges As Decimal
    txtWeight.Text = weight
    txtDistance.Text = distance

    'Returns error if distance is greater then 3000 miles
    'or less then 10 miles or if weight is less then 0 kg
    'or greater then 20 kg
    'Also includes Formulas and declaration of values

    If distance < 10 Then lblChargeResult.Text = " Please enter valid weight and distance "
    If distance > 3000 Then lblChargeResult.Text = " Please enter valid weight and distance "

    If weight < 0 Then
    lblChargeResult.Text = " Please enter valid weight and distance "
    ElseIf weight > 0 Then
    charges = 0.01
    ElseIf weight > 2 Then
    charges = 0.015
    ElseIf weight > 6 Then
    charges = 0.02
    ElseIf weight > 10 Then
    charges = 0.025
    ElseIf weight > 20 Then
    lblChargeResult.Text = " Please enter valid weight and distance "
    End If

    'Displays Charge Result
    lblChargeResult.Text = Val(charges * distance)
    End Sub

    If anyone can point out my error and/or tell me how to correct it I would appreciate it.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Need help with calculation for VB express

    You've got your ElseIfs in the wrong order. The "weight > 0" will catch everything before it has a chance to be tested against the other conditions. You should have the greatest value first and work your way down. What you need to do is use a break point and debug your code properly. Click the first line of that code and press F9 to set a break point. Now when you run your app it will stop at that line. You can then use F10 to step through the code one line at a time. You can mouse over properties and variables to see their current values and you can use the Locals and Watch windows to follow variables and properties and to evaluate expressions.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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