Results 1 to 4 of 4

Thread: Should class properties be values or references to objects?

  1. #1

    Thread Starter
    Junior Member jamison's Avatar
    Join Date
    Jan 2005
    Posts
    26

    Should class properties be values or references to objects?

    Should class properties be values or references to objects?

    Here's my situation: Reseller Has Customers Has Services

    In my app, the relationship between objects rarely changes. So once a customer is setup for a given reseller, he will pretty much always have that association.

    I frequently do relational type operations such as getting the name of a reseller, when given only the customer or service.

    I would like to keep database hits to a minimum.


    #1. customer class should contain a reference object for the reseller
    VB Code:
    1. Public Class Customer
    2.   public CustID as integer '(readonly)
    3.   public ResellerID as integer
    4.   public Services as ServiceCollection
    5. End Class
    6.  
    7. 'To get the reseller's name from a Customer object
    8. dim theCustomer as new Customer(1)
    9. dim theReseller as new Reseller(theCustomer.ResellerID)
    10. strResellerName = theReseller.Name

    or #2. the resellers' key so that it can be retrieved if needed.
    VB Code:
    1. Public Class Customer
    2.   public CustID as integer '(readonly)
    3.   public Reseller as Reseller
    4.   public Services as ServiceCollection
    5. End Class
    6.  
    7. 'To get the reseller's name from a Customer object
    8. dim theCustomer as new Customer(1)
    9. strResellerName = theCustomer.Reseller.Name

    Right now I'm doing #1. It has benefits such as being able to the the reseller's name with statements like this: strName = theCustomer.Reseller.CompanyName. Unfortunately this is really starting to be a pain in the butt, since I have to set those references all the time. But at the same time, I'm only instantiating the Reseller object one time so there is only one database hit.

    Thanks for any opinions,
    Jamison
    Last edited by jamison; Oct 19th, 2005 at 03:43 PM.

  2. #2

    Thread Starter
    Junior Member jamison's Avatar
    Join Date
    Jan 2005
    Posts
    26

    Re: Should class properties be values or references to objects?

    Bah, writing this pretty much answered my question. Maintaining all the object references is just not elegant at all.

    I started down this road because originally I was using an object manager that would keep track of what had been instantiated in the given application. I dropped the manager for other reasons, but somehow idea #2 stuck around.

    J

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Should class properties be values or references to objects?

    Just out of curiosity, what are you going with then?
    Because I don't like option 2.
    The Customer should not contain the Reseller object. The Reseller is the Parent object. Using option 2 would make it possible to do this:
    theCustomer.Reseller.Customers(3).Reseller.Customers(1).Reseller.Customers(5)

    But if you are doing that, you should have a proper variable theReseller to begin with anyways.

    Just my two centavos.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Junior Member jamison's Avatar
    Join Date
    Jan 2005
    Posts
    26

    Re: Should class properties be values or references to objects?

    Hey TG,

    You are right on both accounts. Circular refernces can easily happen. And I generally have the proper parent object available.

    The main appeal of #2 was being able to get reseller properties with only a service object.

    I've pretty much talked myself out of #2. I'm finding only a couple objects where it makes sense.



    Jamie

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