Results 1 to 5 of 5

Thread: divide by zero while entering a numeric value that begins with 0

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Location
    San Diego, CA
    Posts
    19

    divide by zero while entering a numeric value that begins with 0

    basically I have a textbox

    Code:
        Private Sub txt_NumberA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_NumberA.TextChanged
            
            Try
                NumberA = Decimal.Parse(txt_NumberA.Text)
            Catch ex As Exception
                MsgBox("This program requires a numeric input.  Please try again. Enter .500 as 0.500.")
                Return
            End Try
            
            NumberB = 5.0
            
            NumberC = NumberB/NumberA
    
    lbl_NumberC.Text = NumberC
    
        End Sub


    I have no problem entering any number that does not start with a zero, however, if I want to enter 0.5 in, then as I type in '0', then I have division by zero and the program crashes.

    Could you please help me to fix this? Many thanks,

    Nina
    Last edited by nina_le_huynh; Feb 24th, 2008 at 04:21 AM.

  2. #2
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: divide by zero while entering a numeric value that begins with 0

    Try this:

    Code:
        Private Sub txt_NumberA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_NumberA.TextChanged
            If Val(txt_NumberA.Text) <> 0 Then
                Try
                    numbera = Decimal.Parse(txt_NumberA.Text)
                Catch ex As Exception
                    MsgBox("This program requires a numeric input. Please try again. Enter .500 as 0.500.")
                    Return
                End Try
    
                numberb = 5.0
    
                numberc = numberb / numbera
    
                lbl_NumberC.Text = numberc
            End If
        End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2008
    Location
    San Diego, CA
    Posts
    19

    Re: divide by zero while entering a numeric value that begins with 0

    03myersd,
    It worked, thank you very much.
    Nina

    Quote Originally Posted by 03myersd
    Try this:

    Code:
        Private Sub txt_NumberA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_NumberA.TextChanged
            If Val(txt_NumberA.Text) <> 0 Then
                Try
                    numbera = Decimal.Parse(txt_NumberA.Text)
                Catch ex As Exception
                    MsgBox("This program requires a numeric input. Please try again. Enter .500 as 0.500.")
                    Return
                End Try
    
                numberb = 5.0
    
                numberc = numberb / numbera
    
                lbl_NumberC.Text = numberc
            End If
        End Sub

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: divide by zero while entering a numeric value that begins with 0

    If the user is going to type in 53 your code displays in lbl_NumberC.Text the following:
    1.0
    .09

    Is that what you wanted or did you just want the .09? If so you are in the wrong event handler, and also why you had the problem with 0.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: divide by zero while entering a numeric value that begins with 0

    I'd say you are in the wrong event handler, too. Re-doing the calculation for every character being entered means lots of extra work. LostFocus might be a better place.

    However, this code will be faster. Catching an exception is a painfully slow process (though there is no cost if no exception is thrown), so using TryParse will produce better average results than dealing with an exception.

    vb Code:
    1. Private Sub txt_NumberA_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_NumberA.TextChanged
    2.        
    3.         if not Decimal.TryParse(txt_NumberA.Text,NumberA)
    4.          MsgBox("This program requires a numeric input.  Please try again. Enter .500 as 0.500.")
    5.             Return
    6.         End if
    7.        
    8.         NumberB = 5.0
    9.        
    10.         if NumberA<>0 then
    11.          NumberC = NumberB/NumberA
    12.         else
    13.          NumberC=0
    14.         end if
    15.  
    16. lbl_NumberC.Text = NumberC.ToString
    17.  
    18.     End Sub
    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