Case statement only runs one condition - vb.net

hey when I enter an integer into my case statement it produces the right output but with a string it just produces the same result each time, do you guys know any solutions?
Select Case Grades.Text
Case = "A*"
score = score + 100
MessageBox.Show("You entered an A*")
Gcount = Gcount + 1
Case >= 90
score = score + 100
MessageBox.Show("You entered an A*")
Gcount = Gcount + 1
Case = "A"
score = score + 85
MessageBox.Show("You entered an A")
Gcount = Gcount + 1
MessageBox.Show(Gcount)
Case < 90 And Grades.Text >= 80
score = score + 85
MessageBox.Show("You entered an A")
Gcount = Gcount + 1
MessageBox.Show(Gcount)
Case = "B"
score = score + 75
MessageBox.Show("You entered a B")
Gcount = Gcount + 1
MessageBox.Show(score)
Case < 80 And Grades.Text >= 70
score = score + 75
MessageBox.Show("You entered a B")
Gcount = Gcount + 1
Case = "C"
score = score + 65
MessageBox.Show("You entered a C")
Gcount = Gcount + 1
Case < 70 And Grades.Text >= 60
score = score + 65
MessageBox.Show("You entered a C")
Gcount = Gcount + 1

You should set Option Strict On.
So there is a way like this
Select case True
Case intVariable <= 90
' do something...
Case 91 < intVariable AndAlso intVariable <= 100
' do something...
Case 100 < intVariable AndAlso intVariable <= 110
' do something... and so on
End Select

try this:
Select Case Grades.Text
Case Is >= 90
MessageBox.Show("You entered an A*")
Case Is < 90 And Grades.Text >= 80
MessageBox.Show("You entered an A")
Case Is < 80 And Grades.Text >= 70
MessageBox.Show("You entered a B")
Case Is < 70 And Grades.Text >= 60
MessageBox.Show("You entered a C")
End Select

Related

How do I make the results for a invalid option show

need help to make the input of 101 show as invalid instead of A* as an output on a console app from visual studio code
Imports System
Module Program
Sub Main(args As String())
Dim mark As Byte
Dim grade As String
Console.WriteLine("enter a mark out of 100")
mark = Console.ReadLine
If mark <= 39 Then
grade = "u"
ElseIf mark <= 49 Then
grade = ("e")
ElseIf mark <= 59 Then
grade = ("d")
ElseIf mark <= 69 Then
grade = ("C")
ElseIf mark <= 79 Then
grade = ("B")
ElseIf mark <= 89 Then
grade = ("A")
ElseIf mark >= 99 Then
grade = ("A*")
Else
grade = ("invailid")
End If
Console.WriteLine()
Console.WriteLine("Grade is :" & grade)
Console.ReadLine()
End Sub
End Module

How to calculate average in visual basic code?

