Statt einer einfachen Rückgabe mit True oder False oder einer Rückgabe von Objekten mit Nothing kann man diese aussagekräftig definieren.
Namespace OkResult
''' <summary>
''' flexible Rückgabeparameter
''' mit einer evtl. Fehlermeldung
''' </summary>
''' <remarks></remarks>
Interface IBoolMultiResult
Property IsOk() As Boolean
Property ErrMsg() As String
End Interface
''' <summary>
''' flexible Rückgabeparameter
''' mit typgerechter Rückgabe und
''' mit einer evtl. Fehlermeldung
''' </summary>
''' <typeparam name="T"></typeparam>
''' <remarks></remarks>
Interface IBoolObjectResult(Of T)
Property IsOk() As Boolean
Property Result() As T
Property ErrMsg() As String
End Interface
Public Class BoolResult
Implements IBoolMultiResult
Private m_err As String = ""
Private m_IsOK As Boolean
Public Sub New()
End Sub
Public Sub New(ByVal bIsOk As Boolean, Optional ByVal sErrMsg As String = "")
m_err = sErrMsg : m_IsOK = bIsOk
End Sub
''' <summary>
''' Bei IsOK=false sollte hier eine Fehlermeldung stehen
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Property ErrMsg() As String Implements IBoolMultiResult.ErrMsg
Get
Return m_err
End Get
Set(ByVal value As String)
m_err = value
End Set
End Property
''' <summary>
''' Ob die Aktion mit/ohne Fehler abgeschlossen wurde
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Property IsOk() As Boolean Implements IBoolMultiResult.IsOk
Get
Return m_IsOK
End Get
Set(ByVal value As Boolean)
m_IsOK = value
End Set
End Property
End Class
Public Class ObjectResult(Of T)
Implements IBoolObjectResult(Of T)
Private m_err As String = ""
Private m_IsOK As Boolean
Private m_Result As T
Public Sub New()
End Sub
Public Sub New(ByVal tResult As T, ByVal bIsOk As Boolean, Optional ByVal sErrMsg As String = "")
m_err = sErrMsg : m_IsOK = bIsOk
m_Result = tResult
End Sub
''' <summary>
''' Bei IsOK=false sollte hier eine Fehlermeldung stehen
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Property ErrMsg() As String Implements IBoolObjectResult(Of T).ErrMsg
Get
Return m_err
End Get
Set(ByVal value As String)
m_err = value
End Set
End Property
''' <summary>
''' Ob die Aktion mit/ohne Fehler abgeschlossen wurde
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Property IsOk() As Boolean Implements IBoolObjectResult(Of T).IsOk
Get
Return m_IsOK
End Get
Set(ByVal value As Boolean)
m_IsOK = value
End Set
End Property
''' <summary>
''' Übergabe des Ergebnisses (oder Nothing)
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Property Result() As T Implements IBoolObjectResult(Of T).Result
Get
Return m_Result
End Get
Set(ByVal value As T)
m_Result = value
End Set
End Property
End Class
Public Class testCls
Public Shared Function CopyFile(ByVal Quelldatei As String, ByVal ZielDatei As String) As BoolResult
If (Not IO.File.Exists(Quelldatei)) OrElse (String.IsNullOrEmpty(ZielDatei)) Then
Return New BoolResult(False, "Ungültige Parameterübergabe")
End If
Try
My.Computer.FileSystem.CopyFile(Quelldatei, ZielDatei)
Catch ex As Exception
Return New BoolResult(False, ex.Message)
End Try
Return New BoolResult(True)
End Function
End Class
Public Shared Function LoadEffects() As OkResult.ObjectResult(Of DataTable)
Dim res As New OkResult.ObjectResult(Of DataTable)
Dim path As String = My.Application.Info.DirectoryPath
Dim file As String = String.Format("{0}\SmsEffekt.xml", path)
If IO.File.Exists(file) Then
Try
Dim dt As New DataTable
dt.ReadXml(file)
Return New OkResult.ObjectResult(Of DataTable)(dt, True)
Catch ex As Exception
Return New OkResult.ObjectResult(Of DataTable)(Nothing, False, ex.Message)
End Try
End If
Return New OkResult.ObjectResult(Of DataTable)(Nothing, False, "Datei nicht gefunden " + file)
End Function
End Namespace
Kommentare zum Snippet