Home > Blockchain >  Need some assistance with a Area Calculator in Visual Basics
Need some assistance with a Area Calculator in Visual Basics

Time:12-11

I need some help with my code in a Rectangle calculator... So I made a rectangle area calculator that features Length, Width, Area, Number of Rectangles, and Smallest Rectangle... I am also creating a catch bock that handles invalid-cast extractions 2 of them... Also a A expectation class when the value of the result is greater then 1 Million... Here is my code

Public Class Form1
Dim area As Decimal
Dim numberofRectangles As Integer
Dim smallestRectangle As Decimal 

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Try
        Dim length As Decimal = CDec(txtLength.Text)
        Dim width As Decimal = CDec(txtWidth.Text)

        Dim Area As Decimal = width * length
        Dim numberofRectangles As Integer = numberofRectangles   1
        Dim smallestRectangle As Decimal = Math.Min(smallestRectangle, Area)

        txtArea.Text = Area.ToString("n2")
        txtNumberOfRectangles.Text = numberofRectangles.ToString
        txtSmallestRectangle.Text = smallestRectangle.ToString("n2")

        txtLength.Select()

    Catch ex As InvalidCastException
        MessageBox.Show("Please check entries for valid numeric data",
                    ex.GetType.ToString)
    Catch ex As OverflowException
        MessageBox.Show("Please check to make sure entries aren't too large.",
            ex.GetType.ToString)
    Catch ex As Exception
        MessageBox.Show(ex.Message & vbNewLine & vbNewLine & ex.StackTrace,
               ex.GetType.ToString)
        If area < 1000000000 Then
            Throw New FormatException("The rectangle Is too large!")
            Return
        End If

    Finally

    End Try
End Sub

For one I am having a error where my smallest rectangle is at 999,999,999,00 and For the expectation when the value of the result is greater then 1 Million I am having trouble as you can probably see from the code. Looking for some advice on my code

Edit: Fix the top part Now getting 0.00 in the smallest rectangle box

Should I being using me.compute somehwhere aswell

CodePudding user response:

You misspelled smallestRecntangle - this is why you have needed to redeclare it - it's different to the class variable smallestRectangle. For this reason, you never set smallestRectangle to anything other than 999999999. Also - you can use a value called Decimal.MaxValue to do the same thing as 999999999, but using the actual maximum possible decimal number. However, I would recommend instead you use Decimal? and assign to null initially. In code, have a check which "is if smallestRectangle is null, then set smallestRectangle, otherwise set it to the minimum of smallestRectangle and area".

CodePudding user response:

Exception handling is for unexpected errors that are out of your control. We can test the user input so we don't need exception handling for that.

ex.GetType.ToString

This doesn't make sense. If you want to display the Exception message you can use and ampersand & and ex.Message. ex.ToString generally provides more information than you want a user to see.

TryParse will check if the string provided in the first parameter can be converted. It returns True or False. If True it will assign the converted string to the second parameter.

numberofRectangles  = 1

is a shortcut way of writing

numberofRectangle = numberofRectangles   1

The only line of code that could throw an exception is the area calculation so we will limit the Try block to that line.

Your main problem was using the Dim statement for numberofRectangles and smallestRectangle inside the Sub. These variables loose there value at End Sub. They only exist inside the Sub. Although Dim has the same meaning as Private for declaring Class level varaibles, Private is preferred. Dim is used for local variable in a method.

To avoid the problem of having smallest rectangle always being zero, I checked the count and set smallestRectangle to the first rectangles area. After that Math.Min takes over.

Private numberofRectangles As Integer
Private smallestRectangle As Decimal

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim length As Decimal
    Dim width As Decimal
    If Not Decimal.TryParse(txtLength.Text, length) Then
        MessageBox.Show("Please check entries for valid numeric data")
        Exit Sub
    End If
    If Not Decimal.TryParse(txtWidth.Text, width) Then
        MessageBox.Show("Please check entries for valid numeric data")
        Exit Sub
    End If
    Dim Area As Decimal
    Try
        Area = width * length
    Catch ex As OverflowException
        MessageBox.Show("Please check to make sure entries aren't too large." &
        ex.Message)
        Exit Sub
    Catch ex As Exception
        MessageBox.Show(ex.Message)
        Exit Sub
    End Try
    numberofRectangles  = 1
    If numberofRectangles < 2 Then
        smallestRectangle = Area
    Else
        smallestRectangle = Math.Min(smallestRectangle, Area)
    End If
    txtArea.Text = area.ToString("n2")
    txtNumberOfRectangles.Text = numberofRectangles.ToString
    txtSmallestRectangle.Text = smallestRectangle.ToString("n2")
    txtLength.Select()
End Sub
  • Related