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