VB 方法命名建议

VB Method naming suggestion

我有一个方法:

问题(相互关联):

你有什么建议吗?谢谢!

Private Function CancelDueToUnsaved() As Boolean
    If Not _data.Modified Then Return False

    Dim answ = MsgBox("Save changes?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.Question, "")

    If answ = MsgBoxResult.Yes Then SaveData()

    Return (answ = MsgBoxResult.Cancel)
End Function

Private Sub Form_Closing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    If CancelDueToUnsaved() Then e.Cancel = True
End Sub

我会把它命名为 "SaveChanges"(正如 Tom 提到的),因为它应该显示保存数据操作发生在此处。如果重要的话,命名参数也可以提高可读性:

Private Sub SaveChanges(ByRef cancelled as Boolean)
...
End Sub

您也可以抛出自定义异常,但对于单个布尔值来说太多了:

Private Sub SaveChanges()
...
If answ = MsgBoxResult.Cancel Then Throw New UserCancelException
...
End Sub

我认为这是很常见的情况;在我的表格中,我使用两个函数解决了问题:

Function SaveChanges() As Boolean

    SaveChanges = False

    'check if data compiled is ok
    'save changes under transaction
    '(...)

    'if everything is ok
    SaveChanges = True

End Function

Function blnExitMode() As Boolean

    blnExitMode = True

    'code to check if there are changes to save
    '(...)

    'if there are changes to save
    Select Case MsgBox("Save changes?", MsgBoxStyle.Question + MsgBoxStyle.YesNoCancel + MsgBoxStyle.DefaultButton3)
        Case MsgBoxResult.Yes
            If Not Me.SaveChanges Then blnExitMode = False

        Case MsgBoxResult.Cancel
            blnExitMode = False

    End Select

End Function

所以我可以在表单关闭时使用此代码:

If Not Me.blnExitMode Then e.Cancel() = True

当用户尝试移动到另一条记录而不保存更改或更改表单 'modality'(即从插入到搜索)时,我也可以调用 blnExitMode

看起来你只是在说"when the form is closed, if changes have been made that were not saved, prompt the user whether to save or not or not close the form. If Yes, save changes and close, if No, do not save but close anyway, if Cancel, do not save or close."是吗?

您可以在表单关闭事件本身内轻松完成此操作,而不是调用其他内容并尝试传递响应(对您来说这有点困难,因为您尝试使用布尔值,但有 3 个选项) .

这就是我自己完成的方法...希望这对您有所帮助:

Private Sub Form_Closing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    If _data.Modified Then
        Dim dialogResult = MessageBox.Show(Me, "Changes have been made.  Would you like to save?", "Unsaved Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

        If dialogResult = System.Windows.Forms.DialogResult.Yes Then
            SaveData()

        ElseIf dialogResult = System.Windows.Forms.DialogResult.Cancel Then
            e.Cancel = True

        Else
            'result is No - don't do anything...just let the form close
        End If
    End If

End Sub

[已解决]

  • 使用表示主要任务的简单名称:我更喜欢 HandleUnsavedChanges() 而不是简单的 SaveChanges()
  • 如果用户取消,则使用 ByRef 参数返回。

注意:我也一直在单击重置按钮时使用此代码,其中一个简单的问题 OK/Cancel 更合适。但我还是放弃了这个按钮,因为它没有多大意义。因此,HandleUnsavedChanges 方法现在实际上可以在 Form.Close().

的事件中传输
Private Sub HandleUnsavedChanges(ByRef userCancelled As Boolean)
    If Not _data.IsModified Then Exit Sub

    Dim answ = MsgBox(msg, MsgBoxStyle.YesNoCancel + MsgBoxStyle.Question, "Bestätigung")
    Select Case answ
        Case MsgBoxResult.Yes
            SaveData()
        Case MsgBoxResult.Cancel
            userCancelled = True
    End Select
End Sub

Private Sub Form_Closing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    HandleUnsavedChanges(userCancelled:=e.Cancel)
End Sub