Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,585 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,956 people online right now. Registration is fast and FREE... Join Now!




How do I Make this ONE else if statement

 
Reply to this topicStart new topic

How do I Make this ONE else if statement, Converting numbers to words

<jabakaba>
7 Oct, 2008 - 06:41 PM
Post #1

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 25

How would i take this and convert it to one else if statment that would compile and and not break


CODE

If IsNumeric(txtEnterNumber.Text) Then
Number = CInt(txtEnterNumber.Text)

If Number >= 1 And Number <= 3999 Then
Number = CInt(txtEnterNumber.Text)
Else
MessageBox.Show("Please only enter a number within 1 and 3,999.")
txtEnterNumber.Focus()
txtEnterNumber.Clear()

Exit Sub

End If

Else
MsgBox("Please enter a number in the textbox field, not letters or words.")
txtEnterNumber.Focus()
txtEnterNumber.Clear()
Exit Sub
End If

If txtEnterNumber.Text.Contains(".") Then
MessageBox.Show("Please only enter whole numbers, not decimal numbers.")
txtEnterNumber.Focus()
txtEnterNumber.Clear()
Exit Sub
End If


Dim Thousands As Integer = CInt(Math.Floor(Number / 1000) * 1000)
Dim Hundreds As Integer = CInt(Math.Floor((Number - Thousands) / 100) * 100)
Dim Tens As Integer = CInt(Math.Floor((Number - Hundreds - Thousands) / 10) * 10)
Dim Ones As Integer = Number Mod 10

Dim EnglishResult As String = ""

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: How Do I Make This ONE Else If Statement
7 Oct, 2008 - 08:18 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You don't make it one else if statement. Why? Because you are actually testing multiple DIFFERENT tests. You should only use an if-elseif setup if you are testing one type of tests and want to do an "one or the other" situation. You have three tests going on here... one that tests if it is a number, one that tests if a number is in a certain range and one that tests if it is a whole number. Another way you can detect that it has these three tests is that you have three different error messages.

What you want to do is the following....

CODE

if it is a number
     if that number does not contain a period and is between 1 and 3999
          do your math here
     else
          report that they need to provide a whole number between 1 and 3999
else
     report that they need to provide a number


Notice that this solution is also a combination of if statements and not if else if statements. You could have combined them into one if the tests were similar like I did above with the number does not contain a period and is in between a range.

But design wise they should not be merged into one if... probably why you are having trouble.

Besides, you don't gain speed or anything by merging them. So go with what works logically rather than syntactically. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 12:50AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month