Why is the result of my program always underweight in BMI? - vb.net

Here is my code:
Dim Weight, Height, Bmi_value As Integer
Weight = TextBox1.Text
Height = TextBox2.Text
Bmi_value = (Weight / Height ^ 2)
TextBox3.Text = Bmi_value
Select Case Bmi_value
Case 0.0 To 18.5
TextBox4.Text = "Underweight"
Case 18.6 To 24.9
TextBox4.Text = "Normal"
Case 25.0 To 29.9
TextBox4.Text = "Overweight"
Case Is >= 30.0
TextBox4.Text = "Obese"
End Select
End Sub

This is your code fixed:
'You were using Integer instead of Double
Dim Weight, Height, Bmi_value As Double
If you want to take the value from the textBoxes, you have to covert it to Double.
Weight = Convert.ToDouble(TextBox1.Text)
Height = Convert.ToDouble(TextBox2.Text)
'Better to call the right function Math.Pow()
Bmi_value = (Weight / Math.Pow(Height, 2))
'You have to convert it to String
TextBox3.Text = Convert.ToString(Bmi_value)
Select Case Bmi_value
Case 0.0 To 18.5
TextBox4.Text = "Underweight"
Case 18.6 To 24.9
TextBox4.Text = "Normal"
Case 25.0 To 29.9
TextBox4.Text = "Overweight"
Case Is >= 30.0
TextBox4.Text = "Obese"
End Select
End Sub
There was alot of error in this code:
First: don't declare var as integer if you need decimal value, like with the BMI calculation.
Second: always convert your value to the right type if you are getting them from TextBox or if you want to print them inside a TextBox

