方法不返回值[VB.NET]

时间:2015-11-24 04:57:35

标签: vb.net visual-studio visual-studio-2010 visual-studio-2012

所以我已经使用构造函数和方法设置了一个基本类,但我不断收到_TotalCostInteger变量= 0,而且我很难过。

Public Class FlooringClass
Protected _LengthInteger As Integer
Protected _WidthInteger As Integer
Protected _AreaInteger As Integer
Protected _CustomerNameString As String
Protected _TotalCostInteger As Decimal
Protected _PriceInteger As Integer
Sub New(ByVal LengthIn As Integer, ByVal WidthIn As Integer, ByVal NameIn As String, ByVal PriceIn As Integer)
    Length = LengthIn
    Width = WidthIn
    Name = NameIn
    Price = PriceIn
    CalculateArea()
    CalculateCost()
End Sub
Public Property Length As Integer
    Get
        Return _LengthInteger
    End Get
    Set(value As Integer)
        value = Length
    End Set
End Property
Public Property Width As Integer
    Get
        Return _WidthInteger
    End Get
    Set(value As Integer)
        value = Width
    End Set
End Property
Public Property Area As Integer
    Get
        Return _AreaInteger
    End Get
    Set(value As Integer)
        value = Area
    End Set
End Property
Public Property Name As String
    Get
        Return _CustomerNameString
    End Get
    Set(value As String)
        value = Name
    End Set
End Property
Public Property Price As Integer
    Get
        Return _PriceInteger
    End Get
    Set(value As Integer)
        value = Price
    End Set
End Property
Public Property TotalCost As Integer
    Get
        Return _TotalCostInteger
    End Get
    Set(value As Integer)
        value = TotalCost
    End Set
End Property
Public Sub CalculateArea()
    _AreaInteger = _LengthInteger * _WidthInteger
End Sub
Overridable Sub CalculateCost()
    Const CARPET_Integer As Integer = 10
    Const LAMINATE_Integer As Integer = 20
    Const CERAMIC_Integer As Integer = 25
    Const Hardwood_integer As Integer = 30
    If Price = 0 Then
        _TotalCostInteger = _AreaInteger * CARPET_Integer
    ElseIf Price = 1 Then
        _TotalCostInteger = _AreaInteger * LAMINATE_Integer
    ElseIf Price = 2 Then
        _TotalCostInteger = _AreaInteger * CERAMIC_Integer
    ElseIf Price = 3 Then
        _TotalCostInteger = _AreaInteger * Hardwood_integer
    End If
End Sub
End Class

然后我用这个来调用总成本:

FlooringObject = New FlooringClass(CInt(LengthTextBox.Text),     CInt(WidthTextBox.Text), NameTextBox.Text, FloorComboBox.SelectedIndex)
CostLabel.Visible = True
CostLabel.Text = (FlooringObject.TotalCost).ToString("C")

等于_TotalCostInteger的标签仍然为零,请帮助。

还要撇开这个:
如何在不执行事件处理程序的情况下更改标签文本(按下按钮)。 我写的内容只有在按下按钮时才有效

If FloorComboBox.SelectedIndex <> -1 Then
    If FloorComboBox.SelectedIndex = 0 Then
        PriceLabel.Text = (PriceInteger(0).ToString("C"))
        PriceLabel.Visible = True
    ElseIf FloorComboBox.SelectedIndex = 1 Then
        PriceLabel.Text = PriceInteger(1).ToString("C")
        PriceLabel.Visible = True
    ElseIf FloorComboBox.SelectedIndex = 2 Then
        PriceLabel.Text = PriceInteger(2).ToString("C")
        PriceLabel.Visible = True
    ElseIf FloorComboBox.SelectedIndex = 3 Then
        PriceLabel.Text = PriceInteger(3).ToString("C")
        PriceLabel.Visible = True
    End If

我想要的是标签的文本随着所选索引的变化而变化。

1 个答案:

答案 0 :(得分:1)

看起来你的属性的setter不正确。它们不会将值设置为您的成员变量。相反,他们使用value参数并使用属性的当前值设置它们。他们应该是这样的:

Public Property TotalCost As Integer
    Get
        Return _TotalCostInteger
    End Get
    Set(value As Integer)
        _TotalCostInteger = value
    End Set
End Property

如果您不单独使用membervariables,您也​​可以使用如下自动实现的属性:

Public Property TotalCost As Integer

对于您的其他问题,我假设您使用的是Windows.Forms? ComboBox有一个SelectedIndexChanged事件,您可以使用该事件重新计算值而不是按钮的Click事件。请参阅here