473,781 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a constrained class that implements an interface (C#2.0)

I have this class:

public class ItemType
....

public class ProductType<T> where T: ItemType
....

Now I want to add an IDisposable interface to ProductType...

--
Thanks,
Nov 16 '05 #1
7 1255
public class ItemType
{
....
}

public class ProductType : ItemType, IDisposable
{
....
public void Dispose()
{
// Clean-up stuff here
}

}


"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:AD******** *************** ***********@mic rosoft.com...
I have this class:

public class ItemType
...

public class ProductType<T> where T: ItemType
...

Now I want to add an IDisposable interface to ProductType...

--
Thanks,

Nov 16 '05 #2
Thanks for the response, but that changes the implementation.

ProductType is not a type of "ItemType" but a container for "ItemType's ",
like List<T> but the "T" needs to be constrained to a specific type.

Example: I want to build a car, so I use car items, but I want to prevent
anyone from using bicycle items.

"Matt Redmond" wrote:
public class ItemType
{
....
}

public class ProductType : ItemType, IDisposable
{
....
public void Dispose()
{
// Clean-up stuff here
}

}


"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:AD******** *************** ***********@mic rosoft.com...
I have this class:

public class ItemType
...

public class ProductType<T> where T: ItemType
...

Now I want to add an IDisposable interface to ProductType...

--
Thanks,


Nov 16 '05 #3
Sorry I misread your code. I thought ProductType was an ItemType. I'm
still confused about the <T> syntax you have at the top there. Oh crap - is
this VB? I'm in C# mode, have never used VB.NET (used 6 tho).

In any case, just implement IDisposable and provide a Dispose() method. So
in VB.NET you'd just put 'Implements IDisposable' right after your class
declaration. The designer will stub out the Dispose() method for you.




"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:E0******** *************** ***********@mic rosoft.com...
Thanks for the response, but that changes the implementation.

ProductType is not a type of "ItemType" but a container for "ItemType's ",
like List<T> but the "T" needs to be constrained to a specific type.

Example: I want to build a car, so I use car items, but I want to prevent
anyone from using bicycle items.

"Matt Redmond" wrote:
public class ItemType
{
....
}

public class ProductType : ItemType, IDisposable
{
....
public void Dispose()
{
// Clean-up stuff here
}

}


"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:AD******** *************** ***********@mic rosoft.com...
I have this class:

public class ItemType
...

public class ProductType<T> where T: ItemType
...

Now I want to add an IDisposable interface to ProductType...

--
Thanks,


Nov 16 '05 #4

"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:AD******** *************** ***********@mic rosoft.com...
I have this class:

public class ItemType
...

public class ProductType<T> where T: ItemType
...
public class ProductType<T> : IDisposable, <whatever else you want to
inherit\impleme nt>
where T : ITemType

//...
Now I want to add an IDisposable interface to ProductType...

--
Thanks,

Nov 16 '05 #5
I think my brain is fried - been a long day. Now I see that this is C#,
not VB -should have read the title of the message. Further, I'm not
familiar with generics so I was confused. Don't you implement an interface
the same was as in C# 1.0 - like:

public class ProductType<T> : IDisposable

???


"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:E0******** *************** ***********@mic rosoft.com...
Thanks for the response, but that changes the implementation.

ProductType is not a type of "ItemType" but a container for "ItemType's ",
like List<T> but the "T" needs to be constrained to a specific type.

Example: I want to build a car, so I use car items, but I want to prevent
anyone from using bicycle items.

"Matt Redmond" wrote:
public class ItemType
{
....
}

public class ProductType : ItemType, IDisposable
{
....
public void Dispose()
{
// Clean-up stuff here
}

}


"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:AD******** *************** ***********@mic rosoft.com...
I have this class:

public class ItemType
...

public class ProductType<T> where T: ItemType
...

Now I want to add an IDisposable interface to ProductType...

--
Thanks,


Nov 16 '05 #6
Wow, that works... looks wierd though. I thought I had tried it before, but
I guess I didn't get it just right.

I just have to make sure that the constraint is the last thing in the list.

"Daniel O'Connell [C# MVP]" wrote:

"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:AD******** *************** ***********@mic rosoft.com...
I have this class:

public class ItemType
...

public class ProductType<T> where T: ItemType
...


public class ProductType<T> : IDisposable, <whatever else you want to
inherit\impleme nt>
where T : ITemType

//...

Now I want to add an IDisposable interface to ProductType...

--
Thanks,


Nov 16 '05 #7
Thanks for your response... it is not the interface that was the problem, it
was the constraint.

Here is a link to a good short explanation of what generics are all about

http://www.15seconds.com/issue/031024.htm
"Matt Redmond" wrote:
I think my brain is fried - been a long day. Now I see that this is C#,
not VB -should have read the title of the message. Further, I'm not
familiar with generics so I was confused. Don't you implement an interface
the same was as in C# 1.0 - like:

public class ProductType<T> : IDisposable

???


"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:E0******** *************** ***********@mic rosoft.com...
Thanks for the response, but that changes the implementation.

ProductType is not a type of "ItemType" but a container for "ItemType's ",
like List<T> but the "T" needs to be constrained to a specific type.

Example: I want to build a car, so I use car items, but I want to prevent
anyone from using bicycle items.

"Matt Redmond" wrote:
public class ItemType
{
....
}

public class ProductType : ItemType, IDisposable
{
....
public void Dispose()
{
// Clean-up stuff here
}

}


"Leicester B. Ford Jr." <Le*******@onli ne.nospam> wrote in message
news:AD******** *************** ***********@mic rosoft.com...
> I have this class:
>
> public class ItemType
> ...
>
> public class ProductType<T> where T: ItemType
> ...
>
> Now I want to add an IDisposable interface to ProductType...
>
> --
> Thanks,


Nov 16 '05 #8

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

Similar topics

3
23435
by: Hal Vaughan | last post by:
I'm creating a sequence of JPanels that will each, in turn, be added to (then removed from) a window (kind of like a wizard). I want these panels to not only extend the JPanel class, but to implement an interface. I have the interface defined in a separate class, like this: public interface IPanel { boolean onNext(); String nextPanel(); boolean hasCancel();
5
18110
by: randomblink | last post by:
Alright... Does anyone know if you can create a class that has a constructor? I would like to create a BUTTONMANAGER class that handles my buttons for me... I am doing this in Access for those who care. I can't seem to get class_initialize to have required arguments in the initializer. Of course I just assumed that the class_initialize was the constructor... Maybe not?
7
3679
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b; but it complains for the following lines
15
6750
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use javascript or vbscript it works. I will appreciate any help. I do the following (C#):
1
1778
by: Hasani \(remove nospam\) | last post by:
The way the system works is, you create a user control (ascx) that will be a template and must implement the interface IPageTemplate. You then create one or more user controls (ascx) that implement the IPageContent interface. A page (aspx) must then be created that loads (using Page.LoadControl) the page template, and the page content. The page content adds itself to the page template. The page template is then added to the aspx's control...
4
2858
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. '**************************************************************************** ' Issues '****************************************************************************
4
2361
by: SH | last post by:
I wish to create a program (really a Windows Service) that sits and waits for a client PC to communicate with it, but I can't come up with a good method of doing so. I want to have a service running on a server, waiting for clients to send it commands. The service would then do something based on the command and send the information back to the client. This is exactly what any database does. What is the best method for doing this? The...
19
1774
by: Fernando Cacciola | last post by:
I'm puzzled, Why and how _exactly_ is this: void Foo<T>(T v ) where T : Interface/Value/Class/class any better than this void Foo( Interface/Value/Class/object v )
13
11288
by: LordHog | last post by:
Hello all, I have a little application that needs to poll a device (CAN communications) every 10 to 15 ms otherwise the hardware buffer might overflow when there are message burst on the bus. I would implement an interrupt driven model, but the external device (which is connected via USB) does not support interrupts therefore the device needs to be polled at a specific interval. So my question is how can I implement a "deterministic"...
0
9474
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
10143
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
10076
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
9939
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
8964
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...
0
6729
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();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
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
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.