Re: More adventures in learning OOP
- From: "Peter Rilling" <peter@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 14 Dec 2005 14:58:54 -0800
There are usually two ways of defining ownership. Depending on your needs,
you can use either one or both.
1) If an order belongs to only a single person, then you can pass an
instance of the person into the constructor for the order then provide a
proeprty to access the owner. This way the order knows who owns it. This
allwos you to go from order to person without a lot of work.
public class Order{
public Order( Person owner ){...}
public Person Owner{get{...}}
private Order m_owner = null;
}
2) You can provide a collection (or some list) in the Person class that
contains a list of all orders. This allows you to go from person to orders.
Below is a generic list, but you can always make it strongly typed by
creating your own collection or using generics.
public class Person{
public ArrayList Orders {get{...}}
private ArrayList m_orders = new ArrayList();
}
Now, with the above, you can do something like:
Person myPerson = new Person();
myPerson.Orders.Add(new Order(myPerson));
Variations on this exist, but these are the two primary ways to maintain
relationships.
"RSH" <way_beyond_oops@xxxxxxxxx> wrote in message
news:%231UMHAQAGHA.3984@xxxxxxxxxxxxxxxxxxxxxxx
>
> I am exploring OOP and I am trying to get a grasp on the basics. Below is
> the code that creates a basic Person with basic properties.
>
> I also have an Orders Class where Orders are made by Persons. I am having
> trouble figuring out how to "connect" the two so that when a new Order is
> created I can specify the Person that it belongs to.
>
> Here is the code...how do I go about making an Order belong to a
> Person...or am I way off base here?
>
> Thanks!
> Ron
>
>
>
> using System;
>
> using System.Collections.Generic;
>
> using System.Text;
>
> namespace ClassTesting
>
> {
>
> public class Person
>
> {
>
> private string firstName;
>
> private string lastName;
>
> private Address mailingAddress;
>
> private Address billingAddress;
>
> private Address shippingAddress;
>
> private Phone homePhone;
>
> private Phone cellPhone;
>
> public Person()
>
> {
>
> this.mailingAddress = new Address();
>
> this.billingAddress = new Address();
>
> this.shippingAddress = new Address();
>
> this.homePhone = new Phone();
>
> this.cellPhone = new Phone();
>
> }
>
> public Address ShippingAddress
>
> {
>
> get { return shippingAddress; }
>
> }
>
> public Address BillingAddress
>
> {
>
> get { return billingAddress; }
>
> }
>
> public Address MailingAddress
>
> {
>
> get { return mailingAddress; }
>
> }
>
> public Phone HomePhone
>
> {
>
> get { return homePhone; }
>
> }
>
> public Phone CellPhone
>
> {
>
> get { return cellPhone; }
>
> }
>
> public string LastName
>
> {
>
> get { return lastName; }
>
> set { lastName = value; }
>
> }
>
> public string FirstName
>
> {
>
> get { return firstName; }
>
> set { firstName = value; }
>
> }
>
> }
>
> public class Address
>
> {
>
> private string line1;
>
> private string city;
>
> private string state;
>
> private string postalCode;
>
> public string PostalCode
>
> {
>
> get { return postalCode; }
>
> set { postalCode = value; }
>
> }
>
> public string State
>
> {
>
> get { return state; }
>
> set { state = value; }
>
> }
>
> public string City
>
> {
>
> get { return city; }
>
> set { city = value; }
>
> }
>
> public string Line1
>
> {
>
> get { return line1; }
>
> set { line1 = value; }
>
> }
>
> }
>
> public class Phone
>
> {
>
> private int areaCode;
>
> private int prefix;
>
> private int postfix;
>
> private int extension;
>
> public int AreaCode
>
> {
>
> get { return areaCode; }
>
> set { areaCode = value; }
>
> }
>
> public int Prefix
>
> {
>
> get { return prefix; }
>
> set { prefix = value; }
>
> }
>
> public int Postfix
>
> {
>
> get { return postfix; }
>
> set { postfix = value; }
>
> }
>
> public int Extension
>
> {
>
> get { return extension; }
>
> set { extension = value; }
>
> }
>
> public string PhoneNumber
>
> {
>
> get { return "(" + this.AreaCode.ToString() + ")" + this.Prefix.ToString()
> + "-" + this.Postfix.ToString(); }
>
> }
>
> }
>
>
>
>
>
>
>
> public class Orders
>
> {
>
> private int orderNumber;
>
> private DateTime orderDate;
>
> private string quantity;
>
> private string product;
>
> public int OrderNumber
>
> {
>
> get { return orderNumber; }
>
> set { orderNumber = value; }
>
> }
>
> public DateTime OrderDate
>
> {
>
> get { return orderDate; }
>
> set { orderDate = value; }
>
> }
>
> public string Quantity
>
> {
>
> get { return quantity; }
>
> set { quantity = value; }
>
> }
>
> public string Product
>
> {
>
> get { return product; }
>
> set { product = value; }
>
> }
>
> }
>
> }
>
>
.
- Follow-Ups:
- Re: More adventures in learning OOP
- From: Joanna Carter [TeamB]
- Re: More adventures in learning OOP
- References:
- More adventures in learning OOP
- From: RSH
- More adventures in learning OOP
- Prev by Date: Re: More adventures in learning OOP
- Next by Date: Re: Calling Mr. Jon Skeet
- Previous by thread: Re: More adventures in learning OOP
- Next by thread: Re: More adventures in learning OOP
- Index(es):
Relevant Pages
|