0

所以我建立了一個帶有構造函數和方法的基類,但是我不斷得到_TotalCostInteger變量= 0,以及我被困住了。不返回值的方法[VB.NET]

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

看起來像你的屬性的設置不正確。它們不會將值設置爲您的成員變量。相反,他們使用value參數並將其設置爲屬性的當前值。他們應該是這樣的:

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

如果不單獨使用membervariables,你也可以使用autoimplemented性質是這樣的:

Public Property TotalCost As Integer 

爲了您更多的問題,我假設你使用的是Windows。形式? ComboBox有一個SelectedIndexChanged事件,您可以使用該事件重新計算值而不是按鈕的事件Click。見here

+0

我想補充一點,使用成員變量已經過時了自動實現屬性的可能性。並且,當您對成員變量使用完全實現的屬性時,應始終使用屬性而不是直接寫入成員變量。 – Alexander