473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 1742
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Frank Wisniewski | last post by:
Hi All, I have created my own generic comparer object which implements IComparer and uses reflection. I have tested it and it seem to work fine but I am worried about the way I did it. My goal...
2
by: Dave Thorens | last post by:
Hi. My VB6 application has several collections of custom classes, and each class has differing properties (ie. the classes are not necessarily related). I want to write a generic function which can...
8
by: Steven Cummings | last post by:
Hello, I've scoured this usenet group and didn't find anything specific to my problem, so hopefully this won't be a repeated question. I'm all but certain it's not. I would like to *declare*...
8
by: rgparkins | last post by:
Hi I am creating a sign-up process on a web site much like that of a wizard form. I have browsed many sites to look for examples of how to store the entry data, so that the user can go back and...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
5
by: Ethan Strauss | last post by:
Hi, I have just started using Generic Collections for .Net 2.0 and so far they are working very nicely for me. But, I do have a couple of questions. If I have a Generic collection which has a type...
5
by: sloan | last post by:
I've noticed alot of people tacking on a "T" for a generic abled version of an older class. Ex: 1.1 Code IDataStore
11
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I have worked with application settings in VS2005 and C# for awhile, but usually with standard types. I have been trying to store a custom container/class/type in an application setting and I have...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.