Set/Get Statements, Please HELP!
I am having a problem with maintaing the integrity of the instance variables in the following class. Below is a module that generates two instances of the class CBuilding. The second variable passed to each of the CBuilding instances is intentionally made negative, it refers to the number of floors. In the class I have provided Set and Get statements to limit the values to non-negative numbers, but still the print out yields the negative values passed to the class by the module.
Still the output yields the negative numbers that should have been denied by the Set/Get statements.
Module modBuildOne
Sub Main()
Dim house As New CBuilding(2300, -2, 4, 300)
Dim office As New CBuilding(5500, -6, 25, 100)
'parameters passed: areainsqft, numOfFloors, numOfOccupants, maxAreaPerPerson
Console.WriteLine(" Building Type: House ")
house.PrintBuildingDetails()
Console.WriteLine()
Console.WriteLine(" Building Type: Office ")
office.PrintBuildingDetails()
End Sub
End Module 'modBuildOne
Public Class CBuilding
Inherits Object
Private mAreaInSqFt As Integer
Private mNumOfFloors As Integer
Private mNumOfOccupants As Integer
Private mMaxAreaPerPerson As Integer 'Max area allowed for the person
'Constructor
Public Sub New(ByVal area As Integer, ByVal floor As Integer, ByVal occupants _
As Integer, ByVal maxArea As Integer)
mAreaInSqFt = area
mNumOfFloors = floor
mNumOfOccupants = occupants
mMaxAreaPerPerson = maxArea
End Sub 'Constructor
Public Property Area() As Integer
Get
Return mAreaInSqFt
End Get
Set(ByVal Value As Integer)
If (Value >= 0 AndAlso Value < 100000) Then
mAreaInSqFt = Value
Else
mAreaInSqFt = 0
End If
End Set
End Property 'Area
Public Property Floor() As Integer
Get
Return mNumOfFloors
End Get
Set(ByVal Value As Integer)
If (Value > 0 AndAlso Value < 100) Then
mNumOfFloors = Value
Else
mNumOfFloors = 0
End If
End Set
End Property 'floor
Public Property Occupants() As Integer
Get
Return mNumOfOccupants
End Get
Set(ByVal Value As Integer)
If (Value >= 0 AndAlso Value < 100000) Then
mNumOfOccupants = Value
Else
mNumOfOccupants = 0
End If
End Set
End Property 'occupants
Public Property maxArea() As Integer
Get
Return mMaxAreaPerPerson
End Get
Set(ByVal Value As Integer)
If (Value >= 0 AndAlso Value < 100000) Then
mMaxAreaPerPerson = Value
Else
mMaxAreaPerPerson = 0
End If
End Set
End Property 'maxArea
Private Function AreaPerPerson() As Integer
Return (maxArea / Occupants)
End Function 'AreaPerPerson
Public Sub PrintBuildingDetails()
Console.WriteLine()
Console.WriteLine(" Total Area: {0} sqft", Area)
Console.WriteLine(" Total number of floors: {0} ", Floor)
Console.WriteLine(" Total number of occupants: {0} ", Occupants)
Console.WriteLine(" Area available per occupant: {0} sqft", AreaPerPerson())
Console.WriteLine(" Maximum Occupants if each one takes {0} sqft: {1:F}", maxArea, GetMaxOccupants(maxArea))
Console.WriteLine()
End Sub 'PrintBuildingDetails
Private Function GetMaxOccupants(ByVal maxArea As Integer)
Return Area / maxArea
End Function
End Class 'CBuilding
Output
Building Type: House
Total Area: 2300 sqft
Total number of floors : -2
Total number of occupants: 4
Area available per occupant: 75 sqft
Maximum occupants if each one takes 300 sqft: 7.67
Building Type: Office
Total Area: 5500 sqft
Total number of floors : -6
Total number of occupants: 25
Area available per occupant: 4 sqft
Maximum occupants if each one takes 300 sqft: 55
|