我有一个简单的程序,但是我不能让这个类返回一个矩形的区域;它总是返回0,包括类的变量。对于我的生活,我无法让这件事工作。这是主要形式:
Public Class Form1
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Dim rect As New Rectangle
'Dim strLength As String
'Dim strWidth As String
Double.TryParse(txtLength.Text, rect.Length)
Double.TryParse(txtWidth.Text, rect.Width)
'strLength = InputBox("Please input the length of the rectangle", "Calculate Area")
'Double.TryParse(strLength, rect.Length)
'strWidth = InputBox("Please input the width of the rectangle", "Calculate Area")
'Double.TryParse(strWidth, rect.Width)
lblArea.Text = rect.GetArea.ToString
End Sub
End Class
这是班级
Public Class Rectangle
Private _dblLength As Double
Private _dblWidth As Double
Public Property Length As Double
Get
Return _dblLength
End Get
Set(value As Double)
If value > 0 Then
_dblLength = value
Else
_dblLength = 0
End If
End Set
End Property
Public Property Width As Double
Get
Return _dblWidth
End Get
Set(value As Double)
If value > 0 Then
_dblLength = value
Else
_dblLength = 0
End If
End Set
End Property
'when you call New in main form it initalizes the variables
Public Sub New()
_dblLength = 0
_dblWidth = 0
End Sub
Public Function GetArea() As Double
Return _dblLength * _dblWidth
End Function
End Class
答案 0 :(得分:3)
您的Width
媒体资源出现错误:设置_dblLength
,但返回_dblWidth
。
_dblWidth
将始终为零,因此您的计算区域将始终为零。