I'm trying to find one category that is based on two different classification with this code but i got notifications that "elseif must be precedeed by a matching if or elseif" on the first two classification i made.
if pof < 0.1 then CCat = "A"
elseif 0.1 < pof < 0.2 then CCat = "B"
elseif 0.2 < pof < 0.3 then CCat = "C"
elseif 0.3 < pof < 0.5 then CCat = "D"
else CCat = "E"
end if
if cof < 10000 then CCat = "A"
elseif 10000 < cof < 50000 then CCat = "B"
elseif 50000 < cof < 150000 then CCat = "C"
elseif 150000 < cof < 1000000 then CCat = "D"
else CCat = "E"
end if
is there a problem with my if conditions? pof and cof are both a double resulting from a calculation done before.
It's just a little syntactic trip-up you're hitting. If you drop the code after the "Then" operator down to the next line, VB considers it a multi-line statement, rather than a single-line statement. That's when you get ElseIf, Else, and End If
I am betting you're used to C#, where you can map it all out with brackets. {}
It took me a few moments to remember VB enough, but here's what I think you want:
If pof < 0.1 Then
CCat = "A"
ElseIf 0.1 < pof < 0.2 Then
CCat = "B"
ElseIf 0.2 < pof < 0.3 Then
CCat = "C"
ElseIf 0.3 < pof < 0.5 Then
CCat = "D"
Else
CCat = "E"
End If
If cof < 10000 Then
CCat = "A"
ElseIf 10000 < cof < 50000 Then
CCat = "B"
ElseIf 50000 < cof < 150000 Then
CCat = "C"
ElseIf 150000 < cof < 1000000 Then
CCat = "D"
Else
CCat = "E"
End If
Related
I'm having a bit of a headache when it comes to VBA. I had tried to search online for an answer but to no luck. I have learned Python, but VBA is a different ballpark.
Dim X As String
Function GRADELETTER_PM(Num_Grade As Double)
X = "A"
If Num_Grade >= 0.93 Then 'finds corresponding letter to a grade'
X = "A"
MsgBox X
End If
If Num_Grade >= 0.9 Then
X = "A-"
End If
If Num_Grade >= 0.88 Then
X = "B+"
End If
If Num_Grade >= 0.83 Then
X = "B"
End If
If Num_Grade >= 0.8 Then
X = "B-"
End If
If Num_Grade >= 0.78 Then
X = "C+"
End If
If Num_Grade >= 0.73 Then
X = "C"
End If
If Num_Grade >= 0.7 Then
X = "C-"
End If
If Num_Grade >= 0.67 Then
X = "D+"
End If
If Num_Grade >= 0.6 Then
X = "D"
End If
If Num_Grade < 0.6 Then
X = "F"
End If
End Function
The program is supposed to calculate a grade to its letter. IE a 93% (input) is an "A" (Output) while a 64% is a "D". The only input is the grade. The sheet itself has multiple tables that do not aline themselves perfectly (ie, not the same Col x Row) and the formula will be used 40+ times on that one sheet when it works. Thanks in advance.
I would suggest using a Select Case statement, for example:
Function GRADELETTER_PM(ByVal Num_Grade As Double) As String
Select Case Num_Grade
Case Is >= 0.93: GRADELETTER_PM = "A"
Case Is >= 0.9: GRADELETTER_PM = "A-"
Case Is >= 0.88: GRADELETTER_PM = "B+"
Case Is >= 0.83: GRADELETTER_PM = "B"
Case Is >= 0.8: GRADELETTER_PM = "B-"
Case Is >= 0.78: GRADELETTER_PM = "C+"
Case Is >= 0.73: GRADELETTER_PM = "C"
Case Is >= 0.7: GRADELETTER_PM = "C-"
Case Is >= 0.67: GRADELETTER_PM = "D+"
Case Is >= 0.6: GRADELETTER_PM = "D"
Case Else: GRADELETTER_PM = "F"
End Select
End Function
Here is a reference.
I can't understand how I should fix my code. I keep getting this error.
InvalidCastException was unhandled
An unhandled exception of type 'System.InvalidCastException' occurred
in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Double' is
not valid.
I'm confused on how to fix this. I don't understand the error. It starts at the beginning of the If. Here is the code I am using:
Public class Income_Tax
Dim rate as Double
Dim difference as Double
Private Sub textboxqitni_TextChanged(sender As Object, e As EventArgs) Handles textboxqitni.TextChanged
If textboxqitni.Text >= 0 And textboxqitni.Text <= 10000 Then
textboxittd.Text = textboxqitni.Text * 0.05
ElseIf textboxqitni.Text >= 10000 And textboxqitni.Text <= 30000 Then
difference = textboxqitni.Text - 10000
rate = difference * 0.1
textboxittd.Text = rate + 500
ElseIf textboxqitni.Text >= 30000 And textboxqitni.Text <= 70000 Then
difference = textboxqitni.Text - 30000
rate = difference * 0.15
textboxittd.Text = rate + 2500
ElseIf textboxqitni.Text >= 70000 And textboxqitni.Text <= 140000 Then
difference = textboxqitni.Text - 70000
rate = difference * 0.2
textboxittd.Text = rate + 8500
ElseIf textboxqitni.Text >= 140000 And textboxqitni.Text <= 250000 Then
difference = textboxqitni.Text - 140000
rate = difference * 0.25
textboxittd.Text = rate + 22500
ElseIf textboxqitni.Text >= 250000 And textboxqitni.Text <= 500000 Then
difference = textboxqitni.Text - 250000
rate = difference * 0.3
textboxittd.Text = rate + 50000
ElseIf textboxqitni.Text >= 500000 And textboxqitni.Text <= 999999999999999 Then
difference = textboxqitni.Text - 500000
rate = difference * 0.32
textboxittd.Text = rate + 125000
End If
End Sub
The first thing I suggest is to use a decimal datatype for your calculations.
It seems that your mathematical operations involve monetary values and in this case you should always use a decimal datatype to avoid floating point errors well documented.
Next problem is caused by the fact you think that a string containing only digits can be used in mathematical expressions. This is not true and works (sometime) only if you set Option Strict Off in your program options.
This settings was left to Off to facilitate the porting of VB6 programs to VB.NET and you should set it to ON for new code to avoid the subtle bugs introduced by automatic conversion of values.
You should always convert that string to a numeric variable, do the math with the numeric variable and then, if you need to display the result, convert back the number to a string.
Public Class Income_Tax
Dim rate As Decimal
Dim difference As Decimal
Private Sub textboxqitni_TextChanged(sender As Object, e As EventArgs) Handles textboxqitni.TextChanged
' Use a decimal variable to extract the current value typed
Dim value As Decimal
' You don't want to continue if the input is not a valid number
if Not decimal.TryParse(textboxqitni.Text, value) Then
MessageBox.Show("Not a valid number")
return
End If
' Use AndAlso to express logical AND
If value >= 0 AndAlso value <= 10000 Then
value = value * 0.05
' <= 10000 is already takes, the elseif should be for > 10000
ElseIf value > 10000 AndAlso value <= 30000 Then
difference = value - 10000
rate = difference * 0.1
value = rate + 500
ElseIf value > 30000 AndAlso value <= 70000 Then
difference = value - 30000
rate = difference * 0.15
value = rate + 2500
ElseIf value > 70000 AndAlso value <= 140000 Then
difference = value - 70000
rate = difference * 0.2
value = rate + 8500
' ..............................
' Complete with other else if
' ..............................
End If
' Finally set the value back to textbox
textboxittd.Text = value.ToString()
End Sub
Try this
Try
Dim tbval As Integer = Integer.Parse(textboxqitni.Text)
If tbval >= 0 And tbval <= 10000 Then
textboxittd.Text = tbval * 0.05
ElseIf tbval >= 10000 And tbval <= 30000 Then
difference = tbval - 10000
rate = difference * 0.1
textboxittd.Text = rate + 500
ElseIf tbval >= 30000 And tbval <= 70000 Then
difference = tbval - 30000
rate = difference * 0.15
textboxittd.Text = rate + 2500
ElseIf tbval >= 70000 And tbval <= 140000 Then
difference = tbval - 70000
rate = difference * 0.2
textboxittd.Text = rate + 8500
ElseIf tbval >= 140000 And tbval <= 250000 Then
difference = tbval - 140000
rate = difference * 0.25
textboxittd.Text = rate + 22500
ElseIf tbval >= 250000 And tbval <= 500000 Then
difference = tbval - 250000
rate = difference * 0.3
textboxittd.Text = rate + 50000
ElseIf tbval >= 500000 And tbval <= 999999999999999 Then
difference = tbval - 500000
rate = difference * 0.32
textboxittd.Text = rate + 125000
End If
Catch ex As Exception
MsgBox("Error Occured" & vbCrLf & ex.Message)
End Try
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.
I'm trying to calculate discount based on a person's age category and amount of visits at a hair salon. It's just not working properly though. It doesn't calculate the proper discount until the second click and then it does some weird stuff if I keep pressing calculate. Just wondering where I am going wrong, thanks.
' Discount
If radAdult.Checked = True Then
discount = 0
ElseIf radChild.Checked = True Then
discount = totalPrice * 0.1
ElseIf radStudent.Checked = True Then
discount = totalPrice * 0.05
ElseIf radSenior.Checked = True Then
discount = totalPrice * 0.15
End If
' Additional discount
If txtClientVisits.Text >= 1 And txtClientVisits.Text <= 3 Then
additionalDiscount = 0
ElseIf txtClientVisits.Text >= 4 And txtClientVisits.Text <= 8 Then
additionalDiscount = totalPrice * 0.05
ElseIf txtClientVisits.Text >= 9 And txtClientVisits.Text <= 13 Then
additionalDiscount = totalPrice * 0.1
ElseIf txtClientVisits.Text >= 14 Then
additionalDiscount = totalPrice * 0.15
End If
totalPrice = baseRate + serviceRate - (discount + additionalDiscount)
The type of the txtClientVisits.Text property is a String. The comparison operators <, >, >=, and <= for strings perform a lexicographic comparison. VB will then convert 14 to the string "14" and so compare each digit one-by-one, which is not what you want.
(This is why I don't like VB.NET - because it performs these implicit conversions without warning you).
You will need to explicitly convert the number in the textbox to an actual number, then compare based on that:
Dim clientVisits As Integer = CInt( txtClientVisits.Text )
If clientVisits >= 1 AndAlso clientVisits < 4 Then
additionalDiscount = 0
ElseIf clientVisits >= 4 AndAlso clientVisits < 9 Then
additionalDiscount = totalPrice * 0.05
ElseIf clientVisits >= 9 AndAlso clientVisits < 14 Then
additionalDiscount = totalPrice * 0.1
ElseIf clientVisits >= 14 Then
additionalDiscount = totalPrice * 0.15
End If
I note you used inclusive boundary values. That works for integer values but will fail for continuous (floating-point) values. Note how I'm using >= and < instead of >= and <= to avoid that case.
Also, note I'm using the AndAlso operator which is short-circuiting (compared to And which is not). This isn't a functional change, it just means the program will run slightly faster.
Also, you don't need to do ElseIf radChild.Checked = True Then because the value of the .Checked property is already a boolean value, you can do this instead:
ElseIf radChild.Checked Then
I am trying to do some computations on VB.net. I used if else statement since I'm a bit familiar with it. My code goes like this
Try
Dim a As Integer = msalary.Text
If (a < 9000) Then
Label5.Text = a - 200
ElseIf (9000 < a < 9999.99) Then
Label5.Text = a - 225
ElseIf (10000 < a < 10999.99) Then
Label5.Text = a - 250
ElseIf (11000 <= a < 11999.99) Then
Label5.Text = a - 275
ElseIf (12000 <= a < 12999.99) Then
Label5.Text = a - 300
ElseIf (13000 <= a < 14000) Then
Label5.Text = a - 325
ElseIf (14000 <= a < 15000) Then
Label5.Text = a - 350
ElseIf (15000 <= a < 16000) Then
Label5.Text = a - 375
ElseIf (17000 <= a < 18000) Then
Label5.Text = a - 400
ElseIf (18000 <= a < 19000) Then
Label5.Text = a - 425
ElseIf (19000 <= a < 20000) Then
Label5.Text = a - 450
ElseIf (20000 <= a < 21000) Then
Label5.Text = a - 475
ElseIf (21000 <= a < 22000) Then
Label5.Text = a - 500
ElseIf (22000 <= a < 23000) Then
Label5.Text = a - 525
ElseIf (23000 <= a < 24000) Then
Label5.Text = a - 550
ElseIf (24000 <= a < 25000) Then
Label5.Text = a - 575
ElseIf (25000 <= a < 26000) Then
Label5.Text = a - 600
ElseIf (26000 <= a < 27000) Then
Label5.Text = a - 625
ElseIf (27000 <= a < 28000) Then
Label5.Text = a - 650
ElseIf (28000 <= a < 29000) Then
Label5.Text = a - 675
ElseIf (29000 <= a < 30000) Then
Label5.Text = a - 700
ElseIf (30000 <= a < 31000) Then
Label5.Text = a - 725
ElseIf (31000 <= a < 32000) Then
Label5.Text = a - 750
ElseIf (32000 <= a < 33000) Then
Label5.Text = a - 800
ElseIf (33000 <= a < 34000) Then
Label5.Text = a - 825
ElseIf (34000 <= a < 35000) Then
Label5.Text = a - 850
ElseIf (a >= 35000) Then
Label5.Text = a - 875
ElseIf a = "" Then
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The farthest the condition was able to go to -225 even if I put 20000 in it. It will only subtract 225 from the 20000. Is there something wrong on what I did or is there a better way to do it?
Looks like you're missing some AndAlso from your If/ElseIf statements. Normally, you wouldn't be able to go from String to Integer without doing a conversion, so I'm guessing you don't have Option Strict
Try
Dim a As Integer = msalary.Text
If (a < 9000) Then
Label5.Text = a - 200
ElseIf (9000 <= a AndAlso a < 10000) Then
Label5.Text = a - 225
ElseIf (10000 <= a AndAlso a < 11000) Then
Label5.Text = a - 250
ElseIf (11000 <= a AndAlso a < 12000) Then
Label5.Text = a - 275
ElseIf (12000 <= a AndAlso a 13000) Then
Label5.Text = a - 300
ElseIf (13000 <= a AndAlso a < 14000) Then
Label5.Text = a - 325
ElseIf (14000 <= a AndAlso a < 15000) Then
Label5.Text = a - 350
ElseIf (15000 <= a AndAlso a < 16000) Then
Label5.Text = a - 375
ElseIf (17000 <= a AndAlso a < 18000) Then
Label5.Text = a - 400
ElseIf (18000 <= a AndAlso a < 19000) Then
Label5.Text = a - 425
ElseIf (19000 <= a AndAlso a < 20000) Then
Label5.Text = a - 450
ElseIf (20000 <= a AndAlso a < 21000) Then
Label5.Text = a - 475
ElseIf (21000 <= a AndAlso a < 22000) Then
Label5.Text = a - 500
ElseIf (22000 <= a AndAlso a < 23000) Then
Label5.Text = a - 525
ElseIf (23000 <= a AndAlso a < 24000) Then
Label5.Text = a - 550
ElseIf (24000 <= a AndAlso a < 25000) Then
Label5.Text = a - 575
ElseIf (25000 <= a AndAlso a < 26000) Then
Label5.Text = a - 600
ElseIf (26000 <= a AndAlso a < 27000) Then
Label5.Text = a - 625
ElseIf (27000 <= a AndAlso a < 28000) Then
Label5.Text = a - 650
ElseIf (28000 <= a AndAlso a < 29000) Then
Label5.Text = a - 675
ElseIf (29000 <= a AndAlso a < 30000) Then
Label5.Text = a - 700
ElseIf (30000 <= a AndAlso a < 31000) Then
Label5.Text = a - 725
ElseIf (31000 <= a AndAlso a < 32000) Then
Label5.Text = a - 750
ElseIf (32000 <= a AndAlso a < 33000) Then
Label5.Text = a - 800
ElseIf (33000 <= a AndAlso a < 34000) Then
Label5.Text = a - 825
ElseIf (34000 <= a AndAlso a < 35000) Then
Label5.Text = a - 850
ElseIf (a >= 35000) Then
Label5.Text = a - 875
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I wouldn't use such a giant conditional branch, I'd find a more algorithmic approach.
Example:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim salary As Integer = 34567, subtractor As Integer = 200
Dim r As Range = GetSalaryRange(salary)
For I As Integer = 9000 To r.Lower Step 1000
subtractor += 25
Next
Label5.Text = (salary - subtractor).ToString
End Sub
Function GetSalaryRange(salary As Integer) As Range
If salary < 9000 Then Return New Range() With {.Lower = 0, .Upper = 8999}
Dim remainder As Integer = salary Mod 1000
Return New Range With {.Lower = salary - remainder, .Upper = salary - remainder + 999}
End Function
Public Class Range
Public Lower, Upper As Integer
Public Function Contains(number As Integer) As Boolean
If number >= Lower AndAlso number <= Upper Then Return True Else Return False
End Function
End Class
End Class