C# help

Discussion in 'OT Technology' started by jdog12, Dec 3, 2006.

  1. jdog12

    jdog12 New Member

    Joined:
    Nov 27, 2004
    Messages:
    5,474
    Likes Received:
    0
    I posted this in Off Topic and got no help, maybe I'll have better luck here.

    I have to do an assignment on inheritance. I need to create a bookstore. There is going to be 4 classes, Product, Books, Clothes, Bookstore.

    Product will hold basic product info such as price, qty, reorder point

    books and clothes will be subclasses of Product and will hold more detailed info... such as book author, call number for book. and size, material for clothes

    Bookstore will hold my main method and most of the interface stuff.

    The program should be able to do things such as buy a product, return a product, update inventory, etc.


    Anyways the assignment calls for making an array of type Product in Bookstore class, each element in the array will hold different products(some books, some clothes)

    Product[] myProducts = new Product[10]

    then individual products set up as

    myProducts[0] = new Book(insert arguments here)
    myProducts[1] = new Clothes(insert arguments here)


    problem is i need to access data in the subclasses, but i can only access data in the base class.

    ex. if i do:

    myProducts[0].ProductPrice = 5;

    it will change the product price(since it is in the Product class), but it will not let me change data in the subclasses(in this case it would be the Book class), anything inside Product is accesible, but things inside Book class are inaccessible, so BookAuthor would be inaccesible, if i try to do

    myProducts[0].BookAuthor = "john doe";

    it will not let me.

    How would i make it so that i can access that data?







    Here is a general layout, we'll ignore the clothes class for now.

    class Product
    double price;
    int quantity;
    int reorder point;

    properties for those vars
    constructor


    class Book
    string author
    string bookName;
    int callNumber

    properties & constructor



    class Bookstore

    Product[] myProducts = new Product[10]

    main() //i know, not correct format, but work w/ me
    {

    Bookstore start = new Bookstore();
    start.InitProducts();
    }

    public void InitProducts()
    {
    myProducts[0] = new Book(insert arguments here)
    myProducts[1] = new Clothes(insert arguments here)

    //anyways, the following would work since it is in product class
    myProducts[0].Price = "15.25";


    //the following would not work, how can i access this data?
    // it wont even let me do a Console.WriteLine and print this out, only
    //stuff in product class

    myProducts[0].BookAuthor = "john doe";

    }
     
  2. Smash_99

    Smash_99 OT Supporter

    Joined:
    Sep 25, 2004
    Messages:
    3
    Likes Received:
    0
    You need to cast the array element to the class type that you want to access. If you want to access the elements of the Book class do
    Code:
    if( myProducts[0] is Book )
    {
        Book book = (Book)myProducts[0];
        book.BookAuthor = "john doe";
    }
    
    The "is" operator checks to see if the object on the left side of the operator is an instance of the Class type on the right side. It then returns true if it is of type Book. The one catch with the "is" operator is that you must check for the final concrete classes (Ex. Book) before you check for the base class (Ex. Product), because every Book IS A Product.

    Another option to use is the "as" operator. It will automatically cast the object to the type specified, or set it to null if the cast was not successful. Its been a while since I've used it, but I believe the syntax is as follows.
    Code:
    Book b = myProducts[0] as Book;
    if( b != null )
    {
        b.BookAuthor = "john doe";
    }
    
     

Share This Page