471,570 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Storing Generic collection in a field

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!
Jan 5 '06 #1
2 1671
"dcew" <dc**@discussions.microsoft.com> a crit dans le message de news:
3F**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 6 '06 #2
That seems to do the job!

Thanks!

"Joanna Carter [TeamB]" wrote:
"dcew" <dc**@discussions.microsoft.com> a écrit dans le message de news:
3F**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

Jan 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Frank Wisniewski | last post: by
8 posts views Thread by Steven Cummings | last post: by
8 posts views Thread by rgparkins | last post: by
3 posts views Thread by Seth Gecko | last post: by
5 posts views Thread by Ethan Strauss | last post: by
5 posts views Thread by sloan | last post: by
11 posts views Thread by =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.