Question odd and even help for VB

Ambre Coleman

New member
Joined
Sep 21, 2011
Messages
4
Location
Taylor, Wisconsin, United States
Programming Experience
Beginner
I have to create a program where the user enters 2 numbers and the program outputs all the even and odd numbers between those 2 numbers. I have that part of the code complete. But now I have to enter code where if the user enters a letter instead a number in either input box, that a message pops up and tells them to enter a number, instead of a letter... I keep going round and round on this and can't seem to get it quiet right. I know I'm close, but I can't seem to figure it out. I'm new at programming so please be kind :)

Here's my code:

'Author: Ambre A. Coleman
'Purpose: To show all odd and even numbers betweeen two entered numbers
'Date: 09/14/2011


Option Strict On
Option Explicit On




Public Class frmOddEven


Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub


Private Sub OddEvens_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


End Sub


Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click
'Declare variables
Dim intLowNumber As Integer
Dim intHighNumber As Integer
Dim btnIsConvertedLow As Boolean
Dim btnIsConvertedHigh As Boolean




btnIsConvertedLow = Integer.TryParse(txtnumber1.Text, intLowNumber)
btnIsConvertedHigh = Integer.TryParse(txtnumber2.Text, intHighNumber)










For i As Integer = (intLowNumber + 1) To (intHighNumber - 1) Step 1
If i Mod 2 = 0 Then
lstEven.Items.Add(CDbl(i.ToString))
Else
lstOdd.Items.Add(CDbl(i.ToString))
End If
Next

Try


If IsNumeric(btnIsConvertedLow) = False Then
MessageBox.Show("Please enter 2 numbers into field")
ElseIf IsNumeric(btnIsConvertedHigh) = False Then
MessageBox.Show("Please enter 2 numbers into field")
End If




Catch ex As Exception
End Try
End Sub
End Class
 
You would use a Do loop. Display the InputBox first, then start the Do the following Until the input is valid: display the error message and then display the InputBox again. You can put the call to Integer.TryParse right into the Until clause of the loop because it returns a Boolean.
 
The code doesn't just come to you. The code is an implementation of a design. So, what's your design? When trying to solve a programming problem, it's generally best to forget that it's a programming problem. If you had to do this manually, what steps would you have to follow? If you don't know that then how can you possibly write code to do it? Pick up a pen and paper (you remember those, right) and write down those steps. You don;t need any programming experience for that. Once you've got the steps, then you can write code to implement those steps.
 
If examples are what you want then there are plenty all over the internet. What you're actually asking for is for me to write your code for you, which I'm not prepared to do. If you're not prepared to follow the instructions I provided in my previous two posts then I'm afraid I can help no further. Pretty much everyone wants to just write code without doing any design first. I was the same when I was learning. I didn't have anyone to write my code for me though, so when I couldn't go straight from idea to code, I had to stop and do a proper design. That's what you need to do too. If you try that and you have issues then I'm happy to help you fix them.
 
What are you using for the input box? How do you start the code running (what does the user do, click a button?)
 
An input box returns a string value. If that value is non-numeric, then the TryParse method will return False. For example:

Dim ans as String, num As Integer
ans = InputBox("Enter a number")
If Integer.TryParse(ans, num) = False Then
MessageBox.Show("Not a number")
End If

Now put that inside a Do loop that will keep repeating until TryParse returns True.
 
Back
Top