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