Global System Object extensions for easier conditionals (VB.net example)


/ Published in: VB.NET

Because there is no way to use object extenders in vb.net here is an example of how you can get similar functionality. See the C# example for further info.


Copy this code and paste it in your HTML
  1. Imports System
  2. Imports System.Text
  3. Imports Collections
  4.  
  5. ''' <summary>
  6. ''' System Object extensions
  7. ''' Originally these were intended to be extenders to the System.Object object,
  8. ''' but because in VB.net you cannot extend the System.Object object I changed these to global functions
  9. ''' C# allows this because it doesn't have the same late binding restrictions as VB
  10. ''' </summary>
  11. ''' <remarks>by Bryan Lyman</remarks>
  12. Module Extensions
  13.  
  14. ''' <summary>
  15. ''' Simple null object checker
  16. ''' </summary>
  17. ''' <returns>True if the object is Nothing or DBNull</returns>
  18. Public Function IsNullType(ByVal obj As Object) As Boolean
  19. Return (obj Is Nothing OrElse obj Is DBNull.Value)
  20. End Function
  21.  
  22. ''' <summary>
  23. ''' Simple signed value object checker
  24. ''' </summary>
  25. ''' <returns>True if the object type is signed</returns>
  26. Public Function IsSigned(ByVal obj As Object) As Boolean
  27. Return (TypeOf obj Is System.ValueType AndAlso (TypeOf obj Is Int32 OrElse TypeOf obj Is Int64 OrElse TypeOf obj Is Int16 OrElse TypeOf obj Is IntPtr OrElse TypeOf obj Is Decimal OrElse TypeOf obj Is SByte))
  28. End Function
  29.  
  30. ''' <summary>
  31. ''' Simple empty value object checker
  32. ''' </summary>
  33. ''' <returns>True if the object is empty and is not Nothing</returns>
  34. Public Function IsEmpty(ByVal obj As Object) As Boolean
  35. Return (Not IsNullType(obj) AndAlso ( _
  36. (TypeOf obj Is String AndAlso DirectCast(obj, String).Length = 0) OrElse _
  37. (TypeOf obj Is StringBuilder AndAlso DirectCast(obj, StringBuilder).Length = 0) OrElse _
  38. (TypeOf obj Is ICollection AndAlso DirectCast(obj, ICollection).Count = 0) OrElse _
  39. (TypeOf obj Is System.Array AndAlso DirectCast(obj, Array).Length = 0) OrElse _
  40. (IsSigned(obj) AndAlso obj = -1) OrElse _
  41. (TypeOf obj Is System.ValueType AndAlso obj = 0) OrElse _
  42. (TypeOf obj Is System.Guid AndAlso DirectCast(obj, Guid) = Guid.Empty) _
  43. ))
  44. End Function
  45.  
  46. ''' <summary>
  47. ''' Empty value or null object checker
  48. ''' </summary>
  49. ''' <returns>True if the object is empty or Nothing or DBNull</returns>
  50. Public Function IsNullTypeOrEmpty(ByVal obj As Object) As Boolean
  51. Return (IsNullType(obj) OrElse IsEmpty(obj))
  52. End Function
  53.  
  54. End Module
  55.  
  56. 'an example of how this makes your life easier
  57.  
  58. Private Sub Test(ByVal testVar)
  59. If (IsNullTypeOrEmpty(testVar)) Then
  60. throw new Exception("Invalid testVar")
  61. End If
  62.  
  63. DoSomething(testVar)
  64. End Sub

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.