Posts Tagged ‘VB.Net’

VB.Net overlap dates.

March 20, 2008

If you want to check overlap between dates you can use this function

Public Shared Function IsOverlapDate(ByVal D1Begin As Date, ByVal D1End As Date, ByVal D2Begin As Date, ByVal D2End As Date) As Boolean

If Date.Compare(D1Begin, D2End) <= 0 And Date.Compare(D1End, D2Begin) >= 0 Then

Return True

End If

Return False

End Function

‘_____________________________________________________

Then try to test The function

Dim DBegin1 as New Date(2008,2,14)

Dim DEnd1 as New Date(2008,3,22)

Dim DBegin2 as New Date(2008,3,10)

Dim DEnd2 as New Date(2008,4,21)

Dim DBegin3 as New Date(2008,4,2)

Dim DEnd3 as New Date(2008,4,3)

Dim blnResult as Boolean

blnResult = IsOverlapDate(DBegin1,DEnd1,DBegin2,DEnd2) ‘ It is return true

blnResult = IsOverlapDate(DBegin3,DEnd3,DBegin2,DEnd2) ‘ It is return true

blnResult = IsOverlapDate(DBegin1,DEnd1,DBegin3,DEnd3) ‘ It is return false

Handler Error VB.Net

February 14, 2008

Every program has error and we meet it again and again sometimes we need to copy exception message and VB 2005 not provide convenient way to copy it.

This article demonstrate how we handler exception while development phase.

 

 

 

1. Create class for contain exception data.

 

Public Class ExceptionData

 

Public FileName As String

Public ClassName As String

Public Method As String

Public Section As String

Public Ex As Exception

 

Sub New(ByVal strFileName As String, ByVal strClassName As String)

Me.FileName = strFileName

Me.ClassName = strClassName

 

End Sub

 

End Class

 

 

2. Create class for handler Error.

this class is use for create text for display for us.

 

 

Public Class ErrorHandler

 

Public Shared Sub ShowError(ByVal cEx As ExceptionData)

 

Dim f As New frmShowErrorforDebug

f.ErrorDisplay = GetStringfromException(cEx)

f.ShowDialog()

End Sub

 

Public Shared Function GetStringfromException(ByVal File As String, ByVal ClassName As String, ByVal Method As String, ByVal Section As String, ByVal EX As Exception) As String

 

Dim strB As New System.Text.StringBuilder

 

strB.Append(“Error Occur “).Append(vbCrLf).Append(vbCrLf)

 

 

strB.Append(“File :”).Append(File).Append(vbCrLf)

strB.Append(“ClassName :”).Append(ClassName).Append(vbCrLf)

strB.Append(“Method :”).Append(Method).Append(vbCrLf)

 

 

If Not IsNothing(Section) Then

If Section.Trim <> “” Then

strB.Append(“Section :”).Append(Section).Append(vbCrLf)

 

End If

End If

 

strB.Append(“Source :”).Append(EX.Source).Append(vbCrLf)

strB.Append(“Method throw current exception :”).Append(EX.TargetSite.Name).Append(vbCrLf)

 

strB.Append(“Message :”).Append(EX.Message).Append(vbCrLf)

 

strB.Append(vbCrLf).Append(vbCrLf)

strB.Append(“Stack Trace :”).Append(EX.StackTrace)

 

Return strB.ToString

 

End Function

Public Shared Function GetStringfromException(ByVal cEx As ExceptionData) As String

Return GetStringfromException(cEx.FileName, cEx.ClassName, cEx.Method, cEx.Section, cEx.Ex)

 

 

End Function

End Class

 

3. Create frmShowErrorforDebug

 

in this case we just use for Windowsform application, this form is just contain one textbox for show error data and two button one for copy error data to clipboard ,two for close form.

If you want to handler error for web application you just change from windows form to webform instead.

 

Public Class frmShowErrorforDebug

Public ErrorDisplay As String

 

 

Private Sub frmShowError_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.TextBox1.Text = errordisplay

End Sub

 

Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click

Clipboard.SetText(Me.TextBox1.Text)

 

End Sub

 

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

Me.Close()

End Sub

End Class

 

Now we already created classes for handler error and then we will test it.

 

 

Public Class frmLogin

 

Private FileName As String = “frmLogin.vb”

 

Private cEx As New ExceptionData(FileName, “frmLogin”)

 

Private Sub btnTestError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestError.Click

 

cEx.Method = “btnTestError_Click”

Try

 

Dim str As String = “Hello”

cEx.Section = “Convert str to dbl”

Dim dbl As Double = CDbl(str)

 

Catch ex As Exception

 

 

 

cEx.Ex = ex

ErrorHandler.ShowError(cEx)

 

 

 

End Try

End Sub

End Class

It done.