Dim dblWeight As Double, dblHeight As Double, dblBMI As Double
dblWeight = CDbl(TextBox1.Text) 'assumes lbs.
dblHeight = CDbl(TextBox2.Text) 'assumes inches
On Error GoTo 0
dblBMI = ((dblWeight * 703.0#) / (dblHeight * dblHeight))
TextBox3.Text = dblBMI
Select Case dblBMI
Case Is <= 18.5 : TextBox4.Text = "Underweight"
Case 18.6 To 24.9 : TextBox4.Text = "Normal"
Case 25.0# To 29.9 : TextBox4.Text = "Overweight"
Case 30.0# To 34.9 : TextBox4.Text = "Obese Class I"
Case 35.0# To 39.9 : TextBox4.Text = "Obese Class II"
Case Is >= 40.0# : TextBox4.Text = "Obese Class III"
End Select
Exit Sub
Try this, Works for me.

Related

How to print out multiple If Statement in Visual Basic

I have been trying to display this if statement in my VB form application with no luck. Below is the code I have attempted.
Dim iNumber1 As Double
Dim iNumber2 As Double
Dim iNumber3 As Double
Dim iNumber4 As Double
Dim iNumber5 As Double
Dim iNumber6 As Double
Dim iNumber7 As Double
Dim iAns As Double
Dim overall As Double
iNumber1 = TextBox1.Text
iNumber2 = TextBox2.Text
iNumber3 = TextBox3.Text
iNumber4 = TextBox7.Text
iNumber5 = TextBox6.Text
iNumber6 = TextBox5.Text
iNumber7 = TextBox8.Text
iAns = iNumber1 + iNumber2 + iNumber3 + iNumber4 + iNumber5 + iNumber6 + iNumber7
overall = (Math.Round(Val(iAns / 7), 1))
If iNumber1 <= 1.9 Then
TextBox4.Text = $"Comprehension: {iNumber1} Entering"
ElseIf iNumber1 >= 2.9 And iNumber1 >= 2 Then
TextBox4.Text = $"Comprehension: {iNumber1} Emerging"
ElseIf iNumber1 >= 3.9 And iNumber1 >= 3 Then
TextBox4.Text = $"Comprehension: {iNumber1} Developing"
ElseIf iNumber1 >= 4.9 And iNumber1 >= 4 Then
TextBox4.Text = $"Comprehension: {iNumber1} Expanding"
ElseIf iNumber1 >= 5.9 And iNumber1 >= 5 Then
TextBox4.Text = $"Comprehension: {iNumber1} Bridging"
ElseIf iNumber1 >= 6.9 And iNumber1 >= 6 Then
TextBox4.Text = $"Comprehension: {iNumber1} Reaching"
End If
Any suggestions on how to get this to come out? I am totally stuck. Thanks.

If Statement inside Case Statement (VB.Net)

Hi I am new in coding at Visual studio.
I'm Using Visual Studio 2012
I Have a Question.
I want to Connect combobox and textbox31 to textbox1
ex. if the combobox is 100 and textbox31 is 1 so the textbox1 will be 100
I End Up With This Code:
Dim c As String
c = ComboBox1.Text
Select Case "c"
Case 100
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
Case 75
If TextBox31.Text >= 2.25 Then
TextBox31.Text = 75
ElseIf TextBox1.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
Case 50
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
Case 25
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
End Select
but when i iput in combobox and textbox 31 the textbox1 didnt respond or what i want to get.
The logic you're using there is nested a bit more than I'd normally be comfortable with. In this instance you could consider something more like this:
Dim c As Integer
Dim aintValues() As Integer = {0, 25, 50, 75, 100}
If IsNumeric(ComboBox1.Text) And IsNumeric(TextBox31.Text) Then
c = ComboBox1.Text
Else
Exit Sub
End If
If c <= 25 Then
aintValues = {0, 25, 25, 25, 25}
ElseIf c <= 50 Then
aintValues = {0, 25, 50, 50, 50}
ElseIf c <= 75 Then
aintValues = {0, 25, 50, 75, 75}
End If
If TextBox31.Text >= 3.25 Then
TextBox1.Text = aintValues(0)
ElseIf TextBox31.Text >= 3.0 Then
TextBox1.Text = aintValues(1)
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = aintValues(2)
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = aintValues(3)
ElseIf TextBox31.Text >= 2.25 Then
TextBox1.Text = aintValues(4)
ElseIf TextBox31.Text >= 0 Then
TextBox1.Text = 42
Else
' negative
End If
Or you can still use a select case as follows:
Dim c As Integer
Dim aintValues() As Integer = {0, 25, 50, 75, 100}
If IsNumeric(ComboBox1.Text) And IsNumeric(TextBox31.Text) Then
c = ComboBox1.Text
Else
Exit Sub
End If
If c <= 25 Then
aintValues = {0, 25, 25, 25, 25}
ElseIf c <= 50 Then
aintValues = {0, 25, 50, 50, 50}
ElseIf c <= 75 Then
aintValues = {0, 25, 50, 75, 75}
End If
Select Case TextBox31.Text
Case Is >= 3.25
TextBox1.Text = aintValues(0)
Case Is >= 3.0
TextBox1.Text = aintValues(1)
Case Is >= 2.75
TextBox1.Text = aintValues(2)
Case Is >= 2.5
TextBox1.Text = aintValues(3)
Case Is >= 2.25
TextBox1.Text = aintValues(4)
Case Is >= 0
TextBox1.Text = 42
Case Else
' negative
End Select
I don't know what your script is about, but one thing could be your failure:
The first if..then-statement sets the text of TextBox31 instead of TextBox1.
I rather would write:
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
There's a couple issues with your code, but one place to start would be to understand that your conditional
Case 100
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
End If
is functionally equivalent to
Case 100
If TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
End If
the other conditions will never be tested because if TextBox31.Text is any value greater than or equal to 2.25 then it passes and we're done.
Addressing this problem, you might reverse the order of your conditions, i.e.
Case 100
If TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
End If
Additionally, it sounds like you'd like to cover the situation in which TextBox31.Text < 2.25 which you could do with an Else
Case 100
If TextBox31.Text >= 3.25 Then
TextBox1.Text = 0
ElseIf TextBox31.Text >= 3 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 2.25 Then
TextBox1.Text = 100
Else
TextBox1.Text = 100000
End If
I Solve Now The Problem
i Use This Code:
Case 100
If TextBox31.Text >= 3 Then
TextBox1.Text = 0
ElseIf TextBox31.Text >= 2.75 Then
TextBox1.Text = 25
ElseIf TextBox31.Text >= 2.5 Then
TextBox1.Text = 50
ElseIf TextBox31.Text >= 2.25 Then
TextBox1.Text = 75
ElseIf TextBox31.Text >= 1 Then
TextBox1.Text = 100
Else
TextBox1.Text = 0
Thanks Mr.sfletche For Not Giving up helping me.
Same With Other who help me thanyo very much
Mr.sfletche if i have other problems regarding this would you help me in fute
thanks alot guys
It's a problem of mathematical Sequences. You must not use if statement here. If so, you'll go insane when to work to revise is happened unfortunately.
'Example Data
Dim c = "50"
Dim Text = "3.12"
Dim value As Double
Double.TryParse(Text, value)
Dim num = Math.Ceiling((3.25 - value) / 0.25)
Console.WriteLine(Math.Min(Integer.parseInt(c), 25 * num))
I assume that all you want to do is the above.

Input validation and repetition in vba

I have the following code and I would like help in 4 areas:
Did I validate the inputbox correctly? It should only take positive numerical variables.
How can I let the input box accept both input with, and without, symbols such as ($)?
How can I link the code so that it can directly request another number if the user has entered a negative non numeric number?
How can I repeat the procedure in the loop directly without repeating the whole code?
Option Explicit
Sub IncomeSalaryCalculation()
Dim strSalary As String
Dim dblTaxableSalary As Double
Dim dblTax As Double
Dim dblSalaryAfterTax As Double
Dim strOutput As String
Dim SalaryCalculationRequest As VbMsgBoxResult
strSalary = InputBox("Please indicate your salary", "Salary Calculation")
If Not IsNumeric(strSalary) Then
MsgBox "This is no number! Please enter a non-negatif number", vbOKOnly, "Error"
ElseIf strSalary < 0 Then
MsgBox "You should enter a positive number", vbOKOnly, "Error"
Else
dblTaxableSalary = CDbl(strSalary)
Select Case dblTaxableSalary
Case Is >= 151000
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (150000 - 37400) * 0.5 + (dblTaxableSalary - 150000) * 0.5
Case Is >= 37401
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (dblTaxableSalary - 37400) * 0.4
Case Is >= 2441
dblTax = 2440 * 0.1 + (dblTaxableSalary - 2440) * 0.2
Case Else
dblTax = 2440 * 0.1
End Select
dblSalaryAfterTax = dblTaxableSalary - dblTax
strOutput = "The amount of income tax is " & dblTax & vbNewLine & "Your salary after tax is " & dblSalaryAfterTax
MsgBox strOutput, vbOKOnly, "Final Salary"
Do
SalaryCalculationRequest = MsgBox("Do you want to calculate the tax of a new income?", vbYesNo, "New income tax salary calculation")
If SalaryCalculationRequest = vbYes Then
strSalary = InputBox("Please indicate your salary", "Salary Calculation")
dblTaxableSalary = CDbl(strSalary)
Select Case dblTaxableSalary
Case Is >= 151000
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (150000 - 37400) * 0.5 + (dblTaxableSalary - 150000) * 0.5
Case Is >= 37401
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (dblTaxableSalary - 37400) * 0.4
Case Is >= 2441
dblTax = 2440 * 0.1 + (dblTaxableSalary - 2440) * 0.2
Case Else
dblTax = 2440 * 0.1
End Select
dblSalaryAfterTax = dblTaxableSalary - dblTax
strOutput = "The amount of income tax is " & dblTax & vbNewLine & "Your salary after tax is " & dblSalaryAfterTax
MsgBox strOutput, vbOKOnly, "Final Salary"
Else
MsgBox "Glad to serve you"
End If
Loop Until SalaryCalculationRequest = vbNo
End If
End Sub
This should answer your questions without delving into all your code. Tested.
Sub getInput()
Dim ans As String
Do
ans = InputBox("Please indicate your salary", "Salary Calculation")
Loop Until isValid(ans)
''{{{{{{{{
''Do what you need here with the user input (stored in *ans* variable)
''}}}}}}}}
End Sub
Function isValid(ans As String) As Boolean
Dim cleanAns As variant
cleanAns = IIf(ans Like "*$*", Replace(ans, "$", ""), ans)
isValid = IsNumeric(cleanAns) And cleanAns > 0
If Not isValid Then MsgBox "only positive numbers allowed"
End Function

Syntax in VB Weight Calculator

I'm writing a really simple weight calculator code -- user inputs weight and height, I calculate standard weight for that height, and the code compares that weight to ranges of weights based on the standard in an if/elseif block.
The standard weight is returned correctly, but the code ALWAYS returns "Normal Weight", regardless of the height to weight ratio. I'm new to VB, so my hunch is it's a relatively simple syntax issue.
Dim dbHeight, dbWeight, dbStWeight As Double
dbHeight = CDbl(tbxHeight.Text)
dbWeight = CDbl(tbxWeight.Text)
dbStWeight = (dbHeight * 30.48 - 105) / 0.454
lblFeedback.Text = ("Your standard weight is " & dbStWeight)
If (dbStWeight * 0.9 <= dbWeight <= dbStWeight * 1.1) Then
lblResult.Text = ("Normal Weight")
ElseIf (dbStWeight * 1.1 < dbWeight <= dbStWeight * 1.2) Then
lblResult.Text = ("Over Weight")
ElseIf (dbStWeight * 0.8 <= dbWeight < dbStWeight * 0.9) Then
lblResult.Text = ("Under Weight")
ElseIf (dbWeight > dbStWeight * 1.2) Then
lblResult.Text = ("Very overweight")
ElseIf (dbWeight < dbStWeight * 0.8) Then
lblResult.Text = ("Very underweight")
End If
lblFeedback.Refresh()
lblResult.Refresh()
Private Sub btOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btOK.Click
Dim dbHeight, dbWeight, dbStWeight As Double
dbHeight = CDbl(tbxHeight.Text)
dbWeight = CDbl(tbxWeight.Text)
dbStWeight = (dbHeight * 30.48 - 105) / 0.454
StWeight.Text = ("Your standard weight is " & dbStWeight)
If (dbStWeight * 0.9 <= dbWeight) AndAlso (dbWeight <= dbStWeight * 1.1) Then
Result.Text = ("Normal Weight")
ElseIf (dbStWeight * 1.1 <= dbWeight) AndAlso (dbWeight <= dbStWeight * 1.2) Then
Result.Text = ("Overweight")
ElseIf (dbStWeight <= dbWeight) AndAlso (dbWeight < dbStWeight * 0.9) Then
Result.Text = ("Underweight")
ElseIf (dbWeight > dbStWeight * 1.2) Then
Result.Text = ("Very overweight")
ElseIf (dbWeight < dbStWeight * 0.8) Then
Result.Text = ("Very underweight")
End If
End Sub

How to check whether value of a given field is mon negative (greater than zero) in vb.net

The Condition ( less than zero ) is not working in the below code :
Protected Sub txt_business_revenue_risk_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_business_revenue_risk.TextChanged
Dim a As double= 0.0
If IsNumeric(Me.txt_business_revenue_risk.Text) Then
Me.lbl_business_revenue_risk.Text = ""
Me.lbl_rest_risk.Text = CDbl(Me.txt_business_revenue_risk.Text) - CDbl(Me.txt_mitigated_risk.Text)
If CDbl(Me.txt_business_revenue_risk.Text) < CDbl(a) Then
Me.lbl_business_revenue_risk.Text = "Must be a number greater than zero!"
txt_business_revenue_risk.Text = ""
End If
If CDbl(Me.txt_business_revenue_risk.Text) < CDbl(Me.txt_mitigated_risk.Text) Then
Me.lbl_rest_risk.Text = 0
Else
Me.lbl_rest_risk.Text = CDbl(Me.txt_business_revenue_risk.Text) - CDbl(Me.txt_mitigated_risk.Text)
End If
Else
Me.lbl_business_revenue_risk.Text = "Must be a number!"
txt_business_revenue_risk.Text = ""
End If
End Sub
Avoid the usage of VB6 methods such as IsNumeric, and those lot of Double-parsings, try this:
Private Sub txt_business_revenue_risk_TextChanged(sender As Object, e As EventArgs) _
Handles txt_business_revenue_risk.TextChanged
Dim Zero As Double = 0.0
Dim Num1 As Double, Num2 As Double
If Double.TryParse(CStr(sender.text), Num1) _
AndAlso Double.TryParse(CStr(txt_mitigated_risk.Text), Num2) Then
Me.lbl_business_revenue_risk.Text = String.Empty
Me.lbl_rest_risk.Text = CStr(Num1 - Num2)
Select Case Num1
Case Is < Zero
Me.lbl_business_revenue_risk.Text = "Must be a number greater than zero!"
sender.Text = String.Empty
Case Is < Num2
Me.lbl_rest_risk.Text = CStr(Zero)
Case Else
Me.lbl_rest_risk.Text = CStr(Num1 - Num2)
End Select
Else
Me.lbl_business_revenue_risk.Text = "Must be a number!"
txt_business_revenue_risk.Text = String.Empty
End If
End Sub
Try this -- Don't forget to add exception handling
Dim resMsg As String = ""
Dim txtRevRisk As String = Me.txt_business_revenue_risk.Text
Dim txtMitRisk As String = Me.txt_mitigated_risk.Text
Dim dRes As Double = 0
Dim a As Double = 0.0
If IsNumeric(txtRevRisk) AndAlso IsNumeric(txtMitRisk) Then
Dim dRevRisk = CDbl(txtRevRisk)
Dim dMitRisk = CDbl(txtMitRisk)
resMsg = txtRevRisk
dRes = dRevRisk - dMitRisk
If dRevRisk < a Then
resMsg = "Must be a number greater than zero!"
txtRevRisk = ""
End If
If dRevRisk < dMitRisk Then
dRes = 0
'Else
' Me.lbl_rest_risk.Text = dRevRisk - dMitRisk
End If
Else
resMsg = "Must be a number!"
txtRevRisk = ""
End If
Me.lbl_rest_risk.Text = dRes.ToString()
Me.lbl_business_revenue_risk.Text = resMsg.ToString()
Me.txt_business_revenue_risk.Text = txtRevRisk.ToString()