Attention: We are retiring the ASP.NET Community Blogs. Learn more >

ShowUsYour<Blog>

Irregular expressions regularly

  • KPI ServerControl Source

    Option Strict On
    
    
    Imports System
    Imports System.ComponentModel
    Imports System.Configuration
    Imports System.Data
    Imports System.Drawing
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    
    
    
    '*********************************************************************
    '
    ' KPIs Class
    '
    ' WebControl that displays a listing of KPIs for a given Category/Month. 
    '
    '*********************************************************************
    
    < _
     Description("WebControl that displays a listing of KPIs for a given Category/Month."), _
     DefaultProperty("DataSource"), _
     Designer(GetType(KPIsDesigner)), _
     ToolboxData("<{0}:KPIs runat=server></{0}:KPIs>") _
    > _
    Public Class KPIs
        Inherits Web.UI.WebControls.Table
        Implements INamingContainer
    
        Private mstrCategoryName As String = String.Empty
        Private mstrNavigateUrl As String = String.Empty
        Private mstrBaseImagePath As String = String.Empty
        Private mdtmSelectedMonth As DateTime = DateTime.MinValue
        Private mDatasource As DataTable
    
        '*********************************************************************
        '
        ' BaseImagePath Property
        '
        ' The directory to look in to find the images.
        '
        '*********************************************************************
        < _
            Browsable(True), _
            Description("The directory to look in to find the images."), _
            Category("Data") _
        > _
        Public Property BaseImagePath() As String
            Get
                Return Me.mstrBaseImagePath
            End Get
            Set(ByVal Value As String)
                Me.mstrBaseImagePath = Value
            End Set
        End Property
    
    
        '*********************************************************************
        '
        ' CategoryName Property
        '
        ' The CategoryName to filterBy.
        '
        '*********************************************************************
        < _
            Browsable(True), _
            Description("The name of the current category being displayed."), _
            Category("Data") _
        > _
        Public Property CategoryName() As String
            Get
                Return Me.mstrCategoryName
            End Get
            Set(ByVal Value As String)
                Me.mstrCategoryName = Value
            End Set
        End Property
    
    
        < _
            Browsable(True), _
            Description("The foreground color for alternating items in the table."), _
            Category("Appearance") _
        > _
        Public Property AlternatingForeColor() As Color
            Get
                If ViewState("AlternatingForeColor") Is Nothing Then
                    Return Me.ForeColor
                Else
                    Return CType(ViewState("AlternatingForeColor"), Color)
                End If
            End Get
            Set(ByVal Value As Color)
                ViewState("AlternatingForeColor") = Value
            End Set
        End Property
    
    
        < _
            Browsable(True), _
            Description("The background color for alternating items in the table."), _
            Category("Appearance") _
        > _
        Public Property AlternatingBackColor() As Color
            Get
                If ViewState("AlternatingBackColor") Is Nothing Then
                    Return Me.ForeColor
                Else
                    Return CType(ViewState("AlternatingBackColor"), Color)
                End If
            End Get
            Set(ByVal Value As Color)
                ViewState("AlternatingBackColor") = Value
            End Set
        End Property
    
    
        < _
            Browsable(True), _
            Description("The DataTable to use as a datasource."), _
            Category("Data") _
        > _
        Public Property DataSource() As DataTable
            Get
                Return Me.mDatasource
            End Get
            Set(ByVal Value As DataTable)
                Me.mDatasource = Value
            End Set
        End Property
    
    
    
    
        '*********************************************************************
        '
        ' NavigateUrl Property
        '
        ' The Url to navigate to to display the details for a given KPI
        '
        '*********************************************************************
        < _
            Browsable(True), _
            Description("The Url to navigate to to display the details for a given KPI.  Uses a {0} placeholder to insert the Id of the KPI."), _
            Category("Data") _
        > _
        Public Property NavigateUrl() As String
            Get
                Return Me.mstrNavigateUrl
            End Get
            Set(ByVal Value As String)
                Me.mstrNavigateUrl = Value
            End Set
        End Property
    
    
        '*********************************************************************
        '
        ' Controls Property
        '
        ' Make sure that when you access a child control, the
        ' CreateChildControls method has been called.
        '
        '*********************************************************************
        Public Overrides ReadOnly Property Controls() As ControlCollection
            Get
                EnsureChildControls()
                Return MyBase.Controls
            End Get
        End Property
    
    
        '*********************************************************************
        '
        ' SelectedMonth Property
        '
        ' The SelectedMonth to filterBy.
        '
        '*********************************************************************
        < _
            Browsable(True), _
            Description("The resultset is filtered by the Month of the DateTime that is passed in."), _
            Category("Data") _
        > _
        Public Property SelectedMonth() As DateTime
            Get
                Return Me.mdtmSelectedMonth
            End Get
            Set(ByVal Value As DateTime)
                Me.mdtmSelectedMonth = Value
            End Set
        End Property
    
    
    
        Protected Overrides Sub CreateChildControls()
            Controls.Clear()
    
            If mDatasource Is Nothing Then
                mDatasource = GetData()
            End If
    
            If Not mDatasource Is Nothing Then
                Me.Rows.Add(BuildHeaderRow())
    
                For Each row As DataRow In mDatasource.Rows
                    Dim KPIId As Integer = Convert.ToInt32(row.Item("kpiId"))
                    Dim KPIDescription As String = row.Item("kpiDescription").ToString
    
                    Dim mtdRaw As Double = Convert.ToDouble(row.Item("mtdRawValue"))
                    Dim ytdRaw As Double = Convert.ToDouble(row.Item("ytdRawValue"))
                    Dim mtdDisplay As Integer = Convert.ToInt32(row.Item("mtdDisplayFlag"))
                    Dim ytdDisplay As Integer = Convert.ToInt32(row.Item("ytdDisplayFlag"))
    
                    Dim alternating As Boolean = (Me.Rows.Count Mod 2 = 0)
                    Me.Rows.Add(BuildRow(KPIId, KPIDescription, mtdRaw, ytdRaw, mtdDisplay, ytdDisplay, alternating))
                Next
            End If
        End Sub
    
    
        '*********************************************************************
        '
        ' Render Method
        '
        '*********************************************************************
        Protected Overrides Sub Render(ByVal tw As HtmlTextWriter)
            MyBase.Render(tw)
        End Sub ' Render
    
    
        Private Function BuildRow(ByVal kpiId As Integer, ByVal kpiDescription As String, ByVal mtdRaw As Double, _
                ByVal ytdRaw As Double, ByVal mtdDisplay As Integer, ByVal ytdDisplay As Integer, ByVal isAlternatingItem As Boolean) As TableRow
            Dim cell As TableCell
            Dim row As New TableRow
    
            If isAlternatingItem Then
                row.ForeColor = Me.AlternatingForeColor
                row.BackColor = Me.AlternatingBackColor
            End If
    
            Dim link As New HyperLink
            link.Text = kpiDescription
            If Not Me.mstrNavigateUrl Is String.Empty Then
                link.NavigateUrl = String.Format(Me.NavigateUrl, kpiId)
            End If
    
            cell = New TableCell
            cell.Controls.Add(link)
            row.Cells.Add(cell)
    
    
            cell = New TableCell
            cell.Controls.Add(GetImage(mtdDisplay, mtdRaw, "MTD"))
            row.Cells.Add(cell)
    
    
            cell = New TableCell
            cell.Controls.Add(GetImage(ytdDisplay, ytdRaw, "YTD"))
            row.Cells.Add(cell)
    
            Return row
        End Function
    
    
        Private Function BuildHeaderRow() As TableRow
            Dim cell As TableCell
            Dim row As New TableRow
    
            cell = New TableHeaderCell
            cell.Text = "&nbsp;"
            row.Cells.Add(cell)
    
            cell = New TableHeaderCell
            cell.Text = "MTD"
            row.Cells.Add(cell)
    
            cell = New TableHeaderCell
            cell.Text = "YTD"
            row.Cells.Add(cell)
    
            Return row
        End Function
    
    
        Private Function GetData() As DataTable
            Return Nothing
        End Function
    
    
        '*********************************************************************
        '
        ' GetImage Method
        '
        ' Returns the appropriate image for a particular rating.
        '
        '*********************************************************************
        Friend Function GetImage(ByVal ratingNumber As Integer, ByVal rawVal As Double, ByVal desc As String) As Web.UI.WebControls.Image
            Dim img As New Web.UI.WebControls.Image
    
            If Me.mstrBaseImagePath <> String.Empty Then
                If ratingNumber = 1 Then
                    img.ImageUrl = Page.ResolveUrl(String.Format("{0}/Green.gif", Me.mstrBaseImagePath))
                ElseIf ratingNumber = 2 Then
                    img.ImageUrl = Page.ResolveUrl(String.Format("{0}/Yellow.gif", Me.mstrBaseImagePath))
                ElseIf ratingNumber = 3 Then
                    img.ImageUrl = Page.ResolveUrl(String.Format("{0}/Red.gif", Me.mstrBaseImagePath))
                Else
                    img.ImageUrl = Page.ResolveUrl(String.Format("{0}/Spacer.gif", Me.mstrBaseImagePath))
                End If
            End If
    
            img.AlternateText = String.Format("{1} : {0}", rawVal, desc)
            Return img
        End Function 'GetImage 
    
    
    End Class
    

  • using C# and unlocking some secrets

    Recently, I've made a decision that all of the code that I write "out of hours" (which is most of the code that I write these days) will be written in C#; and what I'm finding is that switching languages is a very tricky affair.  There's so many little pieces of the language that you have to get used to.  Take the using statement - as opposed to the using directive - for example.  At first sight it seems interesting, according to the docco. it allows me to use an IDisposable type and the call to dispose will get handled under the covers; GREAT!  Or is it?  First of all, methinks that, by the very nature of IDisposable, anything that implemented that interface would be worth putting in a try{}catch{} because you are relying on an "external" resource being available!  So it appears that a statement such as:

  • Conditional matching

    Today, while proving what a prat I am, I added some conditional testing to a pattern which I had written.  Conditional matching allows you to perform a test based on if...then[...else] logic.  The syntax is (?if then | else ) - with the 'else' part being optional.

  • Good VBScript reference

    I never really did much VB6 programming - built a couple of insignificant apps but that was about it - and today a friend asked me to package some VBScript routines into an ActiveX Library project.  It all went pretty well except for the fact that I couldn't work out which .dll to reference to get the VBScript ESCAPE and UNESCAPE functions in my VB6 code.  While looking around Msdn for version numbers etc. I came across this neat little VBScript reference:

  • Using backreferences in a Match

    You can reference backreferences within a regular expression to keep the length and complexity of patterns to a minimum.  Backreferences can be referenced with the \1..\N or \k'name' notation.  To highlight where this is useful consider a simple-minded pattern for matching Dates.  Dates can (potentially) be delimited by either a space, a dot, a hyphen or a slash - that is, any of the following might be allowed:

  • Find words NOT IN text

    I'm currently writing a series of articles (that will hopefully get published {grin}) titled "Regex Reminders" that will provide dozens of code snippets that demonstrate how to perform common - and some not-so-common - operations using regex.

  • Date Regex - [ Note To Self ]

    While scouring through the pile of comments that have been left on RegexLib.com I noticed a pattern which purports to validate Dates in dmy format.  I couldn't help but notice that all of the comments which have been left have been highly favourable of this expression.