函数VB.NET中的检查点

时间:2017-09-26 11:31:17

标签: vb.net winforms

我有一个检查表单插入数据有效性的函数,在这个函数中我要向用户询问一些确认,这个确认需要在函数外面询问,所以如果我点击其中一个确认,我创建消息并发出验证功能,用户确认与否,该功能将再次调用

所以这就是问题所在:我需要在我的函数中加入一些检查点,所以当我调用验证函数时,我会使用用户选择的确认跳转到该检查点并从该检查点运行验证函数

1:这有可能吗?

2:有任何想法吗?

编辑1:我正在我的业务层进行此验证,并且无法显示任何消息框,我只是创建消息并将其返回到UI层,然后从用户和函数调用再次获取答案这个答案,但我不想从头开始运行该功能,需要从我离开的地方运行它

Public Class BL
    Private Queue As Queue(Of String)
    Public Sub New()
        Dim checkpoints = New String(){"CheckPoint1","CheckPoint2","CheckPoint3"}
        checkpoints.ToList.ForEach(Function(item) <b>Queue.Enqueue(item)</b>)
    End Sub

    Public Function Func(ByVal res As Response,ParamArray ByVal params As String()) As Response
        Dim response As Response
        Dim chk = Queue.Dequeue()
        GoTo chk
        CheckPoint1:
        'Do some stuff
        response = New Response(Response.ResponseType.Message,"Are you sure you wanna do this?")
        Return response
        CheckPoint2:
        If(res.Type = Response.ResponseType.ResponseBack)
           Dim r As DialogResult = Convert.ChangeType([Enum].Parse(GetType(DialogResult),res.Message),GetType(DialogResult))
            If (r= DialogResult.OK)
            'Do stuffs on DialogResult.Yes
                Else
            'Do stuffs on DialogResult.No
            End If
            'Do other stuffs with database
        End If
        ' Do other stuff 
        response = New Response(Response.ResponseType.Message,"You should do this!!OK?")
        Return response
        CheckPoint3:
        'Do stuff like CheckPoint1

    End Function
End Class
 Public Class Response
   Friend  Enum ResponseType
        Message
        Result
        ResponseBack
        None
    End Enum
    Friend Message As String
    Friend Type As ResponseType
    Friend Sub New(ByVal type As ResponseType,msg As String)
        Message=msg
        Type= type
    End Sub
    End Class

Public Class Form1
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        Dim BL As New BL()
        ' Add any initialization after the InitializeComponent() call.
        Dim rese As Response
        Do 
            rese =BL.Func(Nothing)
            BL.Func(new Response(Response.ResponseType.ResponseBack,if(MessageBox.Show(rese.Message).ToString())))
        Loop Until rese.Type <> Response.ResponseType.Result
        MessageBox.Show(if(rese.Message="True","OK","ERROR"))
    End Sub
End Class

3 个答案:

答案 0 :(得分:1)

这不是一个客观的答案,但可以提供帮助。你需要某种包含问题和答案的课程。您的验证类会返回一个问题列表(您确定吗?)。

Class ValidationOutput
    ValidationId
    Message
    Result
End Class

调用验证函数后,您将获得需要用户提供额外信息的验证列表。这可以在验证功能之外处理。获取额外信息后,再次调用验证并传递与参数相同的列表。验证时,请查看列表以查看是否存在所需的所有额外信息。

答案 1 :(得分:1)

我认为您的业务逻辑不应该处理用户交互并分为两部分。

但是,如果您更喜欢这种方式,则可以使用回调。为验证/业务方法定义委托参数,并在需要确认时调用该委托。根据返回值继续保存操作与否。

您可以查看下面的链接,了解传递给方法的代理。

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/delegates/how-to-pass-procedures-to-another-procedure

答案 2 :(得分:0)

这应该适合你:

Public Class BL

    Private Queue As Queue(Of String)

    Private _checkpoints As Dictionary(Of String, Func(Of Response, Response)) = New Dictionary(Of String, Func(Of Response, Response)) From
    {
        { "CheckPoint1", Function (res) New Response(Response.ResponseType.Message, "Are you sure you wanna do this?") },
        { "CheckPoint2", Function (res) 
                If (res.Type = Response.ResponseType.ResponseBack)
                    Dim r As DialogResult = CType(Convert.ChangeType([Enum].Parse(GetType(DialogResult), res.Message), GetType(DialogResult)), DialogResult)
                    If (r = DialogResult.OK)
                        'Do stuffs on DialogResult.Yes
                    Else
                        'Do stuffs on DialogResult.No
                    End If
                    'Do other stuffs with database
                End If
                ' Do other stuff 
                Return New Response(Response.ResponseType.Message, "You should do this!!OK?")
            End Function
        },
        { "CheckPoint3", Function (r) New Response(Response.ResponseType.Message, "Are you sure you wanna do this?") }
    }

    Public Sub New()
        _checkpoints.ToList().ForEach(Sub(item) Queue.Enqueue(item.Key))
    End Sub

    Public Function Func(ByVal res As Response, ParamArray ByVal params As String()) As Response
        Dim chk = Queue.Dequeue()
        Return _checkpoints(chk).Invoke(res)
    End Function
End Class

基本上,这会创建一个Dictionary(Of String, Func(Of Response, Response)),将String映射到Func(Of Response, Response),返回您想要的Response

这可能会有一些变化,可能会更适合你,但也许你可以告诉我这是否适合你,如果需要,我可以建议其他选择。