help with generics

G

Guest

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
 
J

Joanna Carter [TeamB]

"mopicus" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| 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
 
G

Guest

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ó:
 
J

Joanna Carter [TeamB]

"mopicus" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| 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
 
M

Marc Gravell

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;
}
}
 
G

Guest

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
 
B

Ben Voigt

mopicus said:
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" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top