Possible to use wildcards in generic types?

karnac

Member
Joined
Aug 25, 2008
Messages
6
Programming Experience
5-10
Greetings all,

This is my first post to this site. Please forgive me if this is the wrong forum.

I've been programming in Java for ages, but my current responsibilities mean that I have to learn Visual Basic .Net (no I can't use C#).

There's one thing that I can't figure out whether it's even possible. Below is a snip of Java. What that does is provide a type safe place to compare two values where it's possible one value might be null. Note that the code below will complain with a compile time error if e.g. someone tries to compare an Integer and a String. Rather than put all the if/else switching everyplace a null/nothing field is valid, I can simply use this as a comparator. Is it possible to do a similar trick with VB.net generic types? BTW, I'm currently using VB 2005 Express while I'm waiting for the full version of Visual Studio 2008 - if version makes a difference.

VB.NET:
public class NullComparator {
    public static <T extends Comparable<T>> int compareNullBefore( T l, T r ){
        if ( l == r ) return 0;
        if ( l == null ) {
            if ( r == null ) {
                return 0;
            } else {
                return -1;
            }
        } else {
            if ( r == null ) {
                return 1;
            }
        }
        return l.compareTo( r );
    }
}
 
I don't think you need it, in VB.Net you compare reference types with Is operator which supports null reference (Nothing) for both operands. Value types is compared with = operator and always has default values when not assigned.

Comparing objects of different types is not a problem because two references simply compares False, values of different value types may have same value (0D = 0.0F) or sometimes you get compiler error if you don't convert properly.
 
Well, here's what I did - but I can't help but feel that there's a less "ported from something else by a noob" way to do this:

VB.NET:
Public Shared Function CompareNothingBefore(ByVal l As Object, ByVal r As Object) As Integer
  If l Is Nothing Then
    If r Is Nothing Then
        Return 0
    Else
        Return -1
    End If
  Else
    If r Is Nothing Then
        Return 1
    End If
  End If
  Return l.CompareTo(r)
End Function

That passes my unit tests, but honestly, I don't yet know enough about VB.net to know if my tests are adequate. Oh, and thanks for the response...
 
Here is an example of a class that implements IComparable(Of T), as you see CompareTo is an instance method and you only need to check if the other is Nothing. IComparable is used for default sorting of type instances.
VB.NET:
Class Something
    Implements IComparable(Of Something)

    Public x As Integer '#property#

    Public Function CompareTo(ByVal other As Something) As Integer Implements System.IComparable(Of Something).CompareTo
        If other Is Nothing Then
            Return 1
        Else            
            Return Me.x.CompareTo(other.x)
        End If
    End Function
End Class
Here is an example of a class that implements IComparer(Of T), the example class is made generic with contraint, Class(Of Tparam As Tcontraint). IComparer is used to provide various external sorting.
VB.NET:
Class NothingComparer(Of typ As IComparable(Of typ))
    Implements IComparer(Of typ)

    Public Function Compare(ByVal x As typ, ByVal y As typ) As Integer Implements System.Collections.Generic.IComparer(Of typ).Compare
        If x Is Nothing AndAlso y Is Nothing Then
            Return 0
        ElseIf x Is Nothing Then
            Return -1
        ElseIf y Is Nothing Then
            Return 1
        Else
            Return x.CompareTo(y) 'use typ IComparable
        End If
    End Function
End Class
Usage could be like this:
VB.NET:
Dim l As New List(Of Something)
'...
l.Sort(New NothingComparer(Of Something))
When using the NothingComparer the need for Something to compare 'other' with Nothing is eliminated.
 
Back
Top