Results 1 to 4 of 4

Thread: trapping non-integer errors

  1. #1
    bigr3d is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2007
    Posts
    6
    Reputation
    0

    trapping non-integer errors

    hey, im in my final year of school and im completing a coarse in software development.

    We are completing a simple task at the moment of typing an integer into a test box and having it be displayed. This is simple however we have to have a try and catch statement where if a non-integer is inserted it wont freeze up.
    the code looks like this so far:

    Code:
    Public Class Form1
    
       Private Sub Btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnexit.Click
           End
    
       End Sub
    
       Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
           txtEnter.Text = ""
    
       End Sub
    
       Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEnter.TextChanged
    
       End Sub
    
       Private Sub btnInteger_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInteger.Click
           'Dim I As Integer
           Dim str As String
           lbldisplay.Text = Integer.Parse(str)
           Try
               Console.WriteLine("In Try...Block Code")
               lbldisplay.Text = Integer.Parse(str)
           Catch d As NullReferenceException
               Console.WriteLine("Error!!! ... In cash block")
               Console.WriteLine("Nullreferenceexception Caught") ' not complete with errors
           Catch ex As Exception
    
           End Try
    
       End Sub
    
    
    End Class
    can anyone see the issue and if so could someone give me some tips to basic structure of code
    i have no idea myself

    SOMEONE PLEASE HELP


    HELP!!
    thnx for reading

    i also have the files if you need them
    thnx
    Last edited by bigr3d; 05-30-2007 at 6:58 PM. Reason: code box

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,176
    Reputation
    2368
    Change the code in btnInteger_Click to this:
    Code:
            Dim i As Integer
            If Integer.TryParse(TextBox1.Text, i) Then
                lbldisplay.Text = i.ToString
            Else
                lbldisplay.Text = "could not parse String to Integer"
            End If
    The TryParse function combines Parse and TryCatch and returns Boolean value True if successful.

  3. #3
    bigr3d is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2007
    Posts
    6
    Reputation
    0
    exelent thnak you sooo much ill try it at school tomorrow and get back THNX

  4. #4
    bigr3d is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    May 2007
    Posts
    6
    Reputation
    0
    hey it worked really well thnx.

Bookmarks

Posting Permissions

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