473,763 Members | 5,610 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:baseobjectcol lection<baseobj ect<T>>

class baseobjectcolle ction<T> : BindingList<T> where
T:baseobject<ba seobjectcollect ion<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 baseobjectcolle ction m_colRef;
}

thank you
Jun 26 '06 #1
6 1343
"mopicus" <mo*****@discus sions.microsoft .com> a écrit dans le message de
news: A5************* *************** **...icrosof t.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:baseobjectcol lection<baseobj ect<T>>
|
| class baseobjectcolle ction<T> : BindingList<T> where
| T:baseobject<ba seobjectcollect ion<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 baseobjectcolle ction m_colRef;
| }

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

| class baseobject<T> : where T:baseobjectcol lection<baseobj ect<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<Bas eObject> m_colRef;

...
}

Joanna

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

public class BaseObjectColle ction<T> : BindingList<T>
{
}

public class EmployeeCollect ion : BaseObjectColle ction<Employee>
{
}
public class Employee : BaseObject
{

public Employee()
{
EmployeeCollect ion emp = this.Collection Ref as EmployeeCollect ion;
// compile ERROR : invalid cast
}

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


"Joanna Carter [TeamB]" escribió:
"mopicus" <mo*****@discus sions.microsoft .com> a écrit dans le message de
news: A5************* *************** **...icrosof t.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:baseobjectcol lection<baseobj ect<T>>
|
| class baseobjectcolle ction<T> : BindingList<T> where
| T:baseobject<ba seobjectcollect ion<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 baseobjectcolle ction m_colRef;
| }

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

| class baseobject<T> : where T:baseobjectcol lection<baseobj ect<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<Bas eObject> m_colRef;

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

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

| public class BaseObject
| {
| public BaseObjectColle ction<BaseObjec t> CollectionRef;
| }
|
| public class BaseObjectColle ction<T> : BindingList<T>
| {
| }
|
| public class EmployeeCollect ion : BaseObjectColle ction<Employee>
| {
| }
|
|
| public class Employee : BaseObject
| {
|
| public Employee()
| {
| EmployeeCollect ion emp = this.Collection Ref as EmployeeCollect ion;
| // 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<Bas eObject> to BindingList<Emp loyee>; 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<Emp loyee> employees = new BindingList<Emp loyee>();

...
}

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.Collecti ons.ObjectModel ;
using System.Componen tModel;

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

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

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

i need inheritance from both baseitem and baseitemcollect ion. 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*****@discus sions.microsoft .com> wrote in message
news:3E******** *************** ***********@mic rosoft.com...
thankyou but does not solve my problem
public class BaseObject
{
public BaseObjectColle ction<BaseObjec t> CollectionRef;
}
public class BaseObjectColle ction<T> : BindingList<T>
{
}
perhaps try instead:

public class BaseObject<T> where T : BaseObject

public class BaseObjectColle ction<T> : BindingList<T> where T :
BaseObject<T>

public class Employee : BaseObject<Empl oyee>

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

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

Because if Manager.Collect ionRef had type ManagerCollecti on, then you could
write
foreach (Manager m in CollectionRef)
but if it was really an EmployeeCollect ion, then some of the employees
aren't really managers.

If Manager.Collect ionRef has type EmployeeCollect ions, then you can't ever
store a Manager in a ManagerCollecti on, because although
foreach (Employee e in CollectionRef)
would be ok on a ManagerCollecti on,
CollectionRef.A dd(new SalesPerson())
would fail.

public class EmployeeCollect ion : BaseObjectColle ction<Employee>
{
}
public class Employee : BaseObject
{

public Employee()
{
EmployeeCollect ion emp = this.Collection Ref as EmployeeCollect ion;
// compile ERROR : invalid cast
}

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


"Joanna Carter [TeamB]" escribió:
"mopicus" <mo*****@discus sions.microsoft .com> a écrit dans le message de
news: A5************* *************** **...icrosof t.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:baseobjectcol lection<baseobj ect<T>>
|
| class baseobjectcolle ction<T> : BindingList<T> where
| T:baseobject<ba seobjectcollect ion<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 baseobjectcolle ction m_colRef;
| }

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

| class baseobject<T> : where T:baseobjectcol lection<baseobj ect<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<Bas eObject> 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
2462
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 still no can do... Any info would be great!
2
3104
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
12
2744
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 it defines the language more completely. Great to use.
5
2921
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 an "AppleBasket" which is a class that contains a collection of apples. So, some code: class Fruit{ }
9
5986
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 Experience Only, Please. ...
1
2438
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 "ThrowInConstructor" and "ThrowInFoo". First one throws "MyOwnException" in constructor, second one in "Foo()" method. There is a "GenericCatch" generics class able to accept "ThrowInConstructor" and "ThrowInFoo" as type parameter "<T>". There are two methods in...
7
3257
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 System.Collections.Generic namespace. Literature I have read on this ties generics in with collections, hence articulate their examples as such. That's fine, I understand what is being said. My question is more towards the application and implementation...
13
3837
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
10
1707
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 f1; frm2 f2; etc
2
10043
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 is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
9563
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
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
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
9937
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
9822
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
8821
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...
1
7366
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
6642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
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

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.