Wrox Programmer Forums
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 6th, 2006, 12:26 AM
Authorized User
Join Date: Jun 2005
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez
Default Object Design Pattern problem

We have an object called Product and ProductCollection defined below:
class Product{
public string Name;
public Type.Category Category;

class ProductCollection{
public Product[] List;

what we want to do is to add an attribute to the Product class by creating a derived class called ProductChild(inherits from Product) which will contain the additional attributes.

class ProductChild : Product{
public string AdditionalAttribute;

class ProductChildCollection{
public new ProductChild[] List;

The problem is how to to Create a ProductChildCollection class and cast(convert) the parent ProductCollection into ProductChildCollection without iterating throught the Product List in the ProductCollection.

ProductChildCollection childCollection = (ProductChildCollection)ProductCollection();//casting problem, cannot cast a parent into a child

Our initial solution is to iterate through the ProductCollection and generate child objects from that.

Once that is done we can then set the properties of the additional attributes into each of the ProductChild List items from ProductChildCollection class.

Any ideas?

"Dont you ever give up!"
\"Dont you ever give up!\"
Old September 6th, 2006, 03:08 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma

Surely, with what you have, casting wont work the way you
have proposed here. But what you can do is to write all the
conversion/traversal logic in a member function or an operator
overload in one of these collection classes or in a common function
which does the conversion whenever its needed.

I would add though, that there are better ways of implementing
collections. You could have based these collection classes on
collection and enumeration interfaces, which makes traversal
easy. It would have also helped you in keeping the things cleaner
and casting issues would be much easier to manage as the collection
would all have a common base.

Old September 7th, 2006, 04:10 AM
Authorized User
Join Date: Jun 2005
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez

Thanks Ankur, I'll be trying explicit casting/conversions first,then if it didn't worked Il go with your idea. Thanks anyway.

"Dont you ever give up!"

Similar Threads
Thread Thread Starter Forum Replies Last Post
Throughput/Design Problem tobriain C# 0 June 28th, 2007 10:04 AM
Design Question/Problem dparsons ASP.NET 1.0 and 1.1 Professional 0 November 20th, 2006 12:56 PM
design pattern kishore.dyn Struts 1 February 14th, 2006 02:36 PM
DirectoryInfo.GetFiles(pattern): search pattern fo arif_1947 VS.NET 2002/2003 1 October 19th, 2004 11:59 PM
Java Design issue with UML and Design Patterns the_logical_way Apache Tomcat 0 May 31st, 2004 04:02 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.