borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Memory)Next Topic (Adding Papers to Printers.) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Calculator in VB.NET Solved Topic
Poster Message
12String
Level: Protégé


Registered: 28-10-2005
Posts: 4

icon Calculator in VB.NET

I lost my post so I am trying again.

I have a basic calculator program that has a few minor bugs or 'System.OverflowException' exceptions.

Here is the code I have so far:

'Declare FirstNum and SecondNum variables
    Dim FirstNum, SecondNum As Short

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Assign text box values to variables
        FirstNum = TextBox1.Text
        SecondNum = TextBox2.Text

        'Determine checked button and calculate
        If RadioButton1.Checked = True Then
            TextBox3.Text = FirstNum + SecondNum
        End If
        If RadioButton2.Checked = True Then
            TextBox3.Text = FirstNum - SecondNum
        End If
        If RadioButton3.Checked = True Then
            TextBox3.Text = FirstNum * SecondNum
        End If
        If RadioButton4.Checked = True Then
            TextBox3.Text = FirstNum / SecondNum
        End If
    End Sub

End Class

Here are my exceptions:

   1. When I test to add (+) the following equation, 99999 + 99999 I get a 'System.OverflowException' I need to modify the code to catch and handle this exception so the user can continue.

   2. Under the divide (/) function, division by 0 of course will produce an infinite number. Here I want to modify the program so that the user input is validated before the divide operation is performed, and modify the program to raise an exception, then add the code to handle this exception.

   3.  Under the multiply (*) function, for some reason my program will not take (99 * 999) I need to fix this exception as well.

   4. Finally, the subtract (-) wow now here is a good one. If I subtract 32000 - (-767) the answer is 32767, however add one more to the negative (-768) and Bam! another exception.

I want to use Try Catch in all the areas. Can someone help me please.

12String

       

04-11-2005 at 09:57 AM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon Re: Calculator in VB.NET

Hi,
basic usage of try..catch

try
    ' my code goes here
catch (e as Exception)
    ' do something with the error
end try


Example:


        'Determine checked button and calculate
        If RadioButton1.Checked = True Then
            try
                TextBox3.Text = FirstNum + SecondNum
            catch (e as Exception)
                 textbox3.text = e.Message
             end try
        End If


Hope this helps,
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

04-11-2005 at 10:24 AM
View Profile Send Email to User Show All Posts | Quote Reply
12String
Level: Protégé


Registered: 28-10-2005
Posts: 4
icon Re: Calculator in VB.NET

Kieron,

I tried inserting the code sample you gave, I am still getting an error message right at the beginning on the line:

FirstNum = TextBox1.Text

What I need to know is how do I overcome this Exception error and force the program to calculate more than 4 character numbers. Once I enter the values 99999 + 99999
Calculate that is when the Exception error appears on the above mentioned line.

Help!    

06-11-2005 at 11:36 AM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon Re: Calculator in VB.NET

Hi,
Can you post up your code pls?
K

____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

07-11-2005 at 03:32 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: Calculator in VB.NET

Looks to me like you're overflowing your variables. You have declared them as "Short" which is a 2byte number from -32767 to + 32767. Try declaring it as a long.

Steve  

07-11-2005 at 04:47 PM
View Profile Send Email to User Show All Posts | Quote Reply
12String
Level: Protégé


Registered: 28-10-2005
Posts: 4
icon Re: Calculator in VB.NET

Had to keep variables for FirstNum and SecondNum As Short.

Used Try Catch to handle SystemOverflows followed by appropriate messages. Program works like a charm and I received an A thank you to all who gave me suggestions. I used them with the MSDN library and Wa la finished product.

Thanks again,
12String

10-11-2005 at 04:54 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Calculator in VB.NET Solved Topic
Previous Topic (Memory)Next Topic (Adding Papers to Printers.) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder