Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 10th, 2005, 09:01 PM
Authorized User
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old April 13th, 2005, 09:48 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The class is behaving as it should as you wrote it. When you instantiate the class with a contructor, the properties are not used so your limitation logic never gets hit. I would suggest a change to your constructor:

Public Sub New(ByVal area As Integer, ByVal floor As Integer, ByVal occupants _
    As Integer, ByVal maxArea As Integer)
        Me.Area = area
        Me.Floor = floor
        Me.Occupants = occupants
        Me.MaxArea = maxArea
End Sub 'Constructor

This forces the accessor logic to run.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath: set operation with a disjoint node set rich_unger XSLT 7 May 6th, 2008 09:24 AM
regarding conditional statements abhi.prince4u XSLT 1 July 21st, 2007 06:00 AM
if statements brainchild Javascript 2 March 1st, 2007 06:08 AM
IF ELSE statements gmpurple Classic ASP Databases 4 November 15th, 2004 04:30 PM
If, else if statements Joel JSP Basics 0 March 18th, 2004 06:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.