473,796 Members | 2,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(BizCo llection<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 1770
"dcew" <dc**@discussio ns.microsoft.co m> a écrit dans le message de news:
3F************* *************** **...icrosof t.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(BizCo llection<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>(Bi zCollection<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**@discussio ns.microsoft.co m> a écrit dans le message de news:
3F************* *************** **...icrosof t.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(BizCo llection<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>(Bi zCollection<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
1836
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 was to not have to make a seperate Comparer class for every field of my object that I wanted to sort like the ASP.net TimeTracker starter kit did. My Code is below. Basically to use it all I have to do is to Create the GenericComparer object,...
2
4698
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 be passed any collection in order to populate a control based on the objects in the collection. I figure I would have to pass the type of object into the function too in order to be able to cast each item of the collection, plus, say, a string array...
8
3284
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* (not just instantiate at runtime) a generic collection whose element-type is a generic class too. But I don't want to declare what the element-type's generic parameters are.
8
9459
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 forward along the sign -up process. I am currently trying to use a Session object to store all these details and get them back out of session as each step is selected. My model was a HashTable of NameValueCollection, so I could go to the hash...
3
2262
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) The parent form have a custom control, which have a public sub looking
5
1869
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 which is a reference type, is there a way to get Collection.ContainsKey(RefKey) and Collection to work with different instances of equal keys? For example, I have defined a class of "codon" and I have made a dictionary private...
5
2169
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
10153
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 seen erratic results. I am aware of one known defect where user classes do not show up in the list of types on the Property/Settings page in the visual designer and I am wondering if I am encountering some other peculiar issue, or if there are...
2
4186
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 types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.