473,685 Members | 3,008 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 1762
"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
1822
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
4695
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
3280
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
9451
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
2256
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
1863
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
2162
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
10134
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
4179
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
8570
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8925
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8758
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
8773
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
7584
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 projectplanning, coding, testing, and deploymentwithout 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...
1
6431
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4299
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...
2
2198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.