473,440 Members | 1,787 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,440 software developers and data experts.

Generics Question.


Iam wondering if it is possible to narrow down the type definition while
specifying Generics,

for example i want to create my collection class for a particular Type
T, but i want this Type T to be of interface IEntity (something i implement)

so my collection would be like

Public Class MyGenericCollection(Of T)

public sub Save(myObj as T)

end sub
end class
is it possible...

TIA
Nov 23 '05 #1
3 976
Public Class MyGenericCollection(Of T As IEntity)
Public Class MyGenericCollection(Of T As {IEntity, ISomeOtherEntity})
etc.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

"Ashish" wrote:

Iam wondering if it is possible to narrow down the type definition while
specifying Generics,

for example i want to create my collection class for a particular Type
T, but i want this Type T to be of interface IEntity (something i implement)

so my collection would be like

Public Class MyGenericCollection(Of T)

public sub Save(myObj as T)

end sub
end class
is it possible...

TIA

Nov 23 '05 #2
Thanks David,
that was exactly what i was looking for :)
David Anton wrote:
Public Class MyGenericCollection(Of T As IEntity)
Public Class MyGenericCollection(Of T As {IEntity, ISomeOtherEntity})
etc.

Nov 23 '05 #3
Ashish,
In addition to the interface constraint that David showed.

There are also Class, Structure, New, and base class constraints:

See "Type of Constraints" at:
http://msdn2.microsoft.com/en-us/library/w256ka79.aspx

Also see:
http://msdn2.microsoft.com/en-us/library/t4xaz66w

Public Class MyGenericCollection(Of T As IEntity)
Public Class MyGenericCollection(Of T As {IEntity, Class})
Public Class MyGenericCollection(Of T As {IEntity, Structure})
Public Class MyGenericCollection(Of T As {IEntity, New})
Public Class MyGenericCollection(Of T As {IEntity, Control})

(where Control above is System.Windows.Forms.Control
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Ashish" <as*****@thisisjunk.com> wrote in message
news:Oq**************@TK2MSFTNGP15.phx.gbl...
|
| Iam wondering if it is possible to narrow down the type definition while
| specifying Generics,
|
| for example i want to create my collection class for a particular Type
| T, but i want this Type T to be of interface IEntity (something i
implement)
|
| so my collection would be like
|
| Public Class MyGenericCollection(Of T)
|
| public sub Save(myObj as T)
|
| end sub
| end class
|
|
| is it possible...
|
| TIA
|
|
Nov 23 '05 #4

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

Similar topics

3
by: Fred | last post by:
Is there a way to use generics with vs.net 2003?? I was searching around and I saw gyro but does it works with vs.net 2003?? if it does how am I suppouse to install it. If it doesnt is there...
27
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...
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...
13
by: Luc Vaillant | last post by:
I try to compare to values of generic value type T in a generic class as follow: public class C<T> where T : struct { private T value1; private T value2; C(T value1, T value2) {
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
7
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
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...
0
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...
0
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,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.