ElseIf statement wont run the whole code VB - vb.net

Ok so I've been working on a program I'm making and for some reason when I put a bit of my code into an else if statement, it won't run the statement at all not even the original If statement, However when I put it in a regular If and else statement it works perfectly.
This bit of code works:
If TextBox5.Text > TextBox1.Text & TextBox6.Text > TextBox2.Text Then 'NE
Bearing = Atan(X2 / Y2) * 57.3
Else
Bearing = Atan(Y2 / X2) * 57.3
Bearing = Bearing + -Bearing + -Bearing + 90
If BOFF > 0 Then
Bearing = Bearing - Math.Round(BOFF)
Else
Bearing = Bearing + Math.Round(BOFF)
End If
End If
And this bit of code doesnt work:
If TextBox5.Text > TextBox1.Text & TextBox6.Text > TextBox2.Text Then 'NE
Bearing = Atan(X2 / Y2) * 57.3
ElseIf TextBox5.Text > TextBox1.Text & TextBox6.Text < TextBox2.Text Then
Bearing = Atan(Y2 / X2) * 57.3
Bearing = Bearing + -Bearing + -Bearing + 90
If BOFF > 0 Then
Bearing = Bearing - Math.Round(BOFF)
Else
Bearing = Bearing + Math.Round(BOFF)
End If
End If
Could be a syntax error something, but it doesn't come up with an error at all, runs the code perfectly and all that. I don't know what I'm doing wrong here.

You should use AND instead of &
AND is the logical operator on the other hand & is for joining strings
It is the logical as well as bitwise AND operator. If both the
operands are true, then condition becomes true. This operator does not
perform short-circuiting, i.e., it evaluates both the expressions.
If TextBox5.Text > TextBox1.Text And TextBox6.Text > TextBox2.Text Then 'NE
'Code
ElseIf TextBox5.Text > TextBox1.Text And TextBox6.Text < TextBox2.Text Then
'Code
End If
Note:
If values of TextBox1.Text =4, TextBox6.Text=1 then TextBox1.Text & TextBox6.Text will yields 41
See this demo
& is used to concatenate two strings

Related

Why is the result of my program always underweight in BMI?

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.

VB ElseIf Statement Limited to 1

I was forced by college to do a application in Visual Studio (Basic) and I never had expierence with this language before so I'm confused. I did If statement which is working and then I've added EleseIf which is also working but after I've added Second ElseIf it doesn't work. It seems like only If statement and the first ElseIf is working for me but I need more than just 1 ElseIf statement.
Public Class receipt
Private Sub receipt_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim outputValue As Decimal = My.Settings.outputamount.Remove(4)
Dim calculation As Decimal = outputValue * My.Settings.inputamount
Dim totalwithoutcharge As String = calculation.ToString
customername.Text = "Name: " + My.Settings.Username
Label6.Text = "Entered Money: " + My.Settings.inputamount + " " + My.Settings.currency
Label7.Text = "Converted To: " + totalwithoutcharge + " " + My.Settings.outputcurrency
If calculation < 100 Then
Label8.Text = "Charge: 0%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 100 Then
Label8.Text = "Charge: 1%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 500 Then
Label8.Text = "Charge: 2%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
ElseIf calculation > 1000 Then
Label8.Text = "Charge: 3%"
Label9.Text = "Total: " + totalwithoutcharge + " " + My.Settings.outputcurrency
Else
Label8.Text = "Something went wrong"
End If
End Sub
End Class
I've entered many values I've add over 600 and it was still showing 'Charge: 1%' where it should show 2% instead. The same happen with values higher than 1000 it will still show the 1%. But when the value is below 100 then is OK and it is showing 0%.
Over 100 works fine too
Over 500 doesn't work
I've also tried to do it with two conditions where it would look at range but it didn't work too.
I've tried 'ElseIf calculation > 100 And calculation > 500 Then' but there is no difference.
First of all use & instead of + when concentating strings.
Second if calculation is > 500 it is also greater than 100, 200 etc. So you need to implement a range. So think about the logic.
Based on my comment
If calculation >= 1000 Then
ElseIf calculation >= 500 Then
ElseIf calculation >= 100 Then
Else
'lass than 100
End If

Wrong Calculation in Visual basic

