Storing Generic collection in a field

G

Guest

Here's what I'm trying to understand; how can you store a generic collection
in a variable/field?

If I have an abstract generic collection class as follows...

public abstract class BizCollection<T> : Collection<T> where T : BizBase
{
// Collection Implementation

override void InsertItem(int index, T item)
{
item.SetParent<BizCollection<T>>(this);
base.InsertItem(index, item);
}
}

And then I have an abstract base business object which has method exposed
which allows you to set the parent collection it belongs to as this...

public abstract class BizBase
{
private BizCollection<T> parent;

public void SetParent(BizCollection<T> parent)
{
this.parent = parent;
}
}

You’ll notice the logic is that when an Insert is performed on the
collection it tries to call the BizBase objects SetParent() method – but
obviously the code above won’t compile, as you can't declare "private
BizCollection<T> parent" - so if I don't know what the Type is going to be -
how can I store the collection in a field?

Hope that makes sense,
Thanks in advance!
 
J

Joanna Carter [TeamB]

"dcew" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Here's what I'm trying to understand; how can you store a generic
collection
| in a variable/field?
|
| If I have an abstract generic collection class as follows...
|
| public abstract class BizCollection<T> : Collection<T> where T : BizBase
| {
| // Collection Implementation
|
| override void InsertItem(int index, T item)
| {
| item.SetParent<BizCollection<T>>(this);
| base.InsertItem(index, item);
| }
| }
|
| And then I have an abstract base business object which has method exposed
| which allows you to set the parent collection it belongs to as this...
|
| public abstract class BizBase
| {
| private BizCollection<T> parent;
|
| public void SetParent(BizCollection<T> parent)
| {
| this.parent = parent;
| }
| }
|
| You'll notice the logic is that when an Insert is performed on the
| collection it tries to call the BizBase objects SetParent() method - but
| obviously the code above won't compile, as you can't declare "private
| BizCollection<T> parent" - so if I don't know what the Type is going to
be -
| how can I store the collection in a field?

public interface IBizCollection
{
}

public abstract class BizCollection<T> : Collection<T>, IBizCollection where
T : BizBase
{
// Collection Implementation

override void InsertItem(int index, T item)
{
item.SetParent<BizCollection<T>>(this);
base.InsertItem(index, item);
}
}

public abstract class BizBase
{
private IBizCollection parent;

public void SetParent<T>(BizCollection<T> parent)
{
this.parent = (IBizCollection) parent;
}

public BizCollection<T> GetParent<T>()
{
return (BizCollection<T>) parent;
}
}

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

Top