473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generics, constrains and inheritance

Joe
Hi All!

Here's the problem I'm having. I want to create a generic class that
inherits from CollectionBase and implements IBindingList. As part of
IBindingList I have to implement AddNew(). Since this is a generic class the
new throws an error "Cannot create an instance of the variable type 'T'
because it does not have the new constraint".

How do I add a constraint to my class and be able to keep the CollectionBase
and IBindingList?

When I add the where <T>: it means that I want the class T to implement
CollectionBase and IBindingList.

Is it possible to do what I want?

Thanks,
Joe
Oct 20 '08 #1
6 2330
Joe
I believe I have my answer. I need to declare my class like this:

class MyClass<T: CollectionBase, IBindingList where T: new ()
{
....
}

"Joe" <jb*******@noemail.noemailwrote in message
news:e0**************@TK2MSFTNGP05.phx.gbl...
Hi All!

Here's the problem I'm having. I want to create a generic class that
inherits from CollectionBase and implements IBindingList. As part of
IBindingList I have to implement AddNew(). Since this is a generic class
the new throws an error "Cannot create an instance of the variable type
'T' because it does not have the new constraint".

How do I add a constraint to my class and be able to keep the
CollectionBase and IBindingList?

When I add the where <T>: it means that I want the class T to implement
CollectionBase and IBindingList.

Is it possible to do what I want?

Thanks,
Joe


Oct 20 '08 #2
On Oct 20, 10:10*am, "Joe" <jbassk...@noemail.noemailwrote:
I believe I have my answer. I need to declare my class like this:

class MyClass<T: CollectionBase, IBindingList where T: new ()
{
* * ....

}
I don't think compiler knows what t where T: new () means...
Oct 20 '08 #3
On Mon, 20 Oct 2008 12:57:26 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
On Oct 20, 10:10Â*am, "Joe" <jbassk...@noemail.noemailwrote:
>I believe I have my answer. I need to declare my class like this:

class MyClass<T: CollectionBase, IBindingList where T: new ()
{
Â* Â* ....

}
I don't think compiler knows what t where T: new () means...
Why don't you think that?

http://msdn.microsoft.com/en-us/library/sd2w2ew5.aspx
Oct 20 '08 #4
On Oct 20, 4:18*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 20 Oct 2008 12:57:26 -0700, puzzlecracker <ironsel2...@gmail.com>*
wrote:
On Oct 20, 10:10*am, "Joe" <jbassk...@noemail.noemailwrote:
I believe I have my answer. I need to declare my class like this:
class MyClass<T: CollectionBase, IBindingList where T: new ()
{
* * ....
}
I don't think compiler knows what t where T: new () means...

Why don't you think that?

http://msdn.microsoft.com/en-us/library/sd2w2ew5.aspx
I should have rtfm'ed. I didn't know that you can constrain types
based on different constructor types... learn something new every
day...
Oct 20 '08 #5
puzzlecracker wrote:
On Oct 20, 4:18 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
>On Mon, 20 Oct 2008 12:57:26 -0700, puzzlecracker
<ironsel2...@gmail.comwrote:
>>On Oct 20, 10:10 am, "Joe" <jbassk...@noemail.noemailwrote:
I believe I have my answer. I need to declare my class like this:
>>>class MyClass<T: CollectionBase, IBindingList where T: new ()
{
....
>>>}
I don't think compiler knows what t where T: new () means...

Why don't you think that?

http://msdn.microsoft.com/en-us/library/sd2w2ew5.aspx

I should have rtfm'ed. I didn't know that you can constrain types
based on different constructor types... learn something new every
day...
You can't. At least not the part about different constructor types. There
is a constraint for "public parameterless constructor" and that's it, sadly.
Oct 20 '08 #6
On Oct 20, 10:22*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
I should have rtfm'ed. I didn't know that you can constrain types
based on different constructor types... learn something new every
day...

You can't. *At least not the part about different constructor types. *There
is a constraint for "public parameterless constructor" and that's it, sadly.
For a suggestion of how an alternative might work, see
http://msmvps.com/blogs/jon_skeet/ar...nterfaces.aspx

Jon
Oct 21 '08 #7

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

Similar topics

3
by: Eddie | last post by:
Hi, I've recently read the articles on generics in the .net framework (the next version) and just wanted to check a few points.. As I understand, generic class instances while being strongly...
3
by: Pierre Y. | last post by:
Hi, I've just read that article : http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp I'm asking something. Why are generics constraints not been implemented using...
6
by: Thomas Tomiczek | last post by:
Ok, working my way through a complex library conversion to .NET 2.0 and C# 2.0 with generics I am totally stuck on one thing -if anyone sees the issue I would be more than glad. The situation is...
1
by: Shmulik | last post by:
Is there a way to constrain a Generics class to only accept specific value types, for instance, lets say I want to create a Histogram class that could accept the types int, float, double, etc.,...
5
by: PeterLawrance | last post by:
Hi, I would expect that inheritance through the parameters of a generic class would be fine however I get errors as below in the following code. Public Class Form1 Sub test() Dim xxx As...
12
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
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...
8
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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
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...
0
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...

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.