Two inheritance trees

  • Thread starter Thread starter wojtas
  • Start date Start date
W

wojtas

Hello

I'm trying to create application with as much as possible methods
derived from base class. I use two base classes "Document" and
"DocumentItem", each document has collection of documentItems. When I
cast object of Document class on one of its subclasses collection of
documentItems collection remains in base class. I already know that I
cannot do it with generic collections. Can anybody help me with this,
or point in right direction?
 
"wojtas" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I'm trying to create application with as much as possible methods
| derived from base class. I use two base classes "Document" and
| "DocumentItem", each document has collection of documentItems. When I
| cast object of Document class on one of its subclasses collection of
| documentItems collection remains in base class. I already know that I
| cannot do it with generic collections. Can anybody help me with this,
| or point in right direction?

Without any code as example, it is difficult to see what your problem is.

However, why would you say generics doesn't help ?

class DocumentItem
{
}

class DocumentItemCollection<T> : List<T> where T : DocumentItem { }

class Word : DocumentItem
{
}

class Paragraph : DocumentItem
{
private DocumentItemCollection<Word> words = new
DocumentItemCollection<Word>();

public DocumentItemCollection<Word> Words
{
get { return words; }
}
}

class Document : DocumentItem
{
private DocumentItemCollection<Paragraph> paragraphs = new
DocumentItemCollection<Paragraph>();

public DocumentItemCollection<Paragraph> Paragraphs
{
get { return paragraphs; }
}

private DocumentItemCollection<Document> documents = new
DocumentItemCollection<Document>();

public DocumentItemCollection<Document> Documents
{
get { return documents; }
}
}

Or am I missing something ?

Joanna
 
Joanna Carter [TeamB] napisa³(a):

Thanks for detailed reply, provided code sample is very helpful,
however my problem is slightly different (below).
Without any code as example, it is difficult to see what your problem is.
However, why would you say generics doesn't help ?

I've thought that generic collections prevent using inheritance.

I've planned structure similiar to described in folloving code:

class DocumentItem
{
}

class DocumentItem1 : DocumentItem
{
}

class DocumentItem2 : DocumentItem
{
}

class DocumentItemCollection<T> : List<T> where T : DocumentItem
{
}

class Document
{
private DocumentItemCollection<DocumentItem> items = new
DocumentItemCollection<DocumentItem>();
public virtual DocumentItemCollection<DocumentItem> items
{
get { return words; }
}
}

class Document1 : Document
{
private DocumentItemCollection<DocumentItem1> items = new
DocumentItemCollection<DocumentItem1>();
public override DocumentItemCollection<DocumentItem1> items
{
get { return words; }
}
}

class Document2 : Document
{
private DocumentItemCollection<DocumentItem1> items = new
DocumentItemCollection<DocumentItem1>();
public override DocumentItemCollection<DocumentItem1> items
{
get { return words; }
}
}

I'm aware of errors in presented code, but not quite aware of good
solution :)
 
This code is closer to what I want to achieve:

class DocumentItem
{
public string getText(){
return "a";
}
}

class DocumentItem1 : DocumentItem
{
public string getText(){
return "a1";
}
}

class DocumentItem2 : DocumentItem
{
public string getText(){
return "a2";
}
}

class DocumentItemCollection<T> : List<T> where T : DocumentItem
{
}

class Document
{
private DocumentItemCollection<DocumentItem> items = new
DocumentItemCollection<DocumentItem>();
public DocumentItemCollection<DocumentItem> Items
{
get { return items; }
}
}

class Document1 : Document
{
private DocumentItemCollection<DocumentItem1> items = new
DocumentItemCollection<DocumentItem1>();
public new DocumentItemCollection<DocumentItem1> Items
{
get { return items; }
}
}

class Document2 : Document
{
private DocumentItemCollection<DocumentItem2> items = new
DocumentItemCollection<DocumentItem2>();
public new DocumentItemCollection<DocumentItem2> Items
{
get { return items; }
}
}


I want to get items returning "a2" when I cast object of Document class
on Document2 class. Maybe this code is completly wrong, and there is
better way to achieve this, I'll appreciate any help.
 
"wojtas" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I've thought that generic collections prevent using inheritance.

Not necessarily, it depends on the design :-)

| I've planned structure similiar to described in folloving code:

How about this ?

class CompositeDocument<T> where T : Document
{
private List<T> items = new List<T>();

public List<T> Items
{
get { return items; }
}
}

class Document : CompositeDocument<Document> { }

class Document1 : CompositeDocument<Document1> { }

Joanna
 
Not necessarily, it depends on the design :-)

Now I see that inheritance with generics is possible, but I'm still
confused :)
How about this ?

class CompositeDocument<T> where T : Document
{
private List<T> items = new List<T>();

public List<T> Items
{
get { return items; }
}
}

class Document : CompositeDocument<Document> { }

class Document1 : CompositeDocument<Document1> { }

I've to admit, You've lost me :) I don't see how this code would
compile, is "Document" class supposed to inherit from
"CompositeDocument" class, which is derived from "Document" class?

I dont have problems with single inheritance tree, but two trees, when
object from one tree uses objects from analogic location in second
tree.
 
"wojtas" <[email protected]> a écrit dans le message de (e-mail address removed)...

| > How about this ?
| >
| > class CompositeDocument<T> where T : Document
| > {
| > private List<T> items = new List<T>();
| >
| > public List<T> Items
| > {
| > get { return items; }
| > }
| > }
| >
| > class Document : CompositeDocument<Document> { }
| >
| > class Document1 : CompositeDocument<Document1> { }
|
| I've to admit, You've lost me :) I don't see how this code would
| compile, is "Document" class supposed to inherit from
| "CompositeDocument" class, which is derived from "Document" class?

Sorry, I forgot to paste in the correct code :

class DocumentItem
{
}

class CompositeDocument<T> : DocumentItem where T : DocumentItem
{
private List<T> items = new List<T>();

public List<T> Items
{
get { return items; }
}
}

class Document : CompositeDocument<Document> { }

class Document1 : CompositeDocument<Document1> { }

Is that any better ?

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top