Good afternoon,
I'm trying to alter an application that calculates the area of a square using the following guidelines:
For the Square class:

1) Add a Private variable named _area
2) Associate the _area variable with a Property procedure called Area
3) Change the CalculateArea method to a Sub procedure. The method should calculate the area and then assign the result to the _area variable.
4) Include a parameterized constructor in the class. The constructor should accept one argument: the side measurement. After using the Public property to initialize the Private variable, the constructor should automatically call the CalculateArea method.

For the Main Form:

1) Modify the calcButton's Click even procedure so that it uses the parameterized constructor to instantiate the Square object. There parameterized constructor will automatically calculate the aera of the square, so the line of code that calls the CalculateArea method in the event procedure can be deleted.
2) Code the text box's Enter and TextChanged event procedures. The Enter event procedure should select the existing text. The TextChanged event procedure should clear the contents of the areaLabel.

At this point I believe I've completed the first 2 steps for the Square method, but I'm not sure where to go from there. I would appreciate any assistance or advice.
Here is my code for the Square class and the Main Form, in that order:

Public Class Square
    Private _side As Integer
    Private _area As Integer

    Public Property Area() As Integer
        Get
            Return _area
        End Get
        Set(ByVal value As Integer)
            If value > 0 Then
                _area = value
            Else
                _area = 0
            End If
        End Set
    End Property

    Public Property Side() As Integer
        Get
            Return _side
        End Get
        Set(ByVal value As Integer)
            If value > 0 Then
                _side = value
            Else
                _side = 0

            End If
        End Set
    End Property
    Public Sub New()
        _side = 0
    End Sub
    Public Sub New(ByVal sd As Integer)
        Side = sd
    End Sub
    Public Function CalculateArea() As Integer
        Return _side * _side

    End Function
End Class

Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm

    Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub calcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcButton.Click
        Dim mySquare As New Square
        Dim area As Integer

        Integer.TryParse(sideTextBox.Text, mySquare.Side)

        area = mySquare.CalculateArea()
        areaLabel.Text = area.ToString

        sideTextBox.Focus()
    End Sub
Member Avatar for Unhnd_Exception

It looks like all you need to do is add the Calculate area func in the constructor with the parameter. Just assign _area with the return value of the CalculateArea func.

In the button click event: create a new square object by passing in the textbox value. After your tryparse. After creating the object from the textbox value assign the areaLabels text with the square object's area property. Basically don't call the CalculateArea but create the instance with the variable and it will automatically calculate the area and then read the property.

Looks like you pretty much have it all done.

I also have to change the CalcuateArea from a function to a sub procedure. If you were to code the steps in your reply how would you go about doing so?

Member Avatar for Unhnd_Exception

I would probably do something like this.

Public Class Form1

    Public Class Square
        Private fpArea As Double
        Private fpSide As Double
        Private fpMaxDoubleSquared As Double

        Public ReadOnly Property Area() As Double
            Get
                Return fpArea
            End Get
        End Property

        Public Property Side() As Double
            Get
                Return fpSide
            End Get
            Set(ByVal value As Double)
                If value < 0 OrElse value > fpMaxDoubleSquared Then value = 0

                fpSide = value
                CalculateArea()
            End Set
        End Property

        Public Sub New()
            fpMaxDoubleSquared = Math.Sqrt(Double.MaxValue)
        End Sub

        Public Sub New(ByVal side As Double)
            Me.New() 'Call new with no constructors first so the maxsquared is set.

            If side < 0 OrElse side > fpMaxDoubleSquared Then side = 0

            fpSide = side
            CalculateArea()
        End Sub

        Private Sub CalculateArea()
            fpArea = fpSide * fpSide
        End Sub

    End Class

    Private Sub ButtonCalculateArea_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCalculateArea.Click
        If Not IsNumeric(TextBoxSide.Text) Then
            LabelArea.Text = "0"
            Exit Sub
        End If

        Dim Square As New Square(CDbl(TextBoxSide.Text))
        LabelArea.Text = CStr(Square.Area)
    End Sub

End Class

Thanks again, what do you think of the following code to fulfill this:

2) Code the text box's Enter and TextChanged event procedures. The Enter event procedure should select the existing text. The TextChanged event procedure should clear the contents of the areaLabel.

Private Sub sideTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles sideTextBox.Enter
        sideTextBox.SelectAll()
    End Sub

    Private Sub sideTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sideTextBox.TextChanged
        areaLabel.Text = String.Empty
    End Sub
Member Avatar for Unhnd_Exception

What do you think of it. Looks good to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.