Having trouble with If Then command:-VBForums
Results 1 to 5 of 5

Thread: Having trouble with If Then command:

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Having trouble with If Then command:

    Hello to all, I have completed a class assignment and wanted to expand using an If Then module to determine if an input is either a number or a specific letter. I've been searching forums and have found several examples that I have tried to make work but am unable to succeed as of yet. The VB project is in console mode and I will attempt to cut and paste the code so you can take a look at it. Now, here's a discription of what my program is supposed to do. My program asks for numerical values to input to calculate the area of a triangle. I want to add an if then statement in the event that the input is not a number it will re-ask the user to input the number again. Here is what I have so far.

    Sub Main()
    Dim Number As Integer
    Dim Display As String
    Console.WriteLine("Please input a number")
    Dim input = Console.ReadLine()


    If input(Number) = True Then
    Number = Val(Number)
    If Number < 1 Or Number > 100 Then
    If Number > 100 Then
    Console.WriteLine(" The Number You entered is too high")
    ElseIf Number < 1 Then
    Console.WriteLine(" The Number You entered is too low")
    ElseIf Number >= 1 And Number <= 100 Then
    Process1.Enabled = False
    End If
    Else
    Display = " The number you entered is not between 1 and 100"
    End If
    Else
    Display = " You did not enter a numerical value "
    End If


    End Sub

    How can I cut and paste what I have on VB to be the same in this forum. I used WYSIWYG and it still does not look like what I have on VB.

    Thanks

  2. #2
    PowerPoster
    Join Date
    May 2002
    Posts
    25,966

    Re: Having trouble with If Then command:

    put [CODE][/CODE] or [HIGHLIGHT=VB][/HIGHLIGHT] tags around the code...

    for starters though.... these two lines shouldn't work....
    Code:
    If input(Number) = True Then
        Number = Val(Number)
    The first should produce a compile error...

    Input is going to be a string because that's what Console.ReadLine() returns.
    Number is an integer, but hasn't been assigned a value yet so it's not really valid...
    Then you have Number = Val(Number) ... but Number is still empty, and even if it did, it would already be an integer (because that's what Number has been defined as) ... making Number = Val(Number) redundant.

    I'm guessing that's not really what you wanted....

    Try this instead:
    Code:
    If Integer.TryParse(Input, Number) Then
        If Number < 1 Or Number > 100 Then 
            If Number > 100 Then
    This condition will never run:
    Code:
    ElseIf Number >= 1 And Number <= 100 Then
    It is found inside the first check for out of bounds...



    here's a little quick restructuing:
    Code:
    If Integer.TryParse(Input, Number) Then
        If Number < 1 then
            console.writeline("Value too small")
            Display = "Value entered is not between 1 and 100"
       elseif Number > 100 then
            console.writeline("Value too large")
            Display = "Value entered is not between 1 and 100"
        else ' No need for any further checking we know it's between 1 and 100
            Process1.Enabled = False
        End If
    Else
        Display = " You did not enter a numerical value "
    End If
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Re: Having trouble with If Then command:

    I will give it a try, Thank You...so many questions, so little time in the day!

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Re: Having trouble with If Then command:

    Ok I've been working on this now for several hours and am still having some issues. Maybe I just need to simplify this by breaking it down into two steps. Here is what I now have.

    VB Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Console.WriteLine("please enter a number between 1 and 9, or one of the following letters a, b or c")
    5.         'here is where I would like the program to determine if the input is a number or a letter
    6.         'unfortunatly my text book in on program structure and not on VB so no useable code is
    7.         'in the book.  
    8.         Dim input = Console.ReadLine()
    9.         If input <= 1 OrElse >= 9 Then
    10.         Else input <> (a,b,c) then
    11.             Console.WriteLine("Im sorry, but you did not enter one of the designated characters.  Please try again")
    12.  
    13.  
    14.         End If
    15.  
    16.  
    17.     End Sub
    18.  
    19.    
    20. End Module

  5. #5
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    296

    Re: Having trouble with If Then command:

    Does this do what you want?

    Code:
    Option Compare Binary
    Option Explicit On
    Option Infer Off
    Option Strict On
    
    Imports System
    
    Public Module AreaModule
       Public Sub Main ()
          Dim Area As Integer = 0
          Dim Side1 As Integer = 0
          Dim Side2 As Integer = 0
    
          Side1 = GetNumber("Length of side #1: ")
          Side2 = GetNumber("Length of side #2: ")
          Area = CInt((Side1 * Side2) / 2)
    
          Console.Write ("The triangle's area: ")
          Console.Write (CStr(Area))
       End Sub
    
       Private Function GetNumber(Prompt As String) As Integer
          Dim UserInput As String = Nothing
    
          Do
             Console.Write (Prompt)
             UserInput = Console.ReadLine().Trim
          Loop Until Integer.TryParse(UserInput, New Int32)
    
          Return CInt(CInt(UserInput))
       End Function
    End Module
    The program checks whether the user's input is a valid number using the TryParse() function.

    Hope this helps.
    Last edited by Peter Swinkels; Jan 9th, 2013 at 05:00 AM. Reason: Updated the code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.