473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with generics

hello all, i have two base classes, one inherits from bindinglist<>
i need baseobject contains a generic reference to collection that contains it

class baseobject<T> : where T:baseobjectcollection<baseobject<T>>

class baseobjectcollection<T> : BindingList<T> where
T:baseobject<baseobjectcollection<T>>

i'm getting compile error with this. i'm not sure if it's a syntax error or
what i'm trying it's impossible, without generics of course not problem

class baseobject
{
private baseobjectcollection m_colRef;
}

thank you
Jun 26 '06 #1
6 1330
"mopicus" <mo*****@discussions.microsoft.com> a écrit dans le message de
news: A5**********************************@microsoft.com...

| hello all, i have two base classes, one inherits from bindinglist<>
| i need baseobject contains a generic reference to collection that contains
it
|
| class baseobject<T> : where T:baseobjectcollection<baseobject<T>>
|
| class baseobjectcollection<T> : BindingList<T> where
| T:baseobject<baseobjectcollection<T>>
|
| i'm getting compile error with this. i'm not sure if it's a syntax error
or
| what i'm trying it's impossible, without generics of course not problem
|
| class baseobject
| {
| private baseobjectcollection m_colRef;
| }

Your attempt looks way too complicated !!! :-)

| class baseobject<T> : where T:baseobjectcollection<baseobject<T>>

You don't want a base object to be bound to a collection, you want it to
contain a collection

class BaseObject
{
private BindingList<BaseObject> m_colRef;

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 26 '06 #2
thankyou but does not solve my problem
public class BaseObject
{
public BaseObjectCollection<BaseObject> CollectionRef;
}

public class BaseObjectCollection<T> : BindingList<T>
{
}

public class EmployeeCollection : BaseObjectCollection<Employee>
{
}
public class Employee : BaseObject
{

public Employee()
{
EmployeeCollection emp = this.CollectionRef as EmployeeCollection;
// compile ERROR : invalid cast
}

}
i think this example best illustrates what i want
thankyou anyways


"Joanna Carter [TeamB]" escribió:
"mopicus" <mo*****@discussions.microsoft.com> a écrit dans le message de
news: A5**********************************@microsoft.com...

| hello all, i have two base classes, one inherits from bindinglist<>
| i need baseobject contains a generic reference to collection that contains
it
|
| class baseobject<T> : where T:baseobjectcollection<baseobject<T>>
|
| class baseobjectcollection<T> : BindingList<T> where
| T:baseobject<baseobjectcollection<T>>
|
| i'm getting compile error with this. i'm not sure if it's a syntax error
or
| what i'm trying it's impossible, without generics of course not problem
|
| class baseobject
| {
| private baseobjectcollection m_colRef;
| }

Your attempt looks way too complicated !!! :-)

| class baseobject<T> : where T:baseobjectcollection<baseobject<T>>

You don't want a base object to be bound to a collection, you want it to
contain a collection

