Ozymandias
Ozymandias

Reputation: 166

Method not returning a value [VB.NET]

So I've set up a basic class with a constructor and method, but I keep getting the _TotalCostInteger variable = 0, and well I'm stumped.

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

I then call the Total Cost with this:

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

The label which is equals to _TotalCostInteger remains zero, please help.

Also piggybacking off of this:
How do I change the text of a label without executing the event handler (pressing the button). What I wrote will only work if I hit the button

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

What I want is for the label's text to change as the selected index changes.

Upvotes: 0

Views: 153

Answers (1)

Philipp Grathwohl
Philipp Grathwohl

Reputation: 2836

Looks like the setter of your properties are not correct. They do not set the value to your member variables. Instead they use the value parameter and set them with the current value of the property. They should look like this:

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

If you do not use the membervariables separately, you can also use autoimplemented properties like this:

Public Property TotalCost As Integer

For your additional question, I assume you are using Windows.Forms? There is a SelectedIndexChanged event for the ComboBox that you can use to recalculate the values instead of a Click event of a button. See here.

Upvotes: 1

Related Questions