Hi i have problem a wrong calculation in visual basic
when i input 1.5 all in the combo boxes then
the textbox4.textto textbox14.text are the units that has the values of 3,3,3,3,3,3,6,2,3 that is equal to 29.
I add all from textbox30.textto textbox22.text the divide them to the total units that is 29
the textbox31.text is equal to 1.51724137931034 but the correct value is 1.50
like this
TextBox30.Text = 3*1.5
TextBox29.Text = 3*1.5
TextBox28.Text = 3*1.5
TextBox27.Text = 3*1.5
TextBox26.Text = 3*1.5
TextBox25.Text = 3*1.5
TextBox24.Text = 6*1.5
TextBox23.Text = 2*1.5
TextBox22.Text = 3*1.5
a = 4.5 + 4.5 + 4.5 + 4.5 + 4.5 + 4.5 + 9 + 3 + 4.5
textbox31.text = a/29
heres the code
Dim a As Integer
TextBox30.Text = TextBox4.Text * ComboBox5.Text
TextBox29.Text = TextBox5.Text * ComboBox6.Text
TextBox28.Text = TextBox6.Text * ComboBox7.Text
TextBox27.Text = TextBox7.Text * ComboBox8.Text
TextBox26.Text = TextBox8.Text * ComboBox9.Text
TextBox25.Text = TextBox9.Text * ComboBox10.Text
TextBox24.Text = TextBox10.Text * ComboBox11.Text
TextBox23.Text = TextBox11.Text * ComboBox12.Text
TextBox22.Text = TextBox12.Text * ComboBox13.Text
a = TextBox30.Text + Val(TextBox29.Text) + Val(TextBox28.Text) + Val(TextBox27.Text) + Val(TextBox26.Text) + Val(TextBox25.Text) + Val(TextBox24.Text) + Val(TextBox23.Text) + Val(TextBox22.Text)
TextBox31.Text = (a / 29)
I don't know if it solve your problem, but it's always better to use a datatype like double for decimals. Integer is just for whole numbers.
So try Dim a As Double
a is an integer. a = 44. a/29 = 1.5172413793.
Can avoid some of these problems by ensuring option strict is always on.

Loops - Adding Numbers - Visual Basic

So the program has to add all the numbers from "x" to "y".
But it also has to display all the numbers added :
i.e. 10 to 20 should display 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 = 165
Here's what I have:
Dim firstnum As Integer = Val(TextBox1.Text)
Dim secondnum As Integer = Val(TextBox2.Text)
Dim sum As Integer = 0
While firstnum <= secondnum
sum = sum + firstnum
firstnum = firstnum + 1
Label3.Text = firstnum & "+"
End While
suum.Text = " = " & Val(sum)
With the following:
Label3.Text = firstnum & "+"
You are overwriting the value in Label3 every time you go through the loop. What you probably want to do is concatenate the existing value with the next number.
This should get you on your way:
Label3.Text = Label3.Text & firstnum & " + "
Is Linq ok? Then you can use Enumerable.Range and Enumerable.Sum:
Dim startNum = Int32.Parse(TextBox1.Text)
Dim endNum = Int32.Parse(TextBox2.Text)
Dim numbers = Enumerable.Range(startNum, endNum - startNum + 1) 'inclusive, therefore + 1
Label3.Text = String.Join(" + ", numbers)
suum.Text = numbers.Sum()
your Label3.Text will only contain the last num and "+" at the end of the algorithm. You should replace
Label3.Text = firstnum & "+"
with
Label3.Text = Label3.Text & firstnum & "+ "

Using conditionals in VB.NET to count

I am running a program in windows forms visual basic and im trying to make multiple counters
Here is my code:
If txtAnswer.Text = nMathSum Then
nCount = nCount + 1
lblCorrect.Text = nCount
ElseIf txtAnswer.Text <> nMathSum Then
nIount = nIount + 1
lblIncorrect.Text = nIount
End If
If txtAnswer.Text = nMathDiff Then
nCount = nCount + 1
lblCorrect.Text = nCount
ElseIf txtAnswer.Text <> nMathDiff Then
nIcount = nIcount + 1
lblIncorrect.Text = nIout
End If
It suppose to count how many times i answered correctly and incorrectly
The Counter for the Sum is working fine but the counter for the difference has a problem.
When i input the correct answer it goes to the incorrect label.
You have a misspelling of nIount as nIcount in the seconf Elseif
Also as nIout in the last line. Correct it as:
ElseIf txtAnswer.Text <> nMathDiff Then
nIount = nIount + 1
lblIncorrect.Text = nIount
End If
This is assuming the first appearance (nIount) is the correct spelling.
It's very unlikely that txtAnswer will match both the sum and the difference. So, in your code, you will always have at least one of them incorrect.
Do you have some way of knowing if txtAnswer is supposed to match the sum or difference -- if so, check that before checking the answer.
EDIT (to explain what I mean): Something like
If operation = "+" Then
If txtAnswer.Text = nMathSum Then
nCount = nCount + 1
lblCorrect.Text = nCount
ElseIf txtAnswer.Text <> nMathSum Then
nIcount = nIcount + 1 ' corrected this line to use nIcount
lblIncorrect.Text = nIcount ' corrected this line to use nIcount
End If
Else
If txtAnswer.Text = nMathDiff Then
nCount = nCount + 1
lblCorrect.Text = nCount
ElseIf txtAnswer.Text <> nMathDiff Then
nIcount = nIcount + 1
lblIncorrect.Text = nIcount ' corrected this line too
End If
End If
Where operation is a variable that is set to "+" or "-" depending on whether the user is supposed to provide a sum or difference.