Results 1 to 9 of 9

Thread: OOP Question: VB.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    4

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    4

    Re: OOP Question: VB.NET

    Can you show me a small code example?

    Thanks,

    Scott

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    4

    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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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:
    1. Public Class ArrayHolder
    2.   Private myArr(20) as Order
    3.  
    4.  Public Property ReadOnly(ByRef i as integer) as Order
    5.   Get
    6.      return myArr(i) 'Of course, you'd need some code to make sure that i is in range.
    7.   End Get
    8.  End Property
    9. End Class
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    4

    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.

  7. #7
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: OOP Question: VB.NET

    Building on SH's example, you could also do something like this:
    VB Code:
    1. Public Class ArrayHolder
    2.     Inherits ArrayList
    3.  
    4.     Public Shadows Function Add(ByVal value As Order) As Integer
    5.         Return MyBase.Add(value)
    6.     End Function
    7. 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.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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:
    1. For x=0 to Customer.MaxOrder
    2.  'Whatever
    3. 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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: OOP Question: VB.NET

    Quote 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:
    1. Public Class Customer
    2.  
    3.     Private _orders As List(Of Order)
    4.  
    5.     Public ReadOnly Property Orders() As List(Of Order)
    6.         Get
    7.             If Me._orders Is Nothing Then
    8.                 Me._orders = New List(Of Order)
    9.             End If
    10.  
    11.             Return Me._orders
    12.         End Get
    13.     End Property
    14.  
    15. End Class
    16.  
    17. Public Class Order
    18.  
    19.     Private _orderItems As List(Of OrderItem)
    20.  
    21.     Public ReadOnly Property OrderItems() As List(Of OrderItem)
    22.         Get
    23.             If Me._orderItems Is Nothing Then
    24.                 Me._orderItems = New List(Of OrderItem)
    25.             End If
    26.  
    27.             Return Me._orderItems
    28.         End Get
    29.     End Property
    30.  
    31. End Class
    32.  
    33. Public Class OrderItem
    34.  
    35. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width