【问题标题】:Checkpoints in functions VB.NET函数 VB.NET 中的检查点
【发布时间】:2017-09-26 11:31:16
【问题描述】:

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

所以问题出在这里:我需要在我的函数中放置一些检查点,所以当我调用验证函数时,我会根据用户选择的确认跳转到该检查点并从该检查点运行验证函数

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

【问题讨论】:

  • @ParrishHusband 我需要从函数中返回,做一些事情并再次调用它,我怎样才能用一个while循环来做到这一点?
  • 我读到的问题有点不同,所以忽略我最初的 While 循环评论。您能否更具体地说明您正在验证的内容以及该功能应该做什么?
  • @ParrishHusband 我刚刚编辑了我的问题!!

标签: vb.net winforms


【解决方案1】:

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

Class ValidationOutput
    ValidationId
    Message
    Result
End Class

调用您的验证函数后,您会得到一个需要用户提供额外信息的验证列表。这可以在验证功能之外处理。当您获得额外信息时,再次调用验证并将相同的列表作为参数传递。验证时,请查看列表以查看所需的所有额外信息是否都在那里。

【讨论】:

    【解决方案2】:

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

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

    您可以查看下面的链接以了解委托传递给方法。

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

    【讨论】:

      【解决方案3】:

      这应该适合你:

      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。

      在这方面会有一些变化可能更适合您,但也许您可以让我知道这是否适合您,如果需要,我可以建议其他选择。

      【讨论】:

      • @AliTheOne - 它应该可以正常工作。它与您的问题中的代码完全匹配。
      • @AliTheOne - 它当前使用单个参数 - Func(Of Response, Response) 的输入为 Response。如果您需要更多,您可以扩展 Func 签名,或者您可以在您的 Public Function Func 中定义委托并关闭您需要的变量。如果你告诉我你需要做什么,我可以提供一个例子。
      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多