OOP Principles

S

Scott Schluer

Hello,

I'm getting more and more familiar every day with VB.NET and I'm trying
right now to create custom classes for one of my applications. I've got some
experience with OOP, but I'm not sure exactly how to lay these classes out.
The questions are probably pretty basic for someone with experience so let's
just jump right in, shall we? :)

Scenario: I have a web application that allows users to search for homes for
sale, as well as allowing my clients to administer those homes. I'm creating
a class called RentalProperty. It has properties such as Address, City,
State, Beds, Baths, SquareFootage, IsFeaturedProperty, etc. You get the
idea. However, I also want to have a property "Amenities" which would return
a custom collection of an Amenity object (which I'm also creating). The
amenities are things like "Washer/Dryer", "Pool", etc.

My SQL Server database has the following tables:

Properties (property_id, address, city, state, etc)
Amenities (amenity_id, name)
Properties_Amenities (property_id, amenity_id)

So to this point, creating the RentalProperty class has been pretty simple.
I've also created a simple Amenity class with the following properties: ID,
Name. I also have an AmenityCollection class which returns a collection of
Amenity objects. So...here are my questions:

1) I have the Amenities property of the RentalProperty class set up like
this:

Private p_Amenities As AmenityCollection
Public Property Amenities() As AmenityCollection
Get
Return p_Amenities
End Get
Set(ByVal Value As AmenityCollection)
p_Amenities = Value
End Set
End Property

Here is the AmenityCollection code:

Public Class AmenityCollection : Inherits System.Collections.CollectionBase
Default Public ReadOnly Property Item(ByVal intIndex As Integer) As
Amenity
Get
Return CType(list.Item(intIndex), Amenity)
End Get
End Property

Public Sub Add(ByVal objItem As Amenity)
list.Add(objItem)
End Sub

Public Sub Remove(ByVal intIndex As Integer)
Try
list.RemoveAt(intIndex)
Catch exc As Exception
Throw New ArgumentOutOfRangeException
End Try
End Sub
End Class

1 cont'd) However when I am using a RentalProperty object, and I type
RentalProperty.Amenities. (and wait for intellisense in Visual Studio), it
doesn't show an "Items" property, so that I can do this: For Each objAmenity
As Amenity in RentalProperty.Amenities.Items. Maybe I have the collection
class set up incorrectly? The question is: what's the best way to set this
up so that the "Amenities" property of my RentalProperty returns a usable,
iterable collection of Amenity objects?

2) The Amenities class/collection is really not going to be utilized too
much other than as a property of the RentalProperty object. I'll need to be
able to populate the collection with the current amenities for a given
property, PLUS I want to be able to add/remove amenities (when they're
editing the property in the admin screen) and then call a .Save method to
save the current amenities back to the database. Now, should the .Save
method should be a member of the AmenityCollection class? That doesn't seem
right to me, but by the same token, adding .Save to the Amenity class
doesn't seem right since that's dealing with just one amenity, not posting
the entire set of amenities back to the database. Any clarification here?

That should do it for now! ;-) I'm sure I'm just missing a few key pieces of
knowledge and once I figure these few things out, things are going to be
looking good! Any explanations or references to URLs for further information
are appreciated!

Thanks,

Scott
 
C

Cor Ligthert

Scott,

The problem with creating classes for webapplication is that they are
stateless, which means that for every post the needed objects have to be
instanced. You see the problem before you?

A shared class is as well no solution because that is used by the
application. What means for all sessions together as one shared class until
there are no sessions anymore active. However when you have data that should
be for everybody without any client selection or value, than does that fit.

When you use a dataset (which is of course as well an object instanced from
a class) than you can nicely store that in a session, and get it easily back
when there is a postback in the way you readed it before the post. (When you
do a reread you have the change that there is been changed something by
somebody else)

I hope this helps a little bit?

Cor
 
O

One Handed Man \( OHM - Terry Burns \)

Try Building your solution first. Sometimes intellisense will not instantly
see the new properties of the class until you do this. I copied your code
into RentailProperty Class and Created an AmenityCollection property.
Switched back to the Main Form, create a new instance of the Rental Property
and then looked at its members. The Items property did not instantly show in
intellisense, however as soon as I did a build this worked fine and the Item
property appeared in the list of members


HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top