class BaseObject
{
private BindingList<BaseObject> m_colRef;

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Jun 26 '06 #3
"mopicus" <mo*****@discussions.microsoft.com> a écrit dans le message de
news: 3E**********************************@microsoft.com...

| public class BaseObject
| {
| public BaseObjectCollection<BaseObject> CollectionRef;
| }
|
| public class BaseObjectCollection<T> : BindingList<T>
| {
| }
|
| public class EmployeeCollection : BaseObjectCollection<Employee>
| {
| }
|
|
| public class Employee : BaseObject
| {
|
| public Employee()
| {
| EmployeeCollection emp = this.CollectionRef as EmployeeCollection;
| // compile ERROR : invalid cast
| }
|
| }
|
|
| i think this example best illustrates what i want
| thankyou anyways

Sorry, but you are still over-engineering here and you are going to get
bitten by the covariance problems when you try to convert a
BindingList<BaseObject> to BindingList<Employee>; this simply won't work.
See many previous posts on covariance.

You don't need to subclass these generic classes, simply use them as they
are.

public class BaseObject
{

}

public class Employee : BaseObject
{

}

public class Company : BaseObject
{
private BindingList<Employee> employees = new BindingList<Employee>();

...
}

And I don't see why you would want to include a list of Employees in the
Employee class; this would mean that every single employee would also have a
list of *all* employees inside them ???

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 26 '06 #4
If I understand what you are trying to do, then you can do this with nested
classes (below); however, I should warn you that it will all go a bit freak
when you subclass - i.e. you can't really subclass the inner-class; anything
that inherits from BaseItem<T> will be stuck with the same collection
class - you can't really alter it.

Either class can be the inner; in this context I guess the collection is
less likely to change, so could sit on the inside? Personally, I'm not sure
that generics /nesting is the right answer here... perhaps the BaseItem
could have a BaseCollection reference, and then (on a class-by-class basis)
privide a "new OwnerCollection" implementation that casts this to the
correct collection type?

Marc

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

public class BaseItem<T> {
public T TypedValue; // only public for demo
public BaseCollection OwnerCollection; // only public for demo

public class BaseCollection : BindingList<BaseItem<T>> {
readonly Collection<BaseItem<T>> items = new
Collection<BaseItem<T>>();
}
}
public class SomeClass : BaseItem<int> { }

static class Program {
static void Main() {
SomeClass.BaseCollection collection = new
SomeClass.BaseCollection();
SomeClass item = new SomeClass();
item.TypedValue = 500;
collection.Add(item);
item.OwnerCollection = collection;
}
}
Jun 26 '06 #5
thank you both of you

i need inheritance from both baseitem and baseitemcollection. so I think
best option is take away collection reference from baseitem object.

anyways, it's a reference to the collection, not the collection itself,
isn't it? so there's nothing bad with it about memory usage, i think

i do a lot of work inside an object, removing itself from it's owner
collection, counting items, accessing others ... but i think I can try
another approach without the collection reference.

anyways nested class solution is a good example

again, lot of thanks

Jun 26 '06 #6
"mopicus" <mo*****@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
thankyou but does not solve my problem
public class BaseObject
{
public BaseObjectCollection<BaseObject> CollectionRef;
}
public class BaseObjectCollection<T> : BindingList<T>
{
}
perhaps try instead:

public class BaseObject<T> where T : BaseObject

public class BaseObjectCollection<T> : BindingList<T> where T :
BaseObject<T>

public class Employee : BaseObject<Employee>

You'll have to decide in advance how specific your collections are...

You can get away with Employee, Manager, Salesperson all in a
EmployeeCollection, but then you won't be able to have a ManagerCollection
containing only Manager objects...

Because if Manager.CollectionRef had type ManagerCollection, then you could
write
foreach (Manager m in CollectionRef)
but if it was really an EmployeeCollection, then some of the employees
aren't really managers.

If Manager.CollectionRef has type EmployeeCollections, then you can't ever
store a Manager in a ManagerCollection, because although
foreach (Employee e in CollectionRef)
would be ok on a ManagerCollection,
CollectionRef.Add(new SalesPerson())
would fail.

public class EmployeeCollection : BaseObjectCollection<Employee>
{
}
public class Employee : BaseObject
{

public Employee()
{
EmployeeCollection emp = this.CollectionRef as EmployeeCollection;
// compile ERROR : invalid cast
}

}
i think this example best illustrates what i want
thankyou anyways


"Joanna Carter [TeamB]" escribió:
"mopicus" <mo*****@discussions.microsoft.com> a écrit dans le message de
news: A5**********************************@microsoft.com...

| hello all, i have two base classes, one inherits from bindinglist<>
| i need baseobject contains a generic reference to collection that
contains
it
|
| class baseobject<T> : where T:baseobjectcollection<baseobject<T>>
|
| class baseobjectcollection<T> : BindingList<T> where
| T:baseobject<baseobjectcollection<T>>
|
| i'm getting compile error with this. i'm not sure if it's a syntax
error
or
| what i'm trying it's impossible, without generics of course not problem
|
| class baseobject
| {
| private baseobjectcollection m_colRef;
| }

Your attempt looks way too complicated !!! :-)

| class baseobject<T> : where T:baseobjectcollection<baseobject<T>>

You don't want a base object to be bound to a collection, you want it to
contain a collection

class BaseObject
{
private BindingList<BaseObject> m_colRef;

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Jun 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
2440
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
3081
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
12
2715
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
5
2909
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
9
5948
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
2418
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
3240
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
3792
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
1686
by: info | last post by:
I am trying to create an MDI Application. I am opening the windows OK (MDI Children). But I don't think I am doing it correctly. Basically, I declare the forms on the main form (Parent). frm1...
2
9996
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
7205
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,...
0
7287
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,...
0
5594
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,...
1
5022
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...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
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 ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
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...

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.