Solved

Minicalc program

Posted on 2004-10-01
53
285 Views
Last Modified: 2008-02-01
I am a college student and this is an assignment I have read the other posts and have tried using some of the code but am having trouble getting it.  Below is the requirements of the assignment and the code I have so far any help getting the try catch statements to work would be helpful and appreciated! Basically I’m lost here not looking for the specific answer but some guidance.

1. Test all operators with the values 8 and 2 to make sure it works as expected.
2. Test the program with 99999 + 99999. This should raise a 'System.OverflowException’ exception.
3. Modify the code to catch and handle the exception so that the user can continue. KEEP the declarations of FirstNum and SecondNum as short.
4. Test the program with ‘9 divide 0’. The result shows ‘infinity’. Actually, the calculation should raise an exception ‘System.DivideByZeroException’. Modify the program so that the user input is validated before the divide operation is performed. For a divide by zero, modify the program to raise an exception, then add the code to handle the exception.
5. Try the test (99 * 999). You should get a 'System.OverflowException’ exception. Fix the problem to handle the exception.
6. Now test the '-' operator using the following test cases: (1) variable1 = 32000 and variable2 = -767 and (2) variable1 = 32000 and variable2 = -768. For the first test case, the result should be 32767. For the second test case, you should get an exception. Explain what is different from the first case and fix the problem to handle the exception.
7. Make sure to create a user-friendly interface to guide the user after the exceptions are handled.

    '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
            'this is my first try catch not much happening here except an error from the compiler
            Try
                TextBox3.Text = FirstNum + SecondNum
            Catch Exc As System.OverflowException
                MessageBox.Show("Please choose smaller number")
            End Try
        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
            'here is another example that I have tried but no clue why it isn't functioning
            Try
                TextBox3.Text = FirstNum / SecondNum
            Catch ex As DivideByZeroException
                MessageBox.Show("Division by Zero caused an error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Catch ex As Exception
                MessageBox.Show(ex.Message, "There has been an error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If


    End Sub
0
Comment
Question by:tbury1972
  • 33
  • 17
  • 2
  • +1
53 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
Try this..

        If RadioButton1.Checked = True Then
            'this is my first try catch not much happening here except an error from the compiler
            Try
                Dim Sum As Short
                Sum = FirstNum + SecondNum
            Catch Exc As System.OverflowException
                MessageBox.Show("Please choose smaller number")
                Exit Sub
            End Try
            TextBox3.Text  = Sum
        End If
-Baan
0
 

Author Comment

by:tbury1972
Comment Utility
Diesn't like being declared inside the try I moved it by the other variables and ran it it still comes up with a compiler error not the message box with the ("Please choose smaller number")

        If RadioButton1.Checked = True Then
            'this is my first try catch not much happening here except an error from the compiler
            Try
                Dim Sum As Short
                Sum = FirstNum + SecondNum
            Catch Exc As System.OverflowException
                MessageBox.Show("Please choose smaller number")
                Exit Sub
            End Try
            TextBox3.Text  = Sum
in this format the Sum comes up with not being declared since it is outside the catch statement where it is declared
        End If
0
 

Author Comment

by:tbury1972
Comment Utility
thanks for your help so far I will be back shortly to check the post! I have been pulling my hair out I need a short break
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
Hi there,
ok got it. Actually the Overflow is happening when you are trying to read the values from the TextBoxes. Do one thing, read the values inside try catch.

        Dim FirstNum, SecondNum As Short
        Try
            FirstNum = TextBox1.Text
            SecondNum = TextBox2.Text
        Catch Exc As System.OverflowException
            MessageBox.Show("Please choose smaller number")
            Exit Sub
        End Try
0
 
LVL 4

Expert Comment

by:eozz_2000
Comment Utility
Jejejeje... I'm surprised... you want the complet receipt... better... study man, is the best way to learn.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
I'm not going to give you any code becuase I kind of agree with eozz_2000 but I know what its like to study programming for the first time. The divide by zero question is a good one and it shows that your tutor has put some thought and effort into asking you the questions. Which is much better than someone who just doesn't care.
      VB.Net doesn't throw an exception when a divide by zero opertation is encoutered you have to test the second variable and then you throw the exception. As a reference use to following link.

http://visualbasic.about.com/library/bldyknaninfa.htm

      You should try that your tutor has asked reasonable and infomative questions and trust in your own skill to answer them and leave your hair alone.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
0
 

Author Comment

by:tbury1972
Comment Utility
OK here first I didn't ask for all the code I specifically asked for help if I had a tutor I would be asking them this assignment is for an online class I am taking, my original post should have been read before all of these comments were thrown in.  Now with this erroneous comments I have no idea where to start if I knew some of you were going to respond in this way I would have thought a sec before "paying" for this service.  If there is anyone who would like to help me with this project I will be checking the posts regular while I am attempting to make sense of this assignment!

Thanks,

Erin
0
 

Author Comment

by:tbury1972
Comment Utility
Is there anyone on here today?
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Yes I am, just give me a moment. I will help you.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
I will of course help you with the project and the other users will as well. The second link I gave you was to see the datatypes. Thats what the thrust behind the project is, getting to know the VB.Net datatypes and the issues that you can run into while using them.
     I'll try and clear some things up for you, you maybe already know some of this but I'll cover it anway. Basically the datatypes are there to represent information in the outside world, either numbers or text. The reasons for the differences in them (why you have char, string, Integer, Long, short, single, double, decimal) is because .Net wants you to represent data in the most effective way. Lets say you wrote a program for the social services and had to do a huge amount of calculations based on peoples ages. Representing their ages with a Long (Long can hold numbers in the range -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807) data type would be a waste and would make your program unecassarily slow. If you represent peoples ages with a byte (byte can hold whole numbers from 0 to 255, nobody lives can live until there 255 years old so its safe to use.) data type then your representing the data in the most effective way.
Now things aren't just straight forward in working with the data thats why your project has so many exceptions in it.
An overflow is caused when the number your working with gets to big for your datatype to handle. If in the future the peoples life span was greater than 255 then your program would start throwing exceptions as it ran out of room to hold the data.

I am still on and will help you with code it that is what you wish. Post me back a comment after you read this. I hope it makes things clearer.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Have you covered classes yet in your on line learning program.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
By the way you where doing pretty ok on your own.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Are you there?
0
 

Author Comment

by:tbury1972
Comment Utility
sorry im back I will post some thoughts in a sec
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
No worries, I have built your project on my own machine, I'm working on it now.

In the mean time if you look at the link I gave you for the data types and look at the short defintion closly you should see the answer to your question 6.
0
 

Author Comment

by:tbury1972
Comment Utility
ok I have had a basic class in VB years ago so I have a basic understanding on how it works and can build simple programs but the try catch, and error handling confuses me placement, how to call, ect...  I know I could call a method to handle the error handling and use a case statement to display specific messages in a msg box I have seen some examples formatted that way but it is kinda over my head and am looking for a simpler way to get the the same output I do not need to be that technical
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Ok, I'm working on it this way with one try catch block that way you catch assigments and all. I haven't finished yet but his should give you a rough idea. Hmmm create another button on your form and post his code there. The answer to question 6 is that short can only hold numbers between -32,768 through 32,767 when you subtract -768  from 32000 you end up with a postive (-- = +) (32000 - -768) = 32768 which is too big to for the datatype short to hold.

'Assign text box values to variables
        Try
            FirstNum = TextBox1.Text
            SecondNum = TextBox2.Text
            'Determine checked button and calculate

            If Me.RadioButton1.Checked = True Then
                'this is my first try catch not much happening here except an error from the compiler
                TextBox3.Text = FirstNum + SecondNum
            End If

            If Me.RadioButton2.Checked = True Then
                TextBox3.Text = FirstNum - SecondNum
            End If
            If Me.RadioButton3.Checked = True Then
                TextBox3.Text = FirstNum * SecondNum
            End If

            If Me.RadioButton4.Checked = True Then
                'here is another example that I have tried but no clue why it isn't functioning
                TextBox3.Text = FirstNum / SecondNum
            End If

        Catch ex As System.OverflowException
            MessageBox.Show("Please choose smaller number")
        Catch ex As System.DivideByZeroException
            MessageBox.Show("Division by Zero caused an error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "There has been an error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
0
 

Author Comment

by:tbury1972
Comment Utility
I understand the question to 6 because a variable declared as short can only be between the ranges of -32,768 through 32,767 but in number 3 it specifies it needs to be kept as short so thats where the error handling comes in right?
0
 

Author Comment

by:tbury1972
Comment Utility
Thanks I have the code I tried something similiar to this earlier but didn't have any luck (obviously) but I am here its just for some reason this has been a aheadache I can usually read sample code and figure it out pretty well but there isn't any great examples in my book to go by and this week has been very busy your help is appreciated!
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Yes thats exactly right about question 6. The exercise is just making you aware of the problems that can occur with datatypes. The try catch block can be confusing its different from VB6, its structured exception handling.
0
 

Author Comment

by:tbury1972
Comment Utility
one and two work with the way you put all the catch statements together but the divide by 0 exception doesn't seem to work
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Opps sorry forgot to send you the updated code. Cut and paste this in.

'Assign text box values to variables
        Try
            FirstNum.Parse(TextBox1.Text)
            SecondNum.Parse(TextBox2.Text)
            'Determine checked button and calculate

            If Me.RadioButton1.Checked = True Then
                'this is my first try catch not much happening here except an error from the compiler
                TextBox3.Text = FirstNum + SecondNum
            End If

            If Me.RadioButton2.Checked = True Then
                TextBox3.Text = FirstNum - SecondNum
            End If
            If Me.RadioButton3.Checked = True Then
                TextBox3.Text = FirstNum * SecondNum
            End If

            If Me.RadioButton4.Checked = True Then
                If SecondNum = 0 Then Throw New System.DivideByZeroException()
                TextBox3.Text = FirstNum / SecondNum
            End If

        Catch ex As System.OverflowException
            MessageBox.Show("Please choose smaller number")
        Catch ex As System.DivideByZeroException
            MessageBox.Show("Division by Zero caused an error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "There has been an error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
If SecondNum = 0 Then Throw New System.DivideByZeroException() is the line I added. You create a new DivideByZeroException and throw it.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
I forgot to mention as well if you look at the assingment statemts at the top I put some explicit conversion statements in there. Instead of FirstNum = textbox1.text, I put FirtNum.parse(textbox1.text). This just casts the string into a short datatype. If Option Strict was on your implicit cast would cause an error. VB.Net likes for you to perfom explicit casts and doesn't like implicit ones. Its a change from VB6 to VB.Net
0
 

Author Comment

by:tbury1972
Comment Utility
got it I will give it a try and get back soon thanks
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Try this as well. I added some statements into the overflow exception.

'Assign text box values to variables
        Try
            FirstNum.Parse(TextBox1.Text)
            SecondNum.Parse(TextBox2.Text)
            'Determine checked button and calculate

            If Me.RadioButton1.Checked = True Then
                'this is my first try catch not much happening here except an error from the compiler
                TextBox3.Text = FirstNum + SecondNum
            End If

            If Me.RadioButton2.Checked = True Then
                TextBox3.Text = FirstNum - SecondNum
            End If

            If Me.RadioButton3.Checked = True Then
                TextBox3.Text = FirstNum * SecondNum
            End If

            If Me.RadioButton4.Checked = True Then
                If SecondNum = 0 Then Throw New System.DivideByZeroException()
                TextBox3.Text = FirstNum / SecondNum
            End If

        Catch ex As System.OverflowException
            If Me.RadioButton1.Checked = True Then
                MessageBox.Show("The addition operation caused a result less than -32,768 or greater than 32,767")
            End If
            If Me.RadioButton2.Checked = True Then
                MessageBox.Show("The subtraction operation caused a result less than -32,768 or greater than 32,767")
            End If
            If Me.RadioButton3.Checked = True Then
                MessageBox.Show("The multiplication operation caused a result less than -32,768 or greater than 32,767")
            End If
        Catch ex As System.DivideByZeroException
            MessageBox.Show("Division by Zero caused an error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "There has been an error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Better put one in for division just in case. Add this line in.

If Me.RadioButton4.Checked = True Then
        MessageBox.Show("The Division operation caused a result less than -32,768 or greater than 32,767")
End If
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
You might think of a better statements in the message boxes yourself.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
I'll send you some info explaining exception handling in a while. This seems to be where you had the problem.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
I'll still be around if you want to ask anymore questions.
0
 

Author Comment

by:tbury1972
Comment Utility
If Me.RadioButton4.Checked = True Then
        MessageBox.Show("The Division operation caused a result less than -32,768 or greater than 32,767")
End If
 should this line go just behind the other if statements and on # 5 the 99 * 999 just comes up with 0 for an answer and should be throwing an exception any ideas
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
I placed the new code in the right place. Back in a sec with the other question

Try
            FirstNum.Parse(TextBox1.Text)
            SecondNum.Parse(TextBox2.Text)
            'Determine checked button and calculate

            If Me.RadioButton1.Checked = True Then
                'this is my first try catch not much happening here except an error from the compiler
                TextBox3.Text = FirstNum + SecondNum
            End If

            If Me.RadioButton2.Checked = True Then
                TextBox3.Text = FirstNum - SecondNum
            End If

            If Me.RadioButton3.Checked = True Then
                TextBox3.Text = FirstNum * SecondNum
            End If

            If Me.RadioButton4.Checked = True Then
                If SecondNum = 0 Then Throw New System.DivideByZeroException()
                TextBox3.Text = FirstNum / SecondNum
            End If

        Catch ex As System.OverflowException
            If Me.RadioButton1.Checked = True Then
                MessageBox.Show("The addition operation caused a result less than -32,768 or greater than 32,767")
            End If
            If Me.RadioButton2.Checked = True Then
                MessageBox.Show("The subtraction operation caused a result less than -32,768 or greater than 32,767")
            End If
            If Me.RadioButton3.Checked = True Then
                MessageBox.Show("The multiplication operation caused a result less than -32,768 or greater than 32,767")
            End If
            If Me.RadioButton4.Checked = True Then
                MessageBox.Show("The Division operation caused a result less than -32,768 or greater than 32,767")
            End If
        Catch ex As System.DivideByZeroException
            MessageBox.Show("Division by Zero caused an error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "There has been an error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Yup the parse isn't working the way i thought it should. I rarely use it actuall. Obviously i'm not going to use parse for this operation again. I used CType instead for the two assignment statements.

       'Assign text box values to variables
        Try
            'If me.RadioButton1.
            FirstNum = CType(TextBox1.Text, Short)
            SecondNum = CType(TextBox2.Text, Short)
            'Determine checked button and calculate

            If Me.RadioButton1.Checked = True Then
                'this is my first try catch not much happening here except an error from the compiler
                TextBox3.Text = FirstNum + SecondNum
            End If

            If Me.RadioButton2.Checked = True Then
                TextBox3.Text = FirstNum - SecondNum
            End If

            If Me.RadioButton3.Checked = True Then
                TextBox3.Text = FirstNum * SecondNum
            End If

            If Me.RadioButton4.Checked = True Then
                If SecondNum = 0 Then Throw New System.DivideByZeroException()
                TextBox3.Text = FirstNum / SecondNum
            End If

        Catch ex As System.OverflowException
            If Me.RadioButton1.Checked = True Then
                MessageBox.Show("The addition operation caused a result less than -32,768 or greater than 32,767")
            End If
            If Me.RadioButton2.Checked = True Then
                MessageBox.Show("The subtraction operation caused a result less than -32,768 or greater than 32,767")
            End If
            If Me.RadioButton3.Checked = True Then
                MessageBox.Show("The multiplication operation caused a result less than -32,768 or greater than 32,767")
            End If
        Catch ex As System.DivideByZeroException
            MessageBox.Show("Division by Zero caused an error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "There has been an error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
0
 

Author Comment

by:tbury1972
Comment Utility
Something still isn't right if I divide by zero the exception is ok but if I switch the places in the text box it just comes up with 0 for the answer wich is correct buut it should throw an exception correct I tried changing the code to if firstNum or secnum = 0 then but it does not work.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Try it on your calculator on your computer 50/0 is "Cannot divide by zero", 0/50 is 0 which is correct you don't throw an exception.
0
 

Author Comment

by:tbury1972
Comment Utility
lol thanks I wasn't even thinking in math terms just that a 0 should throw an exception lol I feel really nutz I even stated in my last post that the answer was correct and should have realized it shouldn't have been an error thanks
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Ok this should tighten things up for data entry. Give it a test and let me know.

'Assign text box values to variables
        Try
            'Check to see if the user has entered numbers correctly
            If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MessageBox.Show("You forgot to enter both numbers.", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            'Check to see if any text has been entered.
            If IsNumeric(TextBox1.Text) = False Or IsNumeric(TextBox2.Text) = False Then
                MessageBox.Show("You have enterd none numeric data.", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            'Check to see if the user has picked an operation
            If Me.RadioButton1.Checked = False And Me.RadioButton2.Checked = False And _
            Me.RadioButton3.Checked = False And Me.RadioButton4.Checked = False Then
                MessageBox.Show("You forgot to pick one of the operations", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            FirstNum = CType(TextBox1.Text, Short)
            SecondNum = CType(TextBox2.Text, Short)
            'Determine checked button and calculate

            If Me.RadioButton1.Checked = True Then
                'this is my first try catch not much happening here except an error from the compiler
                shortResult = FirstNum + SecondNum
            End If

            If Me.RadioButton2.Checked = True Then
                shortResult = FirstNum - SecondNum
            End If

            If Me.RadioButton3.Checked = True Then
                shortResult = FirstNum * SecondNum
            End If

            If Me.RadioButton4.Checked = True Then
                If SecondNum = 0 Then Throw New System.DivideByZeroException()
                shortResult = FirstNum / SecondNum
            End If

            TextBox3.Text = shortResult.ToString

        Catch ex As System.OverflowException
            If Me.RadioButton1.Checked = True Then
                MessageBox.Show("The addition operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            If Me.RadioButton2.Checked = True Then
                MessageBox.Show("The subtraction operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            If Me.RadioButton3.Checked = True Then
                MessageBox.Show("The multiplication operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As System.DivideByZeroException
            MessageBox.Show("Division by Zero caused an error!", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
0
 

Author Comment

by:tbury1972
Comment Utility
where is the variable ShortResult declared and what type but the code is awsome i was happy with the last accomplishments but this is over the top lol thanks so much i understand it but would have never have came up with it in that format "NICE"
0
 

Author Comment

by:tbury1972
Comment Utility
should i declare it also as short in the form where I have FirstNm and SecNum declared as short
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Sorry,

    Dim FirstNum As Short
    Dim SecondNum As Short
    Dim shortResult As Short
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Maybe it is over the top. You can still with the old one. Its always here on the exchange.
0
 

Author Comment

by:tbury1972
Comment Utility
everything seems to be working on the new code except for the division function if I div by 0 or div 0 itself the correct answer is displayed either as a answer or en exception but if I div by two real numbers that should give a correct answer it just gives 0 as the answer
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Nope, do 45/9 you'll get 5.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
If you divide 45/60 it rounds the answer to an integer (implicit conversion) closest number is 0.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Sorry make that 45/100. The above is 1.
0
 
LVL 3

Accepted Solution

by:
Arundel_Castle earned 500 total points
Comment Utility
Hows that, everything working, its getting late here in Ireland.
0
 

Author Comment

by:tbury1972
Comment Utility
thanks again for all your help it is appreciated!
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
I think you understood what (datatypes, specificaly short) the assignment was about you just got caught up in the Try ..... Catch block area. Thanks for the points.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Do you see this.

Modify the program so that the user input is validated before the divide operation is performed.
For a divide by zero, modify the program to raise an exception, then add the code to handle the exception.

He might be talking about stoping people from dividing and ending up with a real number.  I'm not sure what the mean.
What do you think you have the link for the assignment.
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
You can replace the code with the one below if you want. If stops a divison that will result in a real number and cleans out textbox3 when there is a problem. You don't have to use it.

'Assign text box values to variables
        Try
            'Check to see if the user has entered numbers correctly
            If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MessageBox.Show("You forgot to enter both numbers.", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TextBox3.Text = ""
                Exit Sub
            End If
            'Check to see if any text has been entered.
            If IsNumeric(TextBox1.Text) = False Or IsNumeric(TextBox2.Text) = False Then
                MessageBox.Show("You have enterd none numeric data.", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TextBox3.Text = ""
                Exit Sub
            End If
            'Check to see if the user has picked an operation
            If Me.RadioButton1.Checked = False And Me.RadioButton2.Checked = False And _
            Me.RadioButton3.Checked = False And Me.RadioButton4.Checked = False Then
                MessageBox.Show("You forgot to pick one of the operations", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TextBox3.Text = ""
                Exit Sub
            End If

            FirstNum = CType(TextBox1.Text, Short)
            SecondNum = CType(TextBox2.Text, Short)

            'FirstNum = TextBox1.Text
            'SecondNum = TextBox2.Text

            'Determine checked button and calculate

            If Me.RadioButton1.Checked = True Then
                'this is my first try catch not much happening here except an error from the compiler
                shortResult = FirstNum + SecondNum
            End If

            If Me.RadioButton2.Checked = True Then
                shortResult = FirstNum - SecondNum
            End If

            If Me.RadioButton3.Checked = True Then
                shortResult = FirstNum * SecondNum
            End If

            If Me.RadioButton4.Checked = True Then
                If SecondNum = 0 Then Throw New System.DivideByZeroException()
                If Not ((FirstNum Mod SecondNum = 0) And (SecondNum < FirstNum)) Then
                    MessageBox.Show("Division resulting in a real number is not allowed", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    TextBox3.Text = ""
                    Exit Sub
                End If

                shortResult = CType(FirstNum / SecondNum, Short)
            End If

            TextBox3.Text = shortResult.ToString

        Catch ex As System.OverflowException
            If Me.RadioButton1.Checked = True Then
                MessageBox.Show("The addition operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox3.Text = ""
            End If
            If Me.RadioButton2.Checked = True Then
                MessageBox.Show("The subtraction operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox3.Text = ""
            End If
            If Me.RadioButton3.Checked = True Then
                MessageBox.Show("The multiplication operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox3.Text = ""
            End If
        Catch ex As System.DivideByZeroException
            MessageBox.Show("Division by Zero caused an error!", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
            TextBox3.Text = ""
        Catch ex As Exception
            MessageBox.Show(ex.Message, "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
            TextBox3.Text = ""
        End Try
    End Sub
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
You should use this one!!!

This one makes the distintion of an overflow at the asignment 99999 + 99999, it doesn't accept real numbers for calculation, only integers. You can cut all the code that blocks the real number stuff out if you want. I have pasted the full page code in this so it replaces button code and everthing. Thats it from me.

----Variable declaration

Dim FirstNum As Short
Dim SecondNum As Short
Dim shortResult As Short
Dim Double_FirstNum As Double
Dim Double_SecondNum As Double

-----Code

Dim PassAssignmentCheck As Boolean

        'Assign text box values to variables
        Try
            'Check to see if any text has been entered.
            If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MessageBox.Show("You forgot to enter both numbers.", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TextBox3.Text = ""
                Exit Sub
            End If

            'Make sure the text is numeric.
            If IsNumeric(TextBox1.Text) = False Or IsNumeric(TextBox2.Text) = False Then
                MessageBox.Show("You have enterd none numeric data.", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TextBox3.Text = ""
                Exit Sub
            End If
            'Check to see if the user has picked an operation
            If Me.RadioButton1.Checked = False And Me.RadioButton2.Checked = False And _
            Me.RadioButton3.Checked = False And Me.RadioButton4.Checked = False Then
                MessageBox.Show("You forgot to pick one of the operations", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TextBox3.Text = ""
                Exit Sub
            End If

            'Assign numbers to double first
            Double_FirstNum = CType(TextBox1.Text, Double)
            Double_SecondNum = CType(TextBox2.Text, Double)

            'Check to see if real numbers have been added.
            If (Double_FirstNum Mod 1) <> 0 Or (Double_SecondNum Mod 1 <> 0) Then
                MessageBox.Show("You cannot enter a real number, only an integer in the range (-32768 to 32767).", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Information)
                TextBox3.Text = ""
                Exit Sub
            End If

            'Distinguish betwen an overflow exception here and after an operation.
            PassAssignmentCheck = False
            FirstNum = CType(TextBox1.Text, Short)
            SecondNum = CType(TextBox2.Text, Short)
            PassAssignmentCheck = True

            'Determine checked button and calculate

            If Me.RadioButton1.Checked = True Then
                'this is my first try catch not much happening here except an error from the compiler
                shortResult = FirstNum + SecondNum
            End If

            If Me.RadioButton2.Checked = True Then
                shortResult = FirstNum - SecondNum
            End If

            If Me.RadioButton3.Checked = True Then
                shortResult = FirstNum * SecondNum
            End If

            If Me.RadioButton4.Checked = True Then
                'Throw exception if divide by zero.
                If SecondNum = 0 Then Throw New System.DivideByZeroException()
                'Check that the result doesn't end up as a real number.
                If Not ((FirstNum Mod SecondNum = 0) And (SecondNum < FirstNum)) Then
                    MessageBox.Show("Division resulting in a real number is not allowed", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    TextBox3.Text = ""
                    Exit Sub
                End If
                shortResult = CType(FirstNum / SecondNum, Short)
            End If

            TextBox3.Text = shortResult.ToString

        Catch ex As System.OverflowException
            If PassAssignmentCheck = False Then
                MessageBox.Show("Your enterd a number that is out of the short data type integer range (-32768 to 32767).", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox3.Text = ""
                Exit Try
            End If
            If Me.RadioButton1.Checked = True Then
                MessageBox.Show("The addition operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox3.Text = ""
            End If
            If Me.RadioButton2.Checked = True Then
                MessageBox.Show("The subtraction operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox3.Text = ""
            End If
            If Me.RadioButton3.Checked = True Then
                MessageBox.Show("The multiplication operation caused a result less than -32,768 or greater than 32,767", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TextBox3.Text = ""
            End If
        Catch ex As System.DivideByZeroException
            MessageBox.Show("Division by Zero caused an error!", "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
            TextBox3.Text = ""
        Catch ex As Exception
            MessageBox.Show(ex.Message, "MiniCalc Application", MessageBoxButtons.OK, MessageBoxIcon.Error)
            TextBox3.Text = ""
        End Try
0
 
LVL 3

Expert Comment

by:Arundel_Castle
Comment Utility
Sorry doesn't replace button code. Its just the sub and the declarations. Its getting late Ugh!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now