about inheritance and array

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

Assume we have the following classes and interfaces.
We have a class called Inventory that is associated to a class called
Product.

We have some classes that is derived from this Product class and from two
interfaces called Wearable and Rentable.
These classes has the following names Book, Soda, Shoe, Coffee, BubbleGum
and Underwear.

Below I have some code and there is a row that I want to ask about.
It's this row "Product[] inventory = read_all_products();" that I have a
question about
Normally when we have an array we do Product[] inventory = new
Product[50]; // allocating memory for the array
But here you assign the return value from method "read_all_products();".
I can't understand the construction assigning the return value from this
read_all_products();"?
What type does this read_all_products(); actually returns?
I just can't understand how it's possible to load the array without
allocation memory by using the new operator.?

If this instead had the following construction I would understand it.
Product[] inventory = new Product[50];
read_all_products();

All this is about explaining this row construction. "Product[] inventory
= read_all_products();"

namespace Inventory
{
public class ProductHandler
{
public static void Main()
{
Product[] inventory = read_all_products(); //load inventory
foreach (Product p in inventory) //Check all products in array
{
if (p is Rentable) //It it rentable?
assign_to_rental_department(p); //Allocate it

if (p is Wearable) //Is it wearable?
{
Wearable w = (Wearable) p; //Yes, cast it so
w.PutOn(); // we can put
it on
}
}
}
}
}

//Tony
 
...
What type does this read_all_products(); actually returns?

It returns a reference to one object, in this case a reference to an
instanmce of an array object, i.e. it doesn't return the array itself.

An array of this type doesn't even consist of instances of objects
themselves, but of *references* to instances of objects.

So...

The array, and the instances themselves, will (hopefully) already exist
someplace when the method returns the reference to the array.

// Bjorn A
 
Product[] read_all_products()
{
Product[] localInventory = new Product[50]; // allocating memory for
the array
//
// : build Inventory
//
return localInventory;
}

However, you probably don't want to lock yourself in to exactly 50 products,
so


Product[] read_all_products()
{
ArrayList tmpInventory = new ArrayList();
//
// : build Inventory
//
Product[] localInventory = new Product[tmpInventory.Count];
tmpInventory.CopyTo(localInventory);
return localInventory;
}



Tony said:
Hello!

Assume we have the following classes and interfaces.
We have a class called Inventory that is associated to a class called
Product.

We have some classes that is derived from this Product class and from
two interfaces called Wearable and Rentable.
These classes has the following names Book, Soda, Shoe, Coffee,
BubbleGum and Underwear.

Below I have some code and there is a row that I want to ask about.
It's this row "Product[] inventory = read_all_products();" that I
have a question about
Normally when we have an array we do Product[] inventory = new
Product[50]; // allocating memory for the array
But here you assign the return value from method
"read_all_products();". I can't understand the construction assigning the
return value from
this read_all_products();"?
What type does this read_all_products(); actually returns?
I just can't understand how it's possible to load the array without
allocation memory by using the new operator.?

If this instead had the following construction I would understand it.
Product[] inventory = new Product[50];
read_all_products();

All this is about explaining this row construction. "Product[] inventory
= read_all_products();"

namespace Inventory
{
public class ProductHandler
{
public static void Main()
{
Product[] inventory = read_all_products(); //load
inventory foreach (Product p in inventory) //Check all
products in array {
if (p is Rentable) //It it rentable?
assign_to_rental_department(p); //Allocate it

if (p is Wearable) //Is it wearable?
{
Wearable w = (Wearable) p; //Yes, cast it so
w.PutOn(); // we can
put it on
}
}
}
}
}

//Tony
 
Below I have some code and there is a row that I want to ask about.
It's this row "Product[] inventory = read_all_products();" that I have a question about
Normally when we have an array we do Product[] inventory = new Product[50]; // allocating memory
for the array
But here you assign the return value from method "read_all_products();".
I can't understand the construction assigning the return value from this read_all_products();"?

Inside read_all_products() there will be the equivalent of new. It will allocate the storage for the
array and it's elements and then return a reference to the array.

It most likely Creates an ArrayList to hold the Products that it reads. It the converts the
ArrayList to an Array and returns a reference to that array.

Bill
 
However, you probably don't want to lock yourself in to exactly 50 products, so


Product[] read_all_products()
{
ArrayList tmpInventory = new ArrayList();
//
// : build Inventory
//
Product[] localInventory = new Product[tmpInventory.Count];
tmpInventory.CopyTo(localInventory);
return localInventory;
}

Or instead of
Product[] localInventory = new Product[tmpInventory.Count];
tmpInventory.CopyTo(localInventory);

How about
Product[] localInventory = (Product[])(tmpInventory.ToArray(Product));

The call to "new Product[]" is hidden inside of ToArray()

Bill
 
Back
Top