|
-
Jul 5th, 2006, 08:41 AM
#1
Thread Starter
New Member
OOP Question: VB.NET
Hello,
I'm getting started in OOP with VB.NET and I have a question. I understand the creation and implementation of classes, etc. but let's assume I have a Customers class, with the usual properties: first name, last name, address, etc.
I also want to be able to use the Customers class to access a collection of the orders this customer has placed.
Now, I have an Orders class in place also. Order date, billing address, etc.
Below this, I want to be able to access a list of the individual items purchased for this order (which is the OrderItems class).
So, if I'm in Visual Studio, I want to be able to call up a customer class and do this sort of thing.
Dim oCustomer As Customer = New Customer(CustID)
For Each Order In oCustomer.Orders
'Do Something
Next
Response.Write(oCustomer.Orders(0).CardType)
For Each OrderItem in oCustomer.Orders(0).OrderItems
'Do something
Next
How do I accomplish this? Links to existing articles or an explanation would help!
Thanks,
Scott
-
Jul 5th, 2006, 08:51 AM
#2
Re: OOP Question: VB.NET
You would need to give your Order class a property that returns a collection of OrderItem objects and then give you Customer class a property that returns a collection of Order objects. There are plenty of instances of this in the Framework already. For instance, the DataSet class has properties named Tables and Relations that return collections of DataTables and DataRelations. The DataTable class, in turn, has Columns and Rows properties that return collections of DataColumns and DataRows.
-
Jul 5th, 2006, 09:02 AM
#3
Thread Starter
New Member
Re: OOP Question: VB.NET
Can you show me a small code example?
Thanks,
Scott
-
Jul 5th, 2006, 09:15 AM
#4
Thread Starter
New Member
Re: OOP Question: VB.NET
Actually, this doesn't accomplish what I needed. That returns a datatable, not a collection of another class.
Using that example, I can't type the following:
oCustomer.Orders(0).BillingAddress
I'd have to type:
oCustomer.Orders(0).Rows(5)
Sorry I don't know how to explain the difference better (not sure of the terms to use) but I need to return a collection of a class, not a datatable.
-
Jul 5th, 2006, 09:26 AM
#5
Re: OOP Question: VB.NET
Actually, a SMALL code example of this is probably impossible. Why are you using classes in this case? This really sounds like a database situation....no, wait, it sounds like a homework type of situation. Do you HAVE to have it this way?
I can understand that every order must have a list of order items. I can also understand that every order must have been made by a customer, and thus a customer can have a list of orders which has a list of order items. However, it makes more sense to have this in a database where each order has a Customer ID which links it to a Customer table, and each orderItem has an OrderID that links it to an Order.
Just went looking for a simple example that would be meaningful, but found nothing. It would look something like this:
VB Code:
Public Class ArrayHolder
Private myArr(20) as Order
Public Property ReadOnly(ByRef i as integer) as Order
Get
return myArr(i) 'Of course, you'd need some code to make sure that i is in range.
End Get
End Property
End Class
My usual boring signature: Nothing
 
-
Jul 5th, 2006, 09:32 AM
#6
Thread Starter
New Member
Re: OOP Question: VB.NET
Far from a homework situation. I'm getting paid on this job and I could just as easily do it the way I've always done it, but I'm looking to expand my knowledge on this a little bit and try a different method of implementation.
I don't HAVE to use classes, I want to so I can learn how to use them more effectively. I should be able to return a collection of classes without too much problem I'm just trying to figure out the correct method of implementation.
-
Jul 5th, 2006, 09:51 AM
#7
Re: OOP Question: VB.NET
Building on SH's example, you could also do something like this:
VB Code:
Public Class ArrayHolder
Inherits ArrayList
Public Shadows Function Add(ByVal value As Order) As Integer
Return MyBase.Add(value)
End Function
End Class
The index method is inherited. You won't need to make your own (but you are allowed to override if you want to customize its functionality).
Last edited by sevenhalo; Jul 5th, 2006 at 10:00 AM.
Reason: Added "Return" to fully mirror the base class. Sorry.
-
Jul 5th, 2006, 10:37 AM
#8
Re: OOP Question: VB.NET
After writing that, I got thinking that this didn't really sound all that much like homework, but I had to ask because it seems like a relatively tortured solution.
After looking at your post #4, it seems like my earlier post was not quite on the mark. It covers one level, but you would want a second level:
A listbox has a Items() collection, which has individual Item objects. In your case, you want to be able to access an Order, or an OrderItem, which would be within an order. Either one should be accessible to the customer. Therefore, I would suggest that a Customer has an Order() property.
The Order() property takes an integer index into the array of Order objects held in the Customer() object, and it returns an Order. This is pretty much what I wrote. However, an Order object would have an OrderItem() property which would also take an integer index into the array of OrderItem objects held in the Order object. This would allow you to do this:
Customer.Order(x) : Returns Order object number x.
Customer.Order(x).OrderItem(y) : Returns OrderItem number y of Order number x.
Customer.Order(x).OrderItem(y).ItemName : Returns the name of said item.
By the way, you might also want a property for both the Customer and the Order objects that returns that maximum index number of the contained arrays. I personally find it more easy to iterate through a contained collection like this if I can write it in this fashion:
VB Code:
For x=0 to Customer.MaxOrder
'Whatever
Next
If any part of the first paragraph appears choppy, it's because I wrote one thing, then changed to something else, and may have screwed up my edit.
My usual boring signature: Nothing
 
-
Jul 5th, 2006, 04:45 PM
#9
Re: OOP Question: VB.NET
 Originally Posted by Scott927
Actually, this doesn't accomplish what I needed. That returns a datatable, not a collection of another class.
Using that example, I can't type the following:
oCustomer.Orders(0).BillingAddress
I'd have to type:
oCustomer.Orders(0).Rows(5)
Sorry I don't know how to explain the difference better (not sure of the terms to use) but I need to return a collection of a class, not a datatable.
Ugh. I was using the DataSet as an example of three tiers of types. I'm not saying that you should use DataSets or DataTables. I'm saying that you need to mimic that structure. Just like a DataSet has a collection DataTables has a collection of DataRows, you need to have a Customer that has a collection of Orders that has a collection of OrderItems.
VB Code:
Public Class Customer
Private _orders As List(Of Order)
Public ReadOnly Property Orders() As List(Of Order)
Get
If Me._orders Is Nothing Then
Me._orders = New List(Of Order)
End If
Return Me._orders
End Get
End Property
End Class
Public Class Order
Private _orderItems As List(Of OrderItem)
Public ReadOnly Property OrderItems() As List(Of OrderItem)
Get
If Me._orderItems Is Nothing Then
Me._orderItems = New List(Of OrderItem)
End If
Return Me._orderItems
End Get
End Property
End Class
Public Class OrderItem
End Class
Even if you're using VB 2005 I'd still be inclined to create my own collection classes derived from CollectionBase for this rather than using Lists, but that is the basic structure.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|