The key here is implementing the IComparable interface when creating your class (see link above for more information).  The IComparable interface has the CompareTo method that does the sorting. Once the interface is in place, you can use the sort method in the generic list: to sort your list (GenericList.Sort()).  Here is some sample code that implements the IComparable interface to an Ingredients Generic list.  To sort this list by IngredientID, instantiate the class (Dim objIngredientList as List(Of BEIngredientList = New List(Of BEIngredientList)}, popluate the list, and then excute the sort method objIngredientList.Sort().

  Public Class BEIngredientList 
   Implements IComparable(Of BEIngredientList)
#Region "Locals"
    Private _ingredientsID As Integer
    Private _ingredientName As String
    Private _ingredientCategoryID As Integer
#End Region
    Public Property IngredientID() As Integer
            Return _ingredientsID
        End Get
        Set(ByVal value As Integer)
            _ingredientsID = value
        End Set
    End Property
    Public Property IngredientName() As String
            Return _ingredientName
        End Get
        Set(ByVal value As String)
            _ingredientName = value
        End Set
    End Property
    Public Property IngredientCategoryID() As Integer
            Return _ingredientCategoryID
        End Get
        Set(ByVal value As Integer)
            _ingredientCategoryID = value
        End Set
    End Property
    Public Function CompareTo(ByVal other As BEIngredientList) As Integer Implements System.IComparable(Of BEIngredientList).CompareTo
        Return Me.IngredientID.CompareTo(other.IngredientID)
    End Function
End Class


  • Computed Columns with Microsoft SQL 2005

    Select the table to which you want to add a computed column. From the Design screen, select the column name (in the example below we use a column called PreparedDryWeight).  Next, in the Column Properties screen expand the Computed Column Specification.  Enter the formula  into the (Formula) field as shown in the example below.
    (case when [PreparedMoisturePercentage]>(0) then [PreparedNetWeight]-[PreparedNetWeight]*([PreparedMoisturePercentage]*(0.01)) else (0) end)

    Note: You cannot use another computed column in the calculation.  Check ou this link The Power of SQL Case Statements by Scott Mitchell

  • Refining a value from a Date data type field using the Value property .

    obj.DryingDate.Value.Date   -Get the Date as Date data type
    obj.DryingDate.Value.Year   -Get the Year as Integer data type

  • Return a DataSet object type with Enterprise Library.  The key is the database.ExecuteDataSet(dbCommand) method.  See sample code below.

    Public Function GetDistinctBatchProductDesc(ByVal statusID As Integer, _
                                                    ByVal receivingLocationID As Integer) As DataSet
            Dim ret As New DataSet
            Dim db As Database = DatabaseFactory.CreateDatabase("SQLDataAccess")
            Dim sqlCommand As String = "Inventory.GetDistinctBatchProductDesc"
            Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(sqlCommand)
            db.AddInParameter(dbCommand, "StatusID", DbType.Int32, statusID)
            db.AddInParameter(dbCommand, "ReceivingLocationID", DbType.Int32, receivingLocationID)
            ret = db.ExecuteDataSet(dbCommand)
            Return ret
        End Function

  • WPF Videos

  • Preventing Users from Leaving Invalid Controls in Windows Forms.

    In a Validating event handler, you can prevent users from leaving the control containing the invalid entry until they clear or fix the error. If you look back to the code that assigned the event handler to the control, you will note that it wrapped the method in a CancelEventHandler. That provides the flexibility to add this line in the handling routine.

    e.Cancel = true; 

    ' Sample code that checks to make sure a textbox control contains text.

    Private Sub txtTypeCode_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTypeCode.Validating
            If Not Regex.IsMatch(txtTypeCode.Text, "\w") Then
     MessageBox.Show("Type Code CANNOT be blank", "Input Error", MessageBoxButtons.OK,           MessageBoxIcon.Error)
                ' Prevents users from leaving textbox until they fix it!
                e.Cancel = True
            End If
    End Sub

    That line of code suppresses any events that would normally follow the Validating event.  To users, the code causes the cursor to remain stubbornly in the control; neither tabbing nor mousing will get it to budge until the user corrects the error.  For more information on the Validating event, see this MSDN article.

  • How to make input parameters optional in T-SQL. Place an "=" sign after the datatype, and then assign a DEFAULT value.

    Create Procedure OptionalParametersDemo
       @floatTest      float=Null,
       @varcharTest varchar(10)=Null,
       @dateTest     datetime=Null,
       @intTest        int=Null
    As ...

  • How to toggle a boolean field in VB.Net:  blnFlag = Not blnFlag