Thursday, November 27, 2003 8:41 PM Jan Tielens

Advanced Generics in VB.NET: More than one Constraint

In one of my previous posts I tried generics both in VB.NET and C#, it turned out both languages support generics the same way. Ofcourse there is the syntax difference between them, and at this time C# has Intellisense that supports generics better, but I'm pretty sure the VB.NET team will catch up. Tonight I explored generics a little bit more; it's possible to add constraints to the generic type. By doing so, you are sure only instances can be created for the generic type, that support for example an interface you want it to. Another possibility is a constraint so the generic types must inherit from a specific base type. Let's say you have a base Entity class, from which your business entity classes inherit from, and a Customer entity class:
Public Class Entity
    Private _id As Long

    Public Property ID() As Long
        Get
            Return _id
        End Get
        Set(ByVal Value As Long)
            _id = Value
        End Set
    End Property
End Class

Public Class Customer
    Inherits Entity
    Private _name As String

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal Value As String)
            _name = Value
        End Set
    End Property
End Class

The generic collection class could look like this:
Public Class MyCollection(Of itemType As Entity)
    Inherits CollectionBase

    Default Public Property Item(ByVal index As Integer) As itemType
        Get
            Return CType(MyBase.InnerList(index), itemtype)
        End Get
        Set(ByVal Value As itemType)
            MyBase.InnerList(index) = Value
        End Set
    End Property

    Public Function Add(ByVal item As itemType) As Integer
        Return MyBase.InnerList.Add(item)
    End Function
End Class

Using the generic MyCollection class goes like this:
Dim c As New MyCollection(Of Customer)

So far pretty simple! The fun begins when you want to add multiple constraints. I searched the internet for some examples, but I couldn't find an example (I searched only a minute or so, yes I'm lazy). Luckily Yves was online and he was following a session about generics at U2U, so I asked him if this was possible. His answer was positive (cool!), but he saw this only in C#. So strengthened by the knowledge that this was possible in C#, I searched for the syntax in VB.NET. I tried a few possibilities, and it turned out that you have to use the & character to add more than one constraint. Extending the sample from above with an additional constraint, for example the generic type needs to implement the IComparable interface:
Public Class MyCollection(Of itemType As Entity & IComparable)
...
End Class

By the way, the C# syntax is:
public class List<ItemType> where ItemType : IComparable<ItemType>

Since I couldn't find any official documentation, I cannot garantuee that the syntax showed in this post is correct. So if anyone of MS reads this (by accident probably :-), it would be great if you can confirm this or give the correct syntax.

Comments

# re: Advanced Generics in VB.NET: More than one Constraint

Tuesday, December 02, 2003 12:59 PM by Maxim V. Karpov

Jan,
It is very useful samples. Where did you find syntax for it? I am struggling through converting C# sytnax of Generics to VB.NET.

Thanks, Maxim

# re: Advanced Generics in VB.NET: More than one Constraint

Tuesday, December 02, 2003 2:26 PM by Jan

Hi Maxim

The C# syntax I found on the net (e.g. from the MSDN site), but the VB.NET syntax I had to "discover" myself. If you have problems, let me know and I'll see if I can help you out!

Jan

# re: Advanced Generics in VB.NET: More than one Constraint

Wednesday, December 03, 2003 3:52 PM by Paul Vick

Actually, the syntax for multiple constraints will be changing, the one in the PDC build was just a preliminary syntax. The new syntax will look more like "Class MyCollection(Of T As {Entity, IComparable})". I really need to get to blogging about this... :-)

# re: Advanced Generics in VB.NET: More than one Constraint

Wednesday, December 03, 2003 3:55 PM by Jan

Hi Paul, thanks for the information!

# re: Advanced Generics in VB.NET: More than one Constraint

Thursday, December 04, 2003 10:57 PM by Maxim V. Karpov

Paul,
I think you should give us some good sample code on this topic. I think It is one of the great feature we are going to have with new release.

By the way new syntax looks confusing {} do not really makes sense for VB developers

# re: Advanced Generics in VB.NET: More than one Constraint

Friday, January 02, 2004 8:13 AM by Alex Thissen

Hi Jan,

There is a lot of information to be found in the C# language specification 2.0 draft. It is located at:
http://download.microsoft.com/download/8/1/6/81682478-4018-48fe-9e5e-f87a44af3db9/SpecificationVer2.doc

As you were not sure whether the syntax of C# is correct, let me assure you it is. The <itemType> you used after IComparable is used only when the interface (or class) itself is also a Generic. Otherwise, you would not include it.

BTW, did you also know that you can add constructor constraints as well. The following sample requires the V type to have a default parameterless constructor. In C# multiple constraints are added to a type in a comma separated list.

public class SomeDictionary<K,V>
where K: IPersistable, IComparable<K>
where V: MyBaseClass, new()
{ // ... }

Hope this helps.

# re: Advanced Generics in VB.NET: More than one Constraint

Friday, January 02, 2004 10:45 AM by Jan Tielens

Thx Alex!

# Multiple Generic Constraints in VB.NET

Friday, June 11, 2004 5:51 AM by TrackBack

I was trying to figure out the syntax for something similar to

# re: Advanced Generics in VB.NET: More than one Constraint

Saturday, October 28, 2006 4:38 PM by eli

hi

is it possible that a function in VB.net return more than one value?

# re: Advanced Generics in VB.NET: More than one Constraint

Wednesday, August 08, 2007 10:11 PM by Fernando Regueiro

How is this Advanced Generics? How more basic can it get?

It is entries like this in blogs that mess up search results.