Generics Question.

A

Ashish

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
 
G

Guest

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
 
J

Jay B. Harlow [MVP - Outlook]

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


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

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