Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Coalesce -- Any suggestions on how to improve this?

Coalesce -- Any suggestions on how to improve this?

Scheduled Pinned Locked Moved Visual Basic
databasetutorialquestioncode-review
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gregory Gadow
    wrote on last edited by
    #1

    In rewriting a website, I have found it useful to be able to select the first non-empty value from a list, where "empty" is defined as Nothing, DBNull.Value and String.Empty. The code I wrote looks like this:

    Public Shared Function Coalesce(ByVal ParamArray Items() As Object) As Object
        Dim Result As Object = Nothing
    
        For Each Obj As Object In Items
            If Obj IsNot Nothing AndAlso Obj IsNot DBNull.Value Then
                If Not String.IsNullOrEmpty(Obj.ToString) Then
                    Result = Obj
                    Exit For
                End If
            End If
        Next
    
        Return Result
    End Function
    

    This way, I could write something like this:

    Response.Write(Toolbox.Coalesce(DR("TerminatedDate"), "%nbsp;").ToString)

    (Yeah, the nbsp is wrong; I couldn't get it to show up otherwise.) The inputs might be various types -- database fields, date variables, strings, etc. The output will almost always be converted into a String, but not necessarily. This looks like it should do what I want, but I'm not fully convinced. Any suggestions on how to improve this?

    R 1 Reply Last reply
    0
    • G Gregory Gadow

      In rewriting a website, I have found it useful to be able to select the first non-empty value from a list, where "empty" is defined as Nothing, DBNull.Value and String.Empty. The code I wrote looks like this:

      Public Shared Function Coalesce(ByVal ParamArray Items() As Object) As Object
          Dim Result As Object = Nothing
      
          For Each Obj As Object In Items
              If Obj IsNot Nothing AndAlso Obj IsNot DBNull.Value Then
                  If Not String.IsNullOrEmpty(Obj.ToString) Then
                      Result = Obj
                      Exit For
                  End If
              End If
          Next
      
          Return Result
      End Function
      

      This way, I could write something like this:

      Response.Write(Toolbox.Coalesce(DR("TerminatedDate"), "%nbsp;").ToString)

      (Yeah, the nbsp is wrong; I couldn't get it to show up otherwise.) The inputs might be various types -- database fields, date variables, strings, etc. The output will almost always be converted into a String, but not necessarily. This looks like it should do what I want, but I'm not fully convinced. Any suggestions on how to improve this?

      R Offline
      R Offline
      rhuiden
      wrote on last edited by
      #2

      I wrote an extension method for this not too long ago.

      <System.Runtime.CompilerServices.Extension()> _
      Public Function GetColumnAsString(ByVal rdr As System.Data.IDataReader, ByVal columnName As String, Optional ByVal valueIfNull As String = "") As String
      Dim obj As Object

      	If rdr Is Nothing Then
      		Return valueIfNull
      	Else
      		obj = rdr(columnName)
      		If obj IsNot Nothing AndAlso obj.Equals(DBNull.Value) = False Then
      			Return obj.ToString()
      		Else
      			Return valueIfNull
      		End If
      	End If
      End Function
      

      Note: it does not check for make sure the column specified exists. Using your example it would be

      Response.Write(DR.GetColumnAsString("TerminatedDate", "%nbsp;"))

      You could also embed it for more of a coalesce functionality.

      Response.Write(DR.GetColumnAsString("TerminatedDate", DR.GetColumnAsString("SomeOtherDate", "%nbsp;"))

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups