 Thursday, February 23, 2006
The longer I work with generics, the more I like them, and I continue to discover new ways to simplify my code by using them. More specifically, I like the ability to write type-safe code that is also more concise, efficient, and (above all) readable, because I don't have to use tons of CType and DirectCast operators. Today I gathered the generic methods I use more frequently in the following module. They are tiny and simple, yet they save me a lot of time and code.
Module GenericFunctions
' Swap two variables Public Sub Swap(Of T)(ByRef var1 As T, ByRef var2 As T) Dim tmp As T = var1 var1 = var2 var2 = tmp End Sub
' Type-safe version of the IIF function ' returns valueOnTrue if expression is True, else returns valueOnFalse Public Function IIf(Of T)(ByVal expression As Boolean, ByVal valueOnTrue As T, ByVal valueOnFalse As T) As T If expression Then Return valueOnTrue Else Return valueOnFalse End If End Function
' Type-safe version of the Choose function ' returns the N-th element of a list of values, or the default value for T if index ' is less than 0 or higher than the number of values Public Function Choose(Of T)(ByVal index As Integer, ByVal values() As T) As T If index >= 0 AndAlso index < values.Length Then Return values(index) Else Return Nothing End If End Function
' Return an array of the specified type Public Function NewArray(Of T)(ByVal ParamArray values() As T) As T() Return values End Function
' Return the min value of a list Public Function Min(Of T As IComparable)(ByVal firstValue As T, ByVal ParamArray values() As T) As T Dim result As T = firstValue For Each value As T In values If result.CompareTo(value) > 0 Then result = value Next Return result End Function
' Return the max value of a list Public Function Max(Of T As IComparable)(ByVal firstValue As T, ByVal ParamArray values() As T) As T Dim result As T = firstValue For Each value As T In values If result.CompareTo(value) < 0 Then result = value Next Return result End Function
' Return True if a value is in specific range Public Function InRange(Of T As IComparable)(ByVal testValue As T, ByVal minValue As T, ByVal maxValue As T) As Boolean Return testValue.CompareTo(minValue) >= 0 AndAlso testValue.CompareTo(maxValue) <= 0 End Function
' Retrieve a dictionary element of a given type, or the provided default value if the element isn't found ' (two overloads) Public Function GetDictionaryValue(Of TKey, TValue)(ByVal dict As Hashtable, ByVal key As TKey, ByVal defaultValue As TValue) As TValue If dict.ContainsKey(key) Then Return CType(dict(key), TValue) Else Return defaultValue End If End Function
Public Function GetDictionaryValue(Of TKey, TValue)(ByVal dict As Dictionary(Of TKey, TValue), ByVal key As TKey, ByVal defaultValue As TValue) As TValue ' If the key is in the dictionary, the following statement stores the corresponding value ' in defaultValue, else it leave defaultValue unchanged dict.TryGetValue(key, defaultValue) Return defaultValue End Function
End Module
Most methods are self-explanatory. One of the most useful ones is NewArray, which lets you create an array and pass it on-the-fly to a method. Let's say the the DoSomething method takes an array of Integers. These are the options you have in VB2005:
' 1. Create the array first, than pass it Dim values() As Integer = {1, 2, 3, 4, 5} DoSomething(values)
' 2. Create the array on the fly using the nearly-undocumented syntax DoSomething(New Integer() {1, 2, 3, 4, 5})
I often use the second syntax, but I noticed that relatively few developers know it. My code is much more readable with the NewArray method
' 3. Use the NewArray generic function to create the array on the fly DoSomething(NewArray(1, 2, 3, 4, 5))
The NewArray method proves to be quite useful also to build For loops whose index can take any sequence of values:
' Test whether "number" is a prime number in the range 1-1000 For Each n As Integer In NewArray(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31) If (number Mod n) = 0 Then Console.Write("{0} is not prime", number): Exit For Next
|
|
Get RSS/Atom Feed
Search in the blog
Archive
| | Sun | Mon | Tue | Wed | Thu | Fri | Sat | | 28 | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 10 | 11 | 12 | 13 | | 14 | 15 | 16 | 17 | 18 | 19 | 20 | | 21 | 22 | 23 | 24 | 25 | 26 | 27 | | 28 | 29 | 30 | 31 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Categories
Powered by: newtelligence dasBlog 1.8.5223.1
|