Posted By

bryanlyman on 10/14/11


Tagged

valid class input method c conditional Net system exception global checking Generic types typeof extension easy Framework Assertion casting boxing bryan lyman valuetype effortless


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

danfinrud55


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.

  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
Posted By: rpresser on November 17, 2011

This is wrong. Extension methods are possible and easy in VB.NET. Here is my favorite:

Imports System.Runtime.CompilerServices
Imports System.Xml.Serialization

Module ToFromXml

     _
    Public Function ToXml(Of T)(ByVal x As T) As String
        Dim ser As New XmlSerializer(GetType(T))
        Using sw As New IO.StringWriter
            Using xtw As New System.Xml.XmlTextWriter(sw)
                xtw.Indentation = False
                ser.Serialize(xtw, x)
                Return sw.ToString()
            End Using
        End Using
    End Function

     _
    Public Function ReadFromXml(Of T)(ByRef x As T, ByVal xml As String) As T
        Dim ser As New XmlSerializer(GetType(T))
        Using sr As New IO.StringReader(xml)
            x = CType(ser.Deserialize(sr), T)
            Return x
        End Using
    End Function

End Module
Posted By: rpresser on November 17, 2011

Trying again:

Imports System.Runtime.CompilerServices
Imports System.Xml.Serialization

Module ToFromXml

    `` _
    Public Function ToXml(Of T)(ByVal x As T) As String
        Dim ser As New XmlSerializer(GetType(T))
        Using sw As New IO.StringWriter
            Using xtw As New System.Xml.XmlTextWriter(sw)
                xtw.Indentation = False
                ser.Serialize(xtw, x)
                Return sw.ToString()
            End Using
        End Using
    End Function

    `` _
    Public Function ReadFromXml(Of T)(ByRef x As T, ByVal xml As String) As T
        Dim ser As New XmlSerializer(GetType(T))
        Using sr As New IO.StringReader(xml)
            x = CType(ser.Deserialize(sr), T)
            Return x
        End Using
    End Function

End Module

Usage:

Module test
      Sub test
           Dim x As New YourClass
          Dim xml As String = x.ToXML()
          Dim y as New YourClass
          y.ReadFromXml(xml)
     End Sub
End Module
Posted By: rpresser on November 17, 2011

I don't know why the [Extension()] isn't showing. It is a code attribute applied to the Function on the line above.

You need to login to post a comment.