So I was just wondering how to calculate an average in visual basic code?
I currently have a form created and the user is to enter 6 numbered for 6 courses and they must be in textboxes. I know that average is the 6 numbers added together divided by the count but I don't know how to grab the numbers from the textbox to calculate the average.
I've searched online for the answer to this but nothing pertains to this exact problem. My textbook is also of no help.
Any help would be greatly appreciated.
Dim input As Integer
If Integer.TryParse(InputTextbox1.Text, input) Then
If input >= 92 And input <= 100 Then
OutputTextbox1.Text = "A+"
ElseIf input >= 88 And input <= 91 Then
OutputTextbox1.Text = "A"
ElseIf input >= 85 And input <= 87 Then
OutputTextbox1.Text = "A-"
ElseIf input >= 82 And input <= 84 Then
OutputTextbox1.Text = "B+"
ElseIf input >= 78 And input <= 81 Then
OutputTextbox1.Text = "B"
ElseIf input >= 75 And input <= 77 Then
OutputTextbox1.Text = "B-"
ElseIf input >= 72 And input <= 74 Then
OutputTextbox1.Text = "C+"
ElseIf input >= 68 And input <= 71 Then
OutputTextbox1.Text = "C"
ElseIf input >= 65 And input <= 67 Then
OutputTextbox1.Text = "C-"
ElseIf input >= 55 And input <= 64 Then
OutputTextbox1.Text = "D"
ElseIf input <= 54 Then
OutputTextbox1.Text = "F"
End If
Else
ErrorTextbox.Text = "Please ensure that what you input is a number between 0 and 100"
End If
This is the code I have currently there is 6 textboxes using the above code to transfer numbers to letters. The numbers that the user enters is what i need to calculate into the average.
Thanks
Try this:
first thing i did is that i total all textbox numbers and then divide to total number so that i can get the average
Note: dont allow textbox to input a letters because it will error
i converted the textbox text to double so that it will consider as number and not letters.
Public Class Form4
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim average As Double = 0.0
Dim total As Double = 0.0
total = CDbl(TextBox1.Text) + CDbl(TextBox2.Text) + CDbl(TextBox3.Text) + CDbl(TextBox4.Text) + CDbl(TextBox5.Text) + CDbl(TextBox6.Text)
average = total / 6
TextBox7.Text = average.ToString()
End Sub
End Class
Modified: The label_grade is the letter of the grade
Public Class Form4
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim average As Double = 0.0
Dim total As Double = 0.0
total = CDbl(TextBox1.Text) + CDbl(TextBox2.Text) + CDbl(TextBox3.Text) + CDbl(TextBox4.Text) + CDbl(TextBox5.Text) + CDbl(TextBox6.Text)
average = total / 6
TextBox7.Text = average.ToString()
If average >= 92 And average <= 100 Then
label_Grade.Text = "A+"
ElseIf average >= 88 And average <= 91 Then
label_Grade.Text = "A"
ElseIf average >= 85 And average <= 87 Then
label_Grade.Text = "A-"
ElseIf average >= 82 And average <= 84 Then
label_Grade.Text = "B+"
ElseIf average >= 78 And average <= 81 Then
label_Grade.Text = "B"
ElseIf average >= 75 And average <= 77 Then
label_Grade.Text = "B-"
ElseIf average >= 72 And average <= 74 Then
label_Grade.Text = "C+"
ElseIf average >= 68 And average <= 71 Then
label_Grade.Text = "C"
ElseIf average >= 65 And average <= 67 Then
label_Grade.Text = "C-"
ElseIf average >= 55 And average <= 64 Then
label_Grade.Text = "D"
ElseIf average <= 54 Then
label_Grade.Text = "F"
End If
End Sub

Automate a Excel VBA Function

I Need a help in Excel VBA. I want to develop a function that will automatically apply the function to selected range of Cells. My Sample Code is Here it work Independently for a single row I want to apply it for minimum 500 rows.
Sub Value() ' ' Value For the Insurance Rate
Dim percentage As Double
Dim year As Double
percentage = Sheet5.Range("R2").Value
year = Sheet5.Range("Q2").Value
If percentage <= 85 And year <= 25 Then
Sheet5.Range("S2").Value = Sheet4.Range("D13").Value
ElseIf percentage <= 85 And year > 25 Then
Sheet5.Range("S2").Value = Sheet4.Range("C13").Value
ElseIf percentage > 85 And percentage <= 90 And year <= 25 Then
Sheet5.Range("S2").Value = Sheet4.Range("D8").Value
ElseIf percentage > 85 And percentage <= 90 And year > 25 Then
Sheet5.Range("S2").Value = Sheet4.Range("C8").Value
ElseIf percentage > 90 And percentage <= 95 And year <= 25 Then
Sheet5.Range("S2").Value = Sheet4.Range("D5").Value
ElseIf percentage > 90 And percentage <= 95 And year > 25 Then
Sheet5.Range("S2").Value = Sheet4.Range("C5").Value
ElseIf percentage > 95 And year <= 25 Then
Sheet5.Range("S2").Value = Sheet4.Range("D3").Value
ElseIf percentage > 95 And year > 25 Then
Sheet5.Range("S2").Value = Sheet4.Range("C3").Value
End If
End Sub
You're pretty close. I just converted your procedure into a User Defined Function, which can then be used like built-in Excel functions in the Excel Window.
Here's the code:
Function myValue(percentage As Double, year As Double) ''Value For the Insurance Rate
If percentage <= 85 And year <= 25 Then
myValue = Sheet4.Range("D13").Value
ElseIf percentage <= 85 And year > 25 Then
myValue = Sheet4.Range("C13").Value
ElseIf percentage > 85 And percentage <= 90 And year <= 25 Then
myValue = Sheet4.Range("D8").Value
ElseIf percentage > 85 And percentage <= 90 And year > 25 Then
myValue = Sheet4.Range("C8").Value
ElseIf percentage > 90 And percentage <= 95 And year <= 25 Then
myValue = Sheet4.Range("D5").Value
ElseIf percentage > 90 And percentage <= 95 And year > 25 Then
myValue = Sheet4.Range("C5").Value
ElseIf percentage > 95 And year <= 25 Then
myValue = Sheet4.Range("D3").Value
ElseIf percentage > 95 And year > 25 Then
myValue = Sheet4.Range("C3").Value
End If
End Function
Then in cell S2 (or any other cell you want to use the function) you would enter
=myValue(R2,Q2)
Also note, that I change the name of the function to myValue to not run into issues with the built in Value members in VBA.

visual basics equal to or greater than

I am trying to write some code that says if textbox1 is equal to 0 between 10 then HandDecimal = 1. Else if textbox1 is equal to 10.1 through 100, then HandDecimal = 3. Else if textbox1 is equal to 100.1 and greater than HandDecimal = 5.
Here is my code, but it does not seem to work for me.
If WeightDecimal = 0 <= 10 Then
HandDecimal = 1
ElseIf WeightTextBox.Text = 10 <= 100 Then
HandDecimal = 3
ElseIf WeightTextBox.Text >= 100.1 Then
HandDecimal = 5
End If
How do I have to change the code to make it work?
Dim weight as Decimal = Decimal.Parse(WeightTextBox.Text)
If weight >= 0 AndAlso weight <= 10 Then
HandDecimal = 1
ElseIf weight > 10 AndAlso weight <= 100 Then
HandDecimal = 3
ElseIf weight > 100 Then
HandDecimal = 5
End If
Select Case statement with To operator
Select Case WeightDecimal
Case 0 To 10
HandDecimal = 1
Case 10.1 To 100
HandDecimal = 3
Case Else
HandDecimal = 5
End Select

Visual Basic - "If" Statements Not Showing Result In Label

I'm working on an assignment where the program needs to calculate and display the total cost for shipping a package, based on weight and whether it is being shipped to Continental U.S., Alaska, or Hawaii. When I click the Calculate button, though, the label that is supposed to display the total is left blank. I've looked through this and tried placing the calculation in different parts/at the end of the "If" statements. Here's what I have thus far, any help would be appreciated:
Dim decWeight As Decimal
Dim decTotalCost As Decimal
Dim decDestination As Decimal
Dim decRate As Decimal
If IsNumeric(txtWeight.Text) Then
decWeight = Convert.ToDecimal(txtWeight.Text)
If decWeight <= 30 > 0 Then
If radContinental.Checked Then
decDestination = 1
ElseIf radHawaii.Checked Then
decDestination = 1.2
ElseIf radAlaska.Checked Then
decDestination = 1.26
End If
If decWeight <= 2 Then
decRate = 3.69
ElseIf decWeight <= 4 > 2 Then
decRate = 4.86
ElseIf decWeight <= 6 > 4 Then
decRate = 5.63
ElseIf decWeight <= 8 > 6 Then
decRate = 5.98
ElseIf decWeight <= 10 > 8 Then
decRate = 6.28
ElseIf decWeight <= 30 > 10 Then
decRate = 15.72
End If
decTotalCost = decRate * decDestination
lblTotalCost.Text = decTotalCost.ToString("C")
ElseIf decWeight <= 0 Then
MsgBox("Please Enter a Positive Weight.", , "Input Error")
ElseIf decWeight > 30 Then
MsgBox("You Have Entered " & decWeight.ToString() & ". Please Enter a Weight Under 30 Pounds", , "Input Error")
End If
ElseIf txtWeight.Text = "" Then
MsgBox("Please Enter the Weight", , "Input Error")
Else : MsgBox("Please Enter a Number", , "Input Error")
End If
You should try this if statement: If decWeight <= 30 and decWeight > 0 Then
This will check if the decWeight is less than or equal to 30 and make sure that it is 'non-zero' Hope this helps :-)