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.
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 );
}
}