none
Need help with VB code

    Question

  • Hi. I have to write a program that calculates your BMI. The Imperial calculation seems to work but the Metric part is not working. It gives me nothing when it is selected from the ComboBox. It is not giving me any error messages so I'm not sure where the problem is. Can any of you take a quick look and see if you can figure out why it is not working?  Any help would be greatly appreciated. Thank you!

    Here is the code:

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

            Dim Weight As Integer = 0

            Dim Height As Integer = 0

            Dim BMI As Double = 0

            Dim BMIstring As String = ""

            Try

                Weight = Convert.ToInt32(txtWeight.Text)

                Height = Convert.ToInt32(txtHeight.Text)

                Select Case cboSelection.SelectedIndex

                    Case 1

                        BMI = Imperial(Weight, Height)

                        BMIstring = Format(BMI, "#######0.00")

                        MsgBox("Your imperial BMI is " & BMIstring)

                    Case 2

                        BMI = Metric(Weight, Height)

                        BMIstring = Format(BMI, "######0.00")

                        MsgBox("Your metric BMI is " & BMIstring)

                End Select

            Catch ex As FormatException

                MsgBox("Please enter a valid number.")

            End Try

        End Sub

        Private Function Imperial(ByRef Weight As Integer, ByRef Height As Integer) As Double

            Return (Weight / (Height ^ 2)) * 703

        End Function

        Private Function Metric(ByRef Weight As Integer, ByRef Height As Integer) As Double

            Return Weight / (Height ^ 2)

        End Function

    Sunday, March 26, 2017 9:36 PM

Answers

  • I did not catch that. Changing it from 1 and 2 to 0 and 1 worked. Thank you so much!
    • Marked as answer by Kimbo1367 Sunday, March 26, 2017 9:50 PM
    Sunday, March 26, 2017 9:50 PM

All replies

  • Kimbo,

    The best way to find it is to put a breakpoint in and step into each line of code.

    Per line, you can then hover your mouse over the variables which are then in scope and you'll see the value of it.

    ***** EDIT *****

    Remember that all collections (including ComboBox items) are zero-based, so the first item is at index 0, not index 1.

    ;-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Sunday, March 26, 2017 9:42 PM
  • Hi. I have to write a program that calculates your BMI. The Imperial calculation seems to work but the Metric part is not working. It gives me nothing when it is selected from the ComboBox.

    It gives you nothing, or it gives you a result of zero?  The difference is important.

    Insert a breakpoint at the Select Case statement.  Run the program with each option selected, step past the breakpoint a line at a time, and watch where the execution steps to.  If it skips the metric processing you need to examine your slect case control variable..

    Sunday, March 26, 2017 9:44 PM
  • Assuming that you have only two items in the ComboBox the SelectedIndex will be either 0 or 1 never 2.  Just change you case statements.


    Lloyd Sheen

    Sunday, March 26, 2017 9:45 PM
  • I did not catch that. Changing it from 1 and 2 to 0 and 1 worked. Thank you so much!
    • Marked as answer by Kimbo1367 Sunday, March 26, 2017 9:50 PM
    Sunday, March 26, 2017 9:50 PM
  • I did not catch that. Changing it from 1 and 2 to 0 and 1 worked. Thank you so much!

    You marked yourself as the answerer?

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, March 26, 2017 9:55 PM
  • I did not catch that. Changing it from 1 and 2 to 0 and 1 worked. Thank you so much!

    Hello,

    If you got the solution from one or more replies you should mark them as either helpful or as answered.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, March 26, 2017 10:13 PM
    Moderator