Results 1 to 10 of 10

Thread: Boolean function

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    11

    Boolean function

    This doesn't seem to be working the way that I planned. This is a simple decimal to binary and hex converter.

    I am wanting the function to return a Boolean value so if the user enters text into the text box, it asks for a number to be entered and doesn't try to convert to binary or hex. It is because the function isn't returning True or False or something in the IfElse statement?
    Thanks for any help.

    Name:  BooleanFunction.jpg
Views: 436
Size:  35.2 KB

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Boolean function

    Please don't post pictures of code. It makes it harder to read and it means that we can't copy and paste if we want to run it ourselves or edit it to show you how to make it better. Post the code as text, formatted as code.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Boolean function

    That TypeCheckInt will work but it is bad code. You can get rid of it altogether and just call Integer.TryParse. It returns a Boolean that indicates whether the input is a valid representation of an Integer and it will also give you the Integer value if it returns True. Two birds with one stone and you don't get the poor performance of throwing an exception if the input is invalid.

    If you don't need the value:
    vb.net Code:
    1. If Integer.TryParse(myString, Nothing) Then
    2.     'Success
    3. Else
    4.     'Failure
    5. End If
    If you do need the value:
    vb.net Code:
    1. Dim myInteger As Integer
    2.  
    3. If Integer.TryParse(myString, myInteger) Then
    4.     'Use myInteger here
    5. Else
    6.     'Failure. myInteger contains zero.
    7. End If

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Boolean function

    OT, don't EVER use End to exit an application. If you want to exit a WinForms app then call Application.Exit.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    11

    Re: Boolean function

    Thanks I have modified the code (including Application.Exit) but text is still generating an error on DecNumber = CInt(TxtNumber.Text). I tried adding an additional IF statement. Why is the Else statement still running? Doesn't Integer.TryParse return false if text is entered?
    Code:
    Public Class Form1
    
        Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            Application.Exit()
        End Sub
    
        Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
            LblHexNum.Text = " "
            LblBinaryNum.Text = " "
            TxtNumber.Clear()
        End Sub
    
        Private Sub BtnConvert_Click(sender As Object, e As EventArgs) Handles BtnConvert.Click
            Dim DecNumber As Integer
    
            If TxtNumber.Text = "" Then
                MessageBox.Show("You must enter a number", "Number Converter", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TxtNumber.Focus()
    
            Else Integer.TryParse(TxtNumber.Text, DecNumber)
                If False Then
                    MessageBox.Show("Please enter a number", "Number Converter", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    TxtNumber.Text = " "
                Else
                    DecNumber = CInt(TxtNumber.Text)
                    LblBinaryNum.Text = Convert.ToString(DecNumber, 2)
                    LblHexNum.Text = UCase(Convert.ToString(DecNumber, 16))
                End If
            End If
        End Sub
    
    End Class
    Last edited by Shaggy Hiker; Mar 25th, 2021 at 04:37 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    11

    Re: Boolean function

    Thank you. Got it working by adding False

    ElseIf Integer.TryParse(TxtNumber.Text, DecNumber) = False Then

    Now working perfectly

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: Boolean function

    Quote Originally Posted by ajred View Post
    Else Integer.TryParse(TxtNumber.Text, DecNumber)
    If False Then
    That's not going to work.

    It should be like:

    Code:
    ElseIf Integer.TryParse(TxtNumber.Text, DecNumber) = False Then
    And because of that faulty code, once fixed you will likely have an extra End If that needs to be removed since your above "If False" statement was creating an extra If block that is not existing with the suggested fix.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    11

    Re: Boolean function

    Yes, thanks. Works perfectly now

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Boolean function

    Im sorry... but even with that fix... no... no. no no.....

    First, you only need one if statement, with the TryPArse.... if that succeeds, then you have your number. If it doesn't then you show the error message...
    And since I dislike negatives...

    Code:
    Private Sub BtnConvert_Click(sender As Object, e As EventArgs) Handles BtnConvert.Click
    Dim DecNumber As Integer
    
    If TxtNumber.Text = "" Then
    MessageBox.Show("You must enter a number", "Number Converter", MessageBoxButtons.OK, MessageBoxIcon.Information)
    TxtNumber.Focus()
    
    Else Integer.TryParse(TxtNumber.Text, DecNumber)
    If False Then
    MessageBox.Show("Please enter a number", "Number Converter", MessageBoxButtons.OK, MessageBoxIcon.Information)
    TxtNumber.Text = " "
    Else
    DecNumber = CInt(TxtNumber.Text)
    LblBinaryNum.Text = Convert.ToString(DecNumber, 2)
    LblHexNum.Text = UCase(Convert.ToString(DecNumber, 16))
    End If
    End If
    End Sub
    All thqt could be simplified down to this:
    Code:
    Private Sub BtnConvert_Click(sender As Object, e As EventArgs) Handles BtnConvert.Click
    Dim DecNumber As Integer
    
    If Integer.TryParse(TxtNumber.Text, DecNumber) Then
        LblBinaryNum.Text = Convert.ToString(DecNumber, 2)
        LblHexNum.Text = UCase(Convert.ToString(DecNumber, 16))
    Else
        MessageBox.Show("You must enter a number", "Number Converter", MessageBoxButtons.OK, MessageBoxIcon.Information)
        TxtNumber.Text = ""
        TxtNumber.Focus()
    End If
    End Sub
    -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??? *

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

    Re: Boolean function

    If people are confused by the sequence of these posts, it's because a post got stuck in the mod queue, so some of you were not replying to the latest post, which you didn't see.

    Ultimately, the way TG wrote it is a good one. I'm not sure what code is the current code, but what TG wrote is what SHOULD be the current code.
    My usual boring signature: Nothing

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