Good afternoon all, I am beginning my first forays into programming and have decided to begin with VB.net as I can get VS2010 professional free through MS Dreamspark program.
I have been following some basic tutorials online and am now writing a small program that runs a loop to add all the numbers together between two numbers input by the user.
Below is the code I have written:
Public Class Form1
Private Sub cmdAddNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNumbers.Click
Dim NumberOne As Integer
Dim NumberTwo As Integer
Dim Result As Integer
Dim i As Integer
If Not IsNumeric(txtNumberOne.Text) Then
MsgBox("Please Enter A Valid Number For Number One")
txtNumberOne.Clear()
Exit Sub
ElseIf txtNumberOne.Text = 0 Then
MsgBox("Please Enter A Valid Number For Number One")
txtNumberOne.Clear()
Exit Sub
ElseIf txtNumberOne.Text > 0 And IsNumeric(txtNumberOne.Text) Then
NumberOne = txtNumberOne.Text
End If
If Not IsNumeric(txtNumberTwo.Text) Then
MsgBox("Please Enter A Valid Number For Number Two")
txtNumberTwo.Clear()
Exit Sub
ElseIf txtNumberTwo.Text < NumberOne Then
MsgBox("Please Enter A Valid Number For Number Two")
txtNumberTwo.Clear()
Exit Sub
ElseIf txtNumberTwo.Text > NumberOne And IsNumeric(txtNumberTwo.Text) Then
NumberTwo = txtNumberTwo.Text
End If
For i = NumberOne To NumberTwo
Result = Result + i
Next i
txtResult.Text = Result
txtNumberOne.Clear()
txtNumberTwo.Clear()
End Sub
End Class
Now, I am wondering if I have written the most efficent If statements to execute this code or if they can be written any simpler with AND/OR statements to possibly remove some of the ElseIf's.
Any insight is greatly appreciated.
Thank you,
Alex
You should start with putting Option Strict On at the top of your code to force yourself to write code without implicit conversions between strings and numbers. An example of a pitfall in your code is where you compare the string value txtNumberTwo.Text to the numeric value NumberOne; it isn't obvious if the string is converted to a number so that the comparison works properly, or if the number is converted to a string so that it does a string comparison instead.
You can use the Int32.TryParse method to parse each number only once instead of three times:
Dim numberOne As Integer
Dim numberTwo As Integer
Dim result As Integer
If Not Int32.TryParse(txtNumberOne.Text, numberOne) Then
MsgBox("Please Enter A Valid Number For Number One")
txtNumberOne.Clear()
Exit Sub
ElseIf numberOne <= 0 Then
MsgBox("Please Enter A Valid Number For Number One")
txtNumberOne.Clear()
Exit Sub
End If
If Not Int32.TryParse(txtNumberTwo.Text, numberTwo) Then
MsgBox("Please Enter A Valid Number For Number Two")
txtNumberTwo.Clear()
Exit Sub
ElseIf numberTwo < numberOne Then
MsgBox("Please Enter A Valid Number For Number Two")
txtNumberTwo.Clear()
Exit Sub
End If
Your loop is not needed at all. You can calculate the sum directly:
Result = (numberOne + numberTwo) * (numberTwo + 1 - numberOne) / 2
What about:
If Not IsNumeric(txtNumberOne.Text) Or txtNumberOne.Text <= 0 Then
MsgBox("Please Enter A Valid Number For Number One")
txtNumberOne.Clear()
Exit Sub
Else
NumberOne = txtNumberOne.Text
End If
If Not IsNumeric(txtNumberTwo.Text) Or txtNumberTwo.Text < NumberOne Then
MsgBox("Please Enter A Valid Number For Number Two")
txtNumberTwo.Clear()
Exit Sub
Else
NumberTwo = txtNumberTwo.Text
End If
Be aware that if NumberOne is equal to Textbox2.Text, NumberTwo is never assigned
i think the best solution for you is just set the textboxes to be able to accept only numbers, that way you can avoid all the checks if the texts is numeric or not and only check if its bigger than zero.
to set the textboxes to accept only numbers copy this function:
Private Function TrapKey(ByVal KCode As String) As Boolean
If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
TrapKey = False
Else
TrapKey = True
End If
End Function
and in the keypress event of the textboxes add
e.Handled = TrapKey(Asc(e.KeyChar))
If Not IsNumeric(txtNumberOne.Text) Then
MsgBox("Please Enter A Valid Number For Number One")
txtNumberOne.Clear()
Exit Sub
ElseIf txtNumberOne.Text = 0 Then
MsgBox("Please Enter A Valid Number For Number One")
txtNumberOne.Clear()
Exit Sub
ElseIf txtNumberOne.Text > 0 And IsNumeric(txtNumberOne.Text) Then
NumberOne = txtNumberOne.Text
End If
The third If is superflious. We know it must be IsNumeric, as it passed the first If, and we know it cannot be 0, as it passed the second If. (You also make no allowances at all, if it happens to be negative)
Now, it been a while since I last did VB.Net, but I'm pretty sure it still have a distinct between strings & integers, which means txtNumberOne.Text = 0 shouldn't even compile.
And also, why are you making your users guess what a "valid number" is?
Dim numberOne as Integer
If IsNumeric(txtNumberOne.Text) Then
numberOne = CInt(txtNumberOne.Text)
else
numberOne = -1;
End If
If numberOne < 1
MsgBox("Please Enter A Positive Number For Number One")
txtNumberOne.Clear()
Exit Sub
End If
Dim doub As Double
If Not (Double.TryParse(txtNumberOne.Text, doub) AndAlso doub > 0) Then
MsgBox("Please Enter A Valid Number For Number One")
txtNumberOne.Clear()
Else
NumberOne = doub
End If
If Not (Double.TryParse(txtNumberTwo.Text, doub) AndAlso doub > 0) Then
MsgBox("Please Enter A Valid Number For Number Two")
txtNumberTwo.Clear()
Else
NumberTwo = doub
End If
I think this is waht you are looking for
Result = (NumberTwo * (NumberTwo + 1) / 2) - ((NumberOne - 1) * (NumberOne) / 2)
Related
private Sub Command1_Click()
a = InputBox("What is the Number ?", "Input Example"," ")
If a = "-1" Then
End If
End Sub
the whole question is: Enter some numbers until a negative value is entered to stop the program(stop running the what is your number thing). then find the average of the entered numbers.
What I want to do, for now, I want to know how can I make my "a" variable accept any negative value like in the code above it stops when I enter -1 but I want to stop when I enter -3, -10, or any negative value.
There are some helpful answers down below
If you are expecting the usual input to be text then you can use the Double.TryParse method to check if a number was entered. If it was, then you can check if that number is negative, and exit the application if so:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim userMsg As String
userMsg = Microsoft.VisualBasic.InputBox("What is your message?", "Message Entry Form", "Enter your message here", 500, 700)
If userMsg <> "" Then
Dim x As Double
If Double.TryParse(userMsg, x) AndAlso x < 0 Then
Application.Exit()
End If
MessageBox.Show(userMsg)
Else
MessageBox.Show("No Message")
End If
End Sub
The AndAlso operator only looks at the second argument if the first evaluated to True.
If you would like to repeat some portion of code till specific condition is met, you need to use:
While...End While Statement (Visual Basic)
or
Do...Loop Statement (Visual Basic)
It's also possible to write conditional loop using For... Next statement
Dim myNumber As Integer = 0
Dim mySum As Integer = 0
Dim myCounter As Integer = 0
Do
Dim answer As Object = InputBox("Enter integer value", "Getting average of integer values...", "")
'check if user clicked "Cancel" button
If answer <> "" Then
'is it a number?
If Int32.TryParse(answer, myNumber)
If myNumber >=0 Then
mySum += myNumber
myCounter += 1
Else
Exit Do
End If
End If
End If
Loop
Dim average As Double = mySum/myCounter
'you can display average now
Tip: Do not use InputBox, because this "control" is VisualBasic specific. Use custom Form instead. There's tons of examples on Google!
I'm still new in VB and I have emailed my lecturer but it seems like he is quite busy and didn't have time to reply to me. Can anyone teach me with it?
Dim split = InputTextBox.Text.Split(vbNewLine)
Dim check As Boolean
For i = 0 To split.Length - 1
check = IsNumeric(split(i))
If Not check Then
Exit For
End If
Next
If check Then
FootForm.Show()
Else
MessageBox.Show("Please enter in positive number only")
End If
I am not sure how your value is stored in the form control. The below should help
Dim split as String = InputTextBox.Text.Split(vbNewLine)
Dim parsedint as Integer
If Int32.TryParse(split, parsedint) AndAlso parsedint < 0 Then
MessageBox.Show("Please enter in positive number only")
Else
FootForm.Show()
End If
You can throw a bit of LINQ at the problem:
If InputTextBox.Lines.All(Function(s)
Dim n As Double
Return Double.TryParse(s, n) AndAlso n >= 0.0
End Function) Then
'All lines represent non-negative numbers.
End If
The longhand version would be:
Dim result = True
For Each s In InputTextBox.Lines
Dim n As Double
If Not Double.TryParse(s, n) OrElse n < 0.0 Then
result = False
Exit For
End If
Next
If result Then
'All lines represent non-negative numbers.
End If
This is the third week of my intro to programming class and I'm stuck. I can get it to run I just can't enter more than 1 number. This is a vb console app.
Module Module1
Sub Main()
Dim highestNumber As Integer = -1000000000
Dim lowestNumber As Integer = 100000000
Dim userInput As Integer
Console.WriteLine("Input your numbers and when you are done enter -99.")
Console.WriteLine("The app will then give the highest and lowest numbers entered.")
userInput = Console.ReadLine()
While userInput <> "-99"
If userInput >= highestNumber Then
highestNumber = userInput
ElseIf userInput <= lowestNumber Then
lowestNumber = userInput
End If
End While
Console.WriteLine("The highest number you entered is: " & highestNumber)
Console.WriteLine("The lowest number you entered is: " & lowestNumber)
Console.ReadLine()
End Sub
End Module
You're only executing ReadLine once, before the loop starts, so you only get to enter one number. As it currently stands, if you don't enter -99 then you have an infinite loop since you cannot change userInput inside the loop.
You need to put a copy of userInput = Console.ReadLine() at the end of the While loop so that a new value can be entered and the logic will be re-executed.
Also, you might as well remove the = signs from your tests. No point changing the highest/lowest unless the new number is actual higher/lower.
Move this line inside your while loop
userInput = Console.ReadLine()
Here's my solution. It checks the input of the user, if it's not an integer they can try again or enter -99 and get out... You can also change how many numbers a user enters by changing a few places where it shows 2, or declare a variable and set it and then use it...
Public Sub Main()
Dim userInput As Integer = 0
Dim lstInteger As New List(Of Integer)
Console.WriteLine("Input your numbers and when you are done enter -99." & Environment.NewLine & "The app will then give the highest and lowest numbers entered.")
Do Until userInput = -99
If Integer.TryParse(Console.ReadLine(),userInput) AndAlso userInput <> -99 Then
lstInteger.Add(userInput)
If lstInteger.Count = 2 Then Exit Do
userInput = 0
If Integer.TryParse(Console.ReadLine(), userInput) AndAlso userInput <> -99 Then
lstInteger.Add(userInput)
If lstInteger.Count = 2 Then Exit Do
Else
If userInput = -99 Then Exit Do Else Console.WriteLine("Please enter a number.")
End If
Else
If userInput <> - 99 AndAlso userInput = 0 Then
Console.WriteLine("Please enter a number.")
Else
Exit Do
End If
End If
Loop
If lstInteger.Count = 2 Then
Console.WriteLine("The highest number you entered is: " & lstInteger.Max.ToString)
Console.WriteLine("The lowest number you entered is: " & lstInteger.Min.ToString)
End If
Console.ReadLine()
End Sub
I am attempting to create a grade calculator program and I am having some two problems:
Getting the right outputs (because I am getting ridiculous numbers) and
Getting my counters to work.
The basic form is basically one enters 7 input grades:
3 exams (weighed 15%, 20%, and 20% respectively)
A project (weighed 10%),
Assignments (weighed 20%),
Peer reviews (weighed 5%) ,
A programming language presentation (weighed 10%)
and the individual is supposed to get an output of their numeric grade, their letter grade, and two counters that count how many people got A's and F's.
For an example when I enter 3 exam grades: 82,87,91; Assignments: 94; Peer reviews: 100; programming language presentation: 90; and final project: 92,
I get a final numeric grade of 253.90 and a letter grade of F when it clearly should be a letter grade of A and numeric 89.90.
My counters also are not working properly because they aren't displaying the count, but I feel like I put it in the right place (displaying outputs). What exactly am I doing wrong here? Here's my code
Option Strict On
Public Class frmGradeCalculator
'declare Constants
Const EXAM1GRADE_WEIGHT As Decimal = 0.15D
Const EXAM2GRADE_WEIGHT As Decimal = 0.2D
Const EXAM3GRADE_WEIGHT As Decimal = 0.2D
Const HOMEWORKGRADE_WEIGHT As Decimal = 0.2D
Const HOMEWORKPEERREVIEW_WEIGHT As Decimal = 0.05D
Const LANGUAGEQUICKREFERENCE_WEIGHT As Decimal = 0.1D
Const FINALPROJECT_WEIGHT As Decimal = 0.1D
'Declare module variables
Dim mdecFinalNumericGrade As Decimal
Dim mstrFinalLetterGrade As String
Dim mintStudentsWithAs As Integer
Dim mintStudentsWithFs As Integer
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'declare variables
Dim decExam1Grade As Decimal
Dim decExam2Grade As Decimal
Dim decExam3Grade As Decimal
Dim decHomeworkGrade As Decimal
Dim decPeerReviewGrade As Decimal
Dim decLanguageReferenceGrade As Decimal
Dim decFinalProjectGrade As Decimal
Dim decPercent As Decimal
'check for blanks
If (txtExam1Grade.Text) = "" Then
MessageBox.Show("You Can't Leave Exam 1 Blank")
Exit Sub
End If
'check for numeric
If IsNumeric(txtExam1Grade.Text) = False Then 'value is not numeric
MessageBox.Show("Please enter a numeric value for Exam 1!")
Exit Sub
End If
'check for blanks
If (txtExam2Grade.Text) = "" Then
MessageBox.Show("Please enter Exam 2!")
Exit Sub
End If
'check for everything else
If IsNumeric(txtExam2Grade.Text) = False Then 'Value is not numeric
MessageBox.Show("Please enter a numeric value for Exam 2!")
Exit Sub
End If
'check for blanks
If (txtExam3Grade.Text) = "" Then
MessageBox.Show("Please enter Exam 3!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtExam3Grade.Text) = False Then
MessageBox.Show("Please enter a positive numeric value for Exam3!")
Exit Sub
End If
'check for blanks
If (txtHomeworkGrade.Text) = "" Then
MessageBox.Show("Please enter Homework Grade!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtHomeworkGrade.Text) = False Then
MessageBox.Show("Please enter a numeric positive value for Homework Grade!")
Exit Sub
End If
'check for blanks
If (txtPeerReviewGrade.Text) = "" Then
MessageBox.Show("Please enter a Peer Review Grade!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtPeerReviewGrade.Text) = False Then
MessageBox.Show("Please enter a numeric positive value for Peer Review Grade!")
Exit Sub
End If
'check for blanks
If (txtLanguageReferenceGrade.Text) = "" Then
MessageBox.Show("Please enter a Language Reference Grade!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtLanguageReferenceGrade.Text) = False Then
MessageBox.Show("Please enter a numeric positive value for Language Reference Grade!")
Exit Sub
End If
'check for blanks
If (txtFinalProjectGrade.Text) = "" Then
MessageBox.Show("Please enter a Final Project Grade!")
Exit Sub
End If
'check for numerics
If IsNumeric(txtFinalProjectGrade.Text) = False Then
MessageBox.Show("Please enter a numeric positive value for Final Project Grade!")
Exit Sub
End If
'convert data types
decExam1Grade = Convert.ToDecimal(txtExam1Grade.Text)
decExam2Grade = Convert.ToDecimal(txtExam2Grade.Text)
decExam3Grade = Convert.ToDecimal(txtExam3Grade.Text)
decHomeworkGrade = Convert.ToDecimal(txtHomeworkGrade.Text)
decPeerReviewGrade = Convert.ToDecimal(txtPeerReviewGrade.Text)
decLanguageReferenceGrade = Convert.ToDecimal(txtLanguageReferenceGrade.Text)
decFinalProjectGrade = Convert.ToDecimal(txtFinalProjectGrade.Text)
mdecFinalNumericGrade = (decExam1Grade * EXAM1GRADE_WEIGHT) + _
(decExam2Grade * EXAM2GRADE_WEIGHT) + _
(decExam3Grade * EXAM3GRADE_WEIGHT) + _
(decHomeworkGrade * HOMEWORKGRADE_WEIGHT) + _
(decPeerReviewGrade * HOMEWORKPEERREVIEW_WEIGHT) + _
(decLanguageReferenceGrade + LANGUAGEQUICKREFERENCE_WEIGHT) + _
(decFinalProjectGrade + FINALPROJECT_WEIGHT)
'check for 0 or positive
If decExam1Grade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Exam 1!")
Exit Sub
End If
'check for 0 or positive
If decExam2Grade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Exam 2!")
Exit Sub
End If
'check for 0 or positive
If decExam3Grade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Exam 3!")
Exit Sub
End If
'check for 0 or positive
If decHomeworkGrade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Homework Grade!")
Exit Sub
End If
'check for 0 or positive
If decPeerReviewGrade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Peer Review Grade!")
Exit Sub
End If
'check for 0 or positive
If decLanguageReferenceGrade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Language Reference!")
Exit Sub
End If
'check for 0 or positive
If decFinalProjectGrade < 0 Then
MessageBox.Show("Please enter a positive value or zero for Final Project Grade!")
Exit Sub
End If
'make sure values are less than 100
If decExam1Grade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decExam2Grade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decExam3Grade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decHomeworkGrade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decPeerReviewGrade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decLanguageReferenceGrade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
If decFinalProjectGrade > 100 Then
MessageBox.Show("Please enter a value thats 100 or less!")
End If
'Determine grade per letter
Select Case decpercent
Case Is >= 89.5D
mstrFinalLetterGrade = "A"
mintStudentsWithAs += 1
Case Is >= 79.5D
mstrFinalLetterGrade = "B"
Case Is >= 69.5D
mstrFinalLetterGrade = "C"
Case Is >= 59.5D
mstrFinalLetterGrade = "D"
Case Else
mstrFinalLetterGrade = "F"
mintStudentsWithFs += 1
End Select
lblFinalLetterGrade.Text = mstrFinalLetterGrade
'display outputs
lblFinalNumericGrade.Text = mdecFinalNumericGrade.ToString("f2")
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'clear the texboxes and labels
txtExam1Grade.Clear()
txtExam2Grade.Clear()
txtExam3Grade.Clear()
txtHomeworkGrade.Clear()
txtPeerReviewGrade.Clear()
txtLanguageReferenceGrade.Clear()
txtFinalProjectGrade.Clear()
lblFinalLetterGrade.Text = ""
lblFinalNumericGrade.Text = ""
'setcursor back to top textbox
txtExam1Grade.Focus()
End Sub
Private Sub btnReset_Click(ByVal sende As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
'reset module level variables
mdecFinalNumericGrade = 0
mstrFinalLetterGrade = ""
mintStudentsWithAs = 0
mintStudentsWithFs = 0
End Sub
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
'close the form
End
End Sub
Private Sub frmGradeCalculator_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Your final grade total is wrong because you're not multiplying the language presentation and final project by their weights, but adding the weight to the score:
Exam1 = 82 * .15 = 12.3
Exam2 = 87 * .20 = 17.4
Exam3 = 91 * .20 = 18.2
Assignments = 94 * .20 = 18.8
Peer Review = 100 * .05 = 5
Language Presentation = 90 + .10 = 90.1
Final Project = 92 + .10 = 92.1
Notice the addition (rather than multiplication) on the last two values. The total is 253.9.
Change the last two calculations for medcFinalNumericGrade to:
(decLanguageReferenceGrade * LANGUAGEQUICKREFERENCE_WEIGHT) + _
(decFinalProjectGrade * FINALPROJECT_WEIGHT)
You will always get an "F" because decpercent is never assigned a value, and therefore the Else Case is executed. Either assign decpercent a value, or use mdecFinalNumericGrade.
For example:
Select Case mdecFinalNumericGrade
instead of
Select Case decpercent
Once you fix the Select Case, your counts should work (unless you reset the form).
Hey guys I have been having this crashing problem with my program.
I am trying to create a program where it calculates a persons account balance by inputting their beginning balance, credit limit, total charges, and total credits. In this case I am having specific problems with the Total Charges and Total Credits Code.
I have a message box set up to where if the Total Charges and total credits box are blank it will say "please enter a numeric value for ....". The problem is that when I do run it and enter a blank, the message shows up and then the program crashes.
After crashing, the The program then highlights in yellow the specific conversion code (in this case: decTotalCharges = Convert.ToDecimal(txtTotalCharges.Text)) and the error says Input string was not in correct format.
What's going on, I converted it into a correct format right? (decimal to decimal?). Here's an in depth look at my code:
Public Class frmEndingBalance
'Declare module level variables
Dim mdecEndingBalance As Decimal
Dim mdecAllCharges As Decimal
Dim mdecAllCredits As Decimal
Dim mintCustomersOverLimit As Integer
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
'clears the form
'clears the labels
txtAccountNumber.Text = ""
txtBeginningBalance.Text = ""
txtTotalCharges.Text = ""
txtTotalCredits.Text = ""
txtCreditLimit.Text = ""
lblEndingBalance.Text = ""
lblCreditMessage.Text = ""
lblAllCharges.Text = ""
lblAllCredits.Text = ""
lblCustomersOverLimit.Text = ""
lblCreditMessage.Text = ""
'clear the textboxes
txtAccountNumber.Clear()
txtBeginningBalance.Clear()
txtTotalCredits.Clear()
txtTotalCharges.Clear()
txtCreditLimit.Clear()
'clear module level variables
mdecEndingBalance = 0
mdecAllCharges = 0
mdecAllCredits = 0
mintCustomersOverLimit = 0
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'Declare Variables
Dim intAccountNumber As Integer
Dim intBeginningBalance As Integer
Dim decTotalCharges As Decimal
Dim decTotalCredits As Decimal
Dim decCreditLimit As Decimal
Dim mdecEndingBalance As Decimal 'Beginning Balance + Charges - Credits()
Dim decCreditMessage As Decimal
'check for numeric
If IsNumeric(txtAccountNumber.Text) = False Then 'value is not numeric
MessageBox.Show("You can't enter anything blank!")
Exit Sub
End If
'convert to a numeric data type
intAccountNumber = Convert.ToInt32(txtAccountNumber.Text)
'check for numeric
If String.IsNullOrEmpty(txtBeginningBalance.Text) Then MessageBox.Show("Beginning Balance Cannot Be Blank!")
'check for everything else
If IsNumeric(txtBeginningBalance.Text) = False Then 'Value is not numeric
MessageBox.Show("Please enter a numeric value for Beginning Balance!")
Exit Sub
End If
'convert to a numeric data type
intBeginningBalance = Convert.ToInt32(txtBeginningBalance.Text)
'check for numeric
If IsNumeric(txtTotalCharges.Text) = False Then 'value is not numeric
MessageBox.Show("Please enter a numeric value for Total Charges!")
End If
'convert
decTotalCharges = Convert.ToDecimal(txtTotalCharges.Text)
'check for 0 or positive
If decTotalCharges < 0 Then
MessageBox.Show("Please enter a positive value or zero for number of Total Charges!")
Exit Sub
End If
'check for numeric
If IsNumeric(txtTotalCredits.Text) = False Then 'value is not numeric
MessageBox.Show("Please enter a numeric value for Total Credits")
End If
'convert to a numeric data type
decTotalCredits = Convert.ToDecimal(txtTotalCredits.Text)
'check for 0 or positive
If decTotalCredits < 0 Then
MessageBox.Show("Please enter a positive value or zero for total credits!")
End If
'check numeric
If IsNumeric(txtCreditLimit.Text) = False Then 'value is not numeric
MessageBox.Show("Please enter a numeric value for the Credit Limit!")
End If
'convert to a numeric data type
decCreditLimit = Convert.ToDecimal(txtCreditLimit.Text)
'check for a 0 or positive
If decCreditLimit < 0 Then
MessageBox.Show("Please enter a positive value or zero for Credit Limit!")
End If
'check for customers over limit
decCreditMessage = decCreditLimit - (mdecEndingBalance)
'running totals
mdecAllCharges += decTotalCharges
mdecAllCredits += decTotalCredits
'calculate Ending Balance
mdecEndingBalance = Convert.ToDecimal(intBeginningBalance + decTotalCharges - (decTotalCredits))
'display outputs
lblEndingBalance.Text = mdecEndingBalance.ToString("c")
lblAllCharges.Text = mdecAllCharges.ToString("c")
lblAllCredits.Text = mdecAllCredits.ToString("c")
lblCustomersOverLimit.Text = mintCustomersOverLimit.ToString("n0")
End Class
Any Help would be greatly appreciated!
Thanks
You've missed Exit Sub out of the test
If IsNumeric(txtTotalCharges.Text) = False Then 'value is not numeric
MessageBox.Show("Please enter a numeric value for Total Charges!")
End If
You've done it in a few other places as well (as have we all , in fact as do we all :( ). Be worth refactoring with a little helper method or something similar.
If IsValidDecimal(txtTotalCharges.Text, "Total Charges")
' etc
End If