Select Case between number - vb.net

I try a select case between number. Each 150000 the code the Textbox5 change on click button.
Select Case TextBox5.Text
Case 0 To 150000
TextBox6.Text = "-"
Case 150001 To 300001
TextBox6.Text = "+1-"
Case 300002 To 450002
TextBox6.Text = "+2-"
Case 450003 To 600003
TextBox6.Text = "+3-"
Case 600004 To 750004
TextBox6.Text = "+4-"
Case 750005 To 900005
TextBox6.Text = "+5-"
Case 900006 To 1050006
TextBox6.Text = "+6-"
Case Else
TextBox6.Text = "+Extra-"
End Select
When I try any number between 900006 to 1050006 I have "+Extra-" not "+6-"
and try over 1050006 I have "-"

This is interesting. But before I try to find out what it's causing, I provide you the correct way to do this: a String is not a number, set Option Strict to On and learn to use type safe code. Don't let the compiler guess what you're trying to achieve.
You can use Int32.TryParse:
Dim number As Int32
If Not Int32.TryParse(TextBox5.Text, number) Then
MessageBox.Show("Please enter a valid integer")
Return
End If
Select Case number ' now integer is the target type
Case 0 To 150000
TextBox6.Text = "-"
Case 150001 To 300001
TextBox6.Text = "+1-"
Case 300002 To 450002
TextBox6.Text = "+2-"
Case 450003 To 600003
TextBox6.Text = "+3-"
Case 600004 To 750004
TextBox6.Text = "+4-"
Case 750005 To 900005
TextBox6.Text = "+5-"
Case 900006 To 1050006
TextBox6.Text = "+6-"
Case Else
TextBox6.Text = "+Extra-"
End Select
This will work and compile also with Option Strict On.
Now why your code doesn't work. If you change the last range to Case 900006 To 999999 it will work as expected. This has to do how strings are compared. Even if this compiles (Strict Off) the Case will treat the range 900006 To 1050006 as strings so as "900006" To "1050006", so they are compared letter for letter from left to right, sinvce "9" is "greater" than "1" this condition is never true, that's why you never get into the last Case but into the Case Else.
Documentation:
The expressions in expressionlist can be of any data type, provided
they are implicitly convertible to the type of testexpression and the
appropriate comparison operator is valid for the two types it is being
used with.
With Option Strict Off the expressionlist (the range) is converted to the type of testexpression which is String (because of TextBox5.Text).
With Option Strict On you correctly get a compiler error because a String is not an Integer:
Option Strict On disallows implicit conversions from 'Integer' to
'String'

I agree with Tim Schmelter. Complementing its answer you should use
Select Case CLng(TextBox5.Text)
See https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functions for details and while you are at it avoid ids like TextBox5

Here is another way to do this, and it also works if using ASP Classic, which does not support the "To" operator. This code also shows the use of a nested Select/Case structure, within the first Select/Case:
CardName = Left(CardNumber, 4)
Select Case CardName
Case 1800
GetCardName = "JCB (Japanese Credit Bureau)"
Case 2014
GetCardName = "enRoute"
Case 2131
GetCardName = "JCB (Japanese Credit Bureau)"
Case 2149
GetCardName = "enRoute"
'Case 3000 To 3059:
Case (CardName => 3000 And CardName <= 3059)
GetCardName = "Diners Club"
'Case 3400 To 3499
Case (CardName => 3400 And CardName <= 3499)
GetCardName = "American Express"
'Case 3528 To 3589
Case (CardName => 3528 And CardName <= 3589)
GetCardName = "JCB (Japanese Credit Bureau)"
'Case 3600 To 3699
Case (CardName => 3600 And CardName <= 3699)
GetCardName = "Diners Club"
'Case 3700 To 3799
Case (CardName => 3700 And CardName <= 3799)
GetCardName = "American Express"
'Case 3800 To 3889
Case (CardName => 3800 And CardName <= 3889)
GetCardName = "Diners Club"
'Case 3890 To 3899
Case (CardName => 3890 And CardName <= 3899)
GetCardName = "carteBlanche"
'Case 4000 To 4999
Case (CardName => 4000 And CardName <= 4999)
GetCardName = "VISA"
'Case 5100 To 5599
Case (CardName => 5100 And CardName <= 5599)
GetCardName = "MasterCard"
Case 5610
GetCardName = "Australian BankCard"
Case 6011
GetCardName = "Discover"
Case Else
Select Case Left(CardNumber, 1)
Case 1 'Airline
GetCardName = "Other (Airline)"
Case 2 'Airline and other industry assignments
GetCardName = "Other (Airline and other idustries)"
Case 3 'Travel/Entertainment
GetCardName = "Other (Travel/Entertainment)"
Case 4 'Banking and financial
GetCardName = "Other (Banking/Financial)"
Case 5 'Banking and financial
GetCardName = "Other (Banking/Financial)"
Case 6 'Merchandising and banking
GetCardName = "Other (Merchandising/Banking)"
Case 7 'Petroleum
GetCardName = "Other (Gas Company)"
Case 8 'HealthCare/Telecommunications and other
GetCardName = "Other (HealthCare/Telecomm/Etc)"
Case 9 'National assignment
GetCardName = "Other [Card Carrier Unknown]"
Case 0 'ISO/TC 68 and other industry assignments
GetCardName = "Other [Card Carrier Unknown]"
End Select
End Select

Dim i as Long
If Long.TryParse(TextBox5.Text, i)
Select Case i
Case 0 To 150000
TextBox6.Text = "-"
Case 150001 To 300001
TextBox6.Text = "+1-"
Case 300002 To 450002
TextBox6.Text = "+2-"
Case 450003 To 600003
TextBox6.Text = "+3-"
Case 600004 To 750004
TextBox6.Text = "+4-"
Case 750005 To 900005
TextBox6.Text = "+5-"
Case 900006 To 1050006
TextBox6.Text = "+6-"
Case Else
TextBox6.Text = "+Extra-"
End Select
Else
TextBox6.Text = "Not a Number"
End If
Modify to your requirements.

Related

Issue converting percentage into grade - all results show as an A

This is a pretty simple project, for some reason the output always shows the results that are under Case >= 85, so an A. Could anyone help me see why? Thanks
Public Class frmMarkBook
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim intScore As Integer 'variable for the score entered
Select Case intScore = CInt(txtScore.Text) 'converts score to an integer
Case >= 85
lblGradeResult.Text = ("A")
txtGradeComment.Text = ("Excellent result")
Case 70 To 84.99
lblGradeResult.Text = ("B")
txtGradeComment.Text = ("Very good result")
Case 55 To 69.99
lblGradeResult.Text = ("C")
txtGradeComment.Text = ("Satisfactory result")
Case 40 To 54.99
lblGradeResult.Text = ("D")
txtGradeComment.Text = ("Needs some improvements")
Case 0 To 39.99
lblGradeResult.Text = ("E")
txtGradeComment.Text = ("Needs to repeat task")
Case Else
MessageBox.Show("Please enter a valid percentage")
End Select
End Sub
The very first thing you should do is turn Option Strict On, both in the project properties and in the VS options, so it's On by default for future projects. That would have told you that that code doesn't make sense by refusing to compile.
This is wrong:
Select Case intScore = CInt(txtScore.Text)
In C# that would assign a value to intScore and then evaluate that value but in VB that's a comparison, not an assignment. That is equivalent to this:
Dim bool As Boolean = (intScore = CInt(txtScore.Text))
Select Case bool
If you want an assignment then you have to actually perform an assignment, not a comparison:
intScore = CInt(txtScore.Text)
Select Case intScore
You can do something like this.
Dim intScore As Integer
intScore = CInt(txtScore.Text)
Select Case intScore
Case Is >= 85
lblGradeResult.Text = ("A")
txtGradeComment.Text = ("Excellent result")
Case 70 To 84.99
lblGradeResult.Text = ("B")
txtGradeComment.Text = ("Very good result")
Case 55 To 69.99
lblGradeResult.Text = ("C")
txtGradeComment.Text = ("Satisfactory result")
Case 40 To 54.99
lblGradeResult.Text = ("D")
txtGradeComment.Text = ("Needs some improvements")
Case 0 To 39.99
lblGradeResult.Text = ("E")
txtGradeComment.Text = ("Needs to repeat task")
Case Else
MessageBox.Show("Please enter a valid percentage")
End Select

VBA Select Case runs through without selection

I am really puzzled by this one. Searched the net already but did not find any answer related to my specific case. I have the following code:
Dim CE_res As Integer
For Index = 1 To 7
Status = ""
CE_res = CInt(Worksheets("werkblad").Range("CEres" & Index).Value)
If CE_res = 0 Then
' Everything ok
Status = "Pass"
Else
Select Case CE_res
Case CE_res < -1500
Status = "Invalid kV Value"
Case CE_res < -999
Status = "No kV Value"
Case CE_res < -399
Status = "kV too high"
Case CE_res = 0
Status = "kV too low"
Case CE_res > 500
Status = "Invalid mAs Value"
Case CE_res > 49
Status = "mAs Value missing"
Case CE_res > 9 And CE_res < 50
Status = "Adapt Target"
Case CE_res > 200
Status = "Adapt Filter"
Case Else
Status = "No Selection"
End Select
End If
The CE_res is set to 50. However, always Case Else is selected. Declared CE_res as integer, coverted Worksheets("werkblad").Range("CEres" & Index).Value to an integer, just to be sure.
However, it does not seem to execute the Select Case correctly. Tried it also with other values, even changed the case CE_res > 49 to CE_res = "50" but this also did not work.
Now, I am out of ideas what could be wrong.
You are not using the correct syntax for the Case statements
Dim CE_res As Integer
For Index = 1 To 7
Status = ""
CE_res = CInt(Worksheets("werkblad").Range("CEres" & Index).Value)
If CE_res = 0 Then
' Everything ok
Status = "Pass"
Else
Select Case CE_res
Case < -1500
Status = "Invalid kV Value"
Case < -999
Status = "No kV Value"
Case < -399
Status = "kV too high"
Case 0 ' or possibly Case < 0 ????
Status = "kV too low"
Case > 500
Status = "Invalid mAs Value"
Case > 200
Status = "Adapt Filter"
Case > 49
Status = "mAs Value missing"
Case > 9
Status = "Adapt Target"
Case Else
Status = "No Selection"
End Select
End If
Note that I had to move > 200 prior to > 49, otherwise a value of, for instance, 230 would have matched > 49 and therefore never reached your > 200.
The way you had it, the statement Case CE_res < -1500 would test if CE_res was < -1500. If it was, that would return a True which was then compared to the object of the case statement (i.e. CE_res) and, if it matched (which it wouldn't) that leg of the Select statement would execute.
As mentioned by other members, due to your use of < > = comparitors you need to make sure you order them correctly or they will not work as intended. With the very specific values you are using you would probably be better to use TO and be more specific with the values.
Select Case
Case -1000 TO -1500
'Do This
Case -400 TO -999
'Do That
Case -1 TO -399
'Do Something Else
End Select
If you do this the there is no ambiguity as to what does what, it's easier to read and the operation order doesn't matter.
You also don't need to add the CE_res to each Case statement as you already specified the variable on the Select Case CE-res line. The Correct syntax is
Select Case CE_res
Case <-1500
Status = "Invalid kV Value"
End Select

Select Case conversion not displaying properly, how can i fix?

I have an executable that when run takes an inputted date and then selects what date suffix is needed with it, however it is always defaulting to the case else.
when i have 02/04/2017 as format DD/MM/YYYY the result of the below case is 2th April instead of 2nd...
Could anyone enlighten me on the problem.
Dim Datewc As Date = Nothing
If CheckBox1.Checked Then
Datewc = TextBox1.Text
End If
'Determine date suffix
Dim datsuff As String = ""
Select Case CInt(Datewc.Day)
Case 1 Or 21 Or 31
datsuff = "st"
Case 2 Or 22
datsuff = "nd"
Case 3 Or 23
datsuff = "rd"
Case Else
datsuff = "th"
End Select
Parse you date
Dim dDate As DateTime =
DateTime.ParseExact(TextBox1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Change your code a little, should work.
Day function returns integer, no need to Cast using CInt.
'Determine date suffix
Dim datsuff As String = ""
Select Case Datewc.Day
Case 1 , 21 , 31
datsuff = "st"
Case 2 , 22
datsuff = "nd"
Case 3 , 23
datsuff = "rd"
Case Else
datsuff = "th"
End Select
Explanation
Case 2 Or 22 means Case ((2 Or 22) ==true), It will be false hence going to else part.

how to create an experience / level up system?

I'm creating a DnD Character Creation program, and I've gotten myself stuck at the "Experience / Level" area. What I want is for every 1000 experience, the level to go up (so 0 to 999 is level 0,
So, I've used the following code to get where I am, but it doesn't change the label (lblLevel) to 1 when experience (txtExperience) is changed to 1000 (or 2 when experience is changed to 2000).
Private Sub txtExperience_textChanged(sender As Object, e As EventArgs) Handles txtExperience.TextChanged
If txtExperience.Text = letters Then
lblLevel.Text = "0"
ElseIf txtExperience.Text >= 10000 Then
lblLevel.Text = "Maxed"
End If
Select Case txtExperience.Text
Case Is <= "999"
lblLevel.Text = "0"
Case "1000" To "1999"
lblLevel.Text = "1"
Case "2000" To "2999"
lblLevel.Text = "2"
Case "3000" To "3999"
lblLevel.Text = "3"
Case "4000" To "4999"
lblLevel.Text = "4"
Case "5000" To "5999"
lblLevel.Text = "5"
Case "6000" To "6999"
lblLevel.Text = "6"
Case "7000" To "7999"
lblLevel.Text = "7"
Case "8000" To "8999"
lblLevel.Text = "8"
Case "9000" To "9999"
lblLevel.Text = "9"
End Select
End Sub
I'll be honest, I'm not sure if I'm using Select Case ... correctly, and when I tried using an If Statement (and Else If in place each of the Case ... to ...) it wouldn't work either. Any help would be greatly appreciated.
You could simplify that even further. Using basic math you can get rid of the long Select Case statement:
Dim Experience As Integer = 0
If Integer.TryParse(txtExperience.Text, Experience) = True Then
If Experience >= 10000 Then
lblLevel.Text = "Maxed"
Return
End If
lblLevel.Text = Math.Floor(Experience / 1000).ToString()
Else
MessageBox.Show("Input must be a whole number between 0 and 10000", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Here's an online test of my code: https://dotnetfiddle.net/VtGFLx
There is a difference between integer and string
Select Case Integer.Parse(txtExperience.Text)
Case Is <= 999
lblLevel.Text = 0
Case 1000 To 1999
lblLevel.Text = 1
Case 2000 To 2999
lblLevel.Text = 2
Case 3000 To 3999
lblLevel.Text = 3
Case 4000 To 4999
lblLevel.Text = 4
Case 5000 To 5999
lblLevel.Text = 5
Case 6000 To 6999
lblLevel.Text = 6
Case 7000 To 7999
lblLevel.Text = 7
Case 8000 To 8999
lblLevel.Text = 8
Case 9000 To 9999
lblLevel.Text = 9
End Select

Calculation Issue in VB 2010

I am having trouble figuring out where my issue with this code is. My calculations work perfectly until the last case (CASE IS > 8), which continuously returns a 0.00 as the result. I'm sure its something small that a newbie like me is missing due to lack of experience. Thank you for your help!
' Declaration of Variable
Convert.ToInt32(txtAttending.Text)
Dim decAttending = txtAttending.Text
If IsNumeric(txtAttending.Text) And txtAttending.Text <= 16 Then
Select Case txtAttending.Text
Case Is = 1
decCost = 695 * decAttending
Case 2 To 4
decCost = 545 * decAttending
Case 5 To 8
decCost = 480 * decAttending
Case Is > 8
decCost = 395 * decAttending
End Select
Else
MsgBox("Please double check that your input is a number not greater than 16", , "Input Error")
End If
If radYes.Checked = True Then
decFinalCost = (decCost - (decCost * 0.15))
lblRepeatDiscount.Visible = True
decDiscount = (decCost * 0.15)
lblDiscount.Text = decDiscount.ToString("C")
lblTotalPrice.Text = decFinalCost.ToString("C")
Else
decFinalCost = decCost
lblTotalPrice.Text = decFinalCost.ToString("C")
End If
Convert.ToInt32(txtAttending.Text) does not convert the text in-place, as your first line of code seems to assert; this is a no-op. txtAttending.Text has type System.String, and will always have that type.
Your case statement should look more like this:
If IsNumeric(txtAttending.Text) Then
Dim decAttending = Convert.ToInt32(txtAttending.Text)
Select Case decAttending
Case Is = 1
decCost = 695 * decAttending
Case 2 To 4
decCost = 545 * decAttending
Case 5 To 8
decCost = 480 * decAttending
Case 9 To 16
decCost = 395 * decAttending
End Select
Else
MsgBox("Please double check that your input is a number not greater than 16", , "Input Error")
This version always uses the System.Int32 variable decAttending when doing numeric operations.
Case Else
decCost = 395 * decAttending
Shouldn't you convert the text to a number first? I'd change these lines:
If IsNumeric(txtAttending.Text) And Val(txtAttending.Text) <= 16 Then
Select Case Val(txtAttending.Text)
Notice that this snippet has no effect: Convert.ToInt32(txtAttending.Text), you're converting a text to an integer, but the result is not being stored in a variable.
Also, what happens if the number is <= 0 ?
Apart from mentioned advices, it seems great, but
are you sure that all code you did is written, or is there a thrown code that manipulate
Diccost,or disattending,please assure us that.