Results 1 to 13 of 13

Thread: None of my If statements are executing.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    None of my If statements are executing.

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Process1.Click
    
            Dim Number As String
            Dim Display As String
    
            
    
            Number = Val(txtNumber.Text)
    
            Display = lblDisplay.Text
    
    
    
            If IsNumeric(Number) = True Then
                If Number < 1 Or Number > 100 Then
                    If Number > 100 Then
                        Display = " The Number You entered is too high"
                    ElseIf Number < 1 Then
                        Display = " 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
    End Class
    My statements are not executing and I am not getting any of my Display messages to come out on my Form when I Run the program.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: None of my If statements are executing.

    It's because
    Code:
    Number = Val(txtNumber.Text)
    would never be numeric, it will allways be a string because you've declared it as such here
    Code:
    Dim Number As String
    also, why not use a numericalupdown for this process?

    Edit - If you insist on using a textbox, then add this to your code:
    Code:
    Private Sub txtNumber_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress
            If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
                   Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
                e.Handled = True
            End If
            If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
                e.Handled = False
            End If
        End Sub
    That away, you'll never have any characters other than numbers(and the backspace) in that textbox. Or take a look at JMc's numerical only textbox here.
    Last edited by dday9; Jun 22nd, 2012 at 05:26 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: None of my If statements are executing.

    Quote Originally Posted by dday9 View Post
    It's because
    Code:
    Number = Val(txtNumber.Text)
    would never be numeric, it will allways be a string because you've declared it as such here
    Code:
    Dim Number As String
    also, why not use a numericalupdown for this process?
    Actually that is not the case. IsNumeric() can be used to see if a string contains a numeric value which will always be true when it has been assigned using a Val() statement.

    Code:
     Dim x As String = "34"
            MsgBox(IsNumeric(x))
    Would be true

    of course all the if tests are treating Number as though it is a numeric variable when it is in fact a string and even though this would usually work it is wrong to do it that way.

    As for why it is not working best to set a break point at the first if statement then single step to see what is happening.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: None of my If statements are executing.

    Also, turn Option Strict ON for the project in the Project|Properties menu (on the Compile tab). That will probably result in dozens of errors, as you are showing lots of confusion about data types. Option Strict will prevent you from making incorrect implicit conversions, which will result in better habits and faster code.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Posts
    16

    Re: None of my If statements are executing.

    Should I also change the string to an integer? I'm actually using a text box because the assignment requires me use if then statements with a text box and label. Will adding that code make the program display correctly?

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: None of my If statements are executing.

    This would be better but still would not work as expected
    Code:
            Dim Number As Integer
            Dim Display As String        
            Display = lblDisplay.Text
    
            If IsNumeric(txtNumber.Text) = True Then
                Number = Val(txtNumber.Text)
                If Number < 1 Or Number > 100 Then
                    If Number > 100 Then
                        Display = " The Number You entered is too high"
                    ElseIf Number < 1 Then
                        Display = " 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
    Last edited by DataMiser; Jun 22nd, 2012 at 05:38 PM.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: None of my If statements are executing.

    The real problem with the code is actually here
    Code:
    If Number < 1 Or Number > 100 Then
                    If Number > 100 Then
                        Display = " The Number You entered is too high"
                    ElseIf Number < 1 Then
                        Display = " 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
    The message saying that the number is not between 1 and 100 will be displayed when the number is between one and 100

    For example 50 is not <1 nor is it > 100 so the code jumps straight to the red else section and gives a false message


    Edit : The other problem is that Display is not being used after it is assigned so it probably does not show any message
    Would need to add
    Code:
    lblDisplay.Text=Display
    at the bottom if it is not already there.

    Edit 2: The code could be rewritten and simplified to something like this
    Code:
    Dim Number As Integer
    
            If IsNumeric(txtNumber.Text) = True Then
                Number = Val(txtNumber.Text)
                If Number > 100 Then
                    lblDisplay.Text = " The Number You entered is too high"
                ElseIf Number < 1 Then
                    lblDisplay.Text = " The Number You entered is too low"
                Else
                    lblDisplay.Text = ""
                    Process1.Enabled = False
                End If
            Else
                lblDisplay.Text = " You did not enter a numerical value "
            End If
    and it should actually work but is still using some old methods leftover from pre .net vb versions.
    Last edited by DataMiser; Jun 22nd, 2012 at 07:22 PM.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: None of my If statements are executing.

    Quote Originally Posted by Shaggy Hiker View Post
    Also, turn Option Strict ON for the project in the Project|Properties menu (on the Compile tab). That will probably result in dozens of errors, as you are showing lots of confusion about data types. Option Strict will prevent you from making incorrect implicit conversions, which will result in better habits and faster code.
    Take this man's advice....It would keep you from making such errors in the future.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Question Re: None of my If statements are executing.

    Hello everyone, I'm working on a class assignment that has a user input numerical values for a geometry calculator and then it displays the answers in console mode. I wanted to expand on the program by using if then statements. I found the examples above very helpful, but I am still not able to construct what I need to work. And Yes, this is my first time in the forum and working with VB so take it easy on the green horn. Here is what I have put together, any assistance would be greatly appreciated. I was unable to find a way to cut and paste the code the same way that was used else where..Sorry

    Thanks

    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[HIGHLIGHT]Sub Main()

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: None of my If statements are executing.

    Code:
    If Number < 1 Or Number > 100 Then
        'Code here will only execute when number is not between 1 and 100
        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 ' this will never be true due to the top if statement
            Process1.Enabled = False 'This will never execute
        End If
    Else
        'This will execute only when the number is between 1 and 100
        Display = " The number you entered is not between 1 and 100" 
    End If

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: None of my If statements are executing.

    We aren't terribly harsh on most people. However, you really should have started your own thread for this. Don't be bashful about it, as it's all about eyeballs around here. The more people that see your post, the more chances that you will get useful information. For that reason, a new thread if almost always the right option, even if you started the original thread. As long as the question is a new one, a new thread is the right way to go to maximize the likelihood of suitable answers. Somebody might jump on me for that, though, as I don't know that I said it quite right, but I'd err on the side of too many threads rather than too few, until somebody objects.

    As for the question, there are lots of things to say:

    1) This line should result in an error at compile time, since it isn't valid code:

    If input(Number) = True Then

    You might have wanted IsNumeric, but leave that aside for the moment.

    2) In the next line you use Val(). I kind of like that function, but it is deprecated VB6 code. You don't have to check whether the input was numeric if you use Val, since Val will ALWAYS return a number. However, Val is also risky, because the way it turns a string into a number can be really surprising to people. It stops parsing as soon as it hits a character that isn't a number, so:

    Val("123") = 123
    Val("12.3") = 12.3
    Val("ABC") = 0
    Val("1,234") = 1
    Val("5A") = 5
    Val("A5") = 0

    The first three are probably expected, as is the last one, but the fourth and fifth might surprise you. The proper way to convert a string into a number would be to use Integer.TryParse (since you want an integer). That would look like this:

    Code:
    If Integer.TryParse(input,Number) Then
     'It was a number, and is now in Number.
    Else
     'It was not a number.
    End If
    3) See what Datamiser wrote, as you have the same mistake about the If Number < 0 Or Number > 100 Then
    (though, in both cases, you should be using OrElse rather than Or).
    My usual boring signature: Nothing

  12. #12
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: None of my If statements are executing.

    Duplicate post - http://www.vbforums.com/showthread.p...f-Then-command ...

    Also, how about we skip IsNumeric for Integer.Tryparse and skip the use of Val as well while we're at it? nvm - I see shaggy covered that.

    -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??? *

  13. #13
    New Member
    Join Date
    Oct 2012
    Posts
    5

    Re: None of my If statements are executing.

    Shaggy, you're right about starting a new thread for this question I actually did do that after I posted on here..noobie mistake. techgnome already posted my new thread below. I'm still working on the project and trying to take everything in. I'll give it another shot before asking more questions. Thanks

    DB

Posting Permissions

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



Click Here to Expand Forum to Full Width