Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 6th, 2006, 12:26 AM
Authorized User
 
Join Date: Jun 2005
Location: , , Philippines.
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!\"
Reply With Quote
  #2 (permalink)  
Old September 6th, 2006, 03:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
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
Default

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.

Regards
Ankur
Reply With Quote
  #3 (permalink)  
Old September 7th, 2006, 04:10 AM
Authorized User
 
Join Date: Jun 2005
Location: , , Philippines.
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez
Default

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!"
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:00 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.