是否有可能让辅助函数导致调用函数 return/exit?
Is it possible to have a helper function cause the calling function to return/exit?
我有一个大型 function/sub,它在进入 ETL 设计模式之前尽早进行大量验证和错误检查。所以我在你想“提前失败”的地方使用“C/C++”-esque 错误检查,即如果验证失败,我会尽快调用 Return
(与 Exit Sub
相同)并且没有进一步的代码被执行。
注意下面 VB.net 代码中的重复代码:
If fn Is Nothing Then
lblDataError.Text = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename is blank, please press the <kbd>Browse</kbd> button first to select the file, then press <kbd>Upload</kbd>."
divFail.Visible = True
Return
ElseIf Path.GetExtension(fn).ToLower() <> ".csv" Then
lblDataError.Text = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename extension must be <kbd>.csv</kbd>."
divFail.Visible = True
Return
Else
For Each badChar As Char In System.IO.Path.GetInvalidFileNameChars
If InStr(fn, badChar) > 0 Then
lblDataError.Text = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename contains invalid character(s)."
divFail.Visible = True
Return
End If
Next
End If
是否可以制作一个辅助函数,使调用函数变为 return?如果这在 .NET 中不可行,是否有任何语言支持它?
Public Function Fail(customErrorMessage as String)
lblDataError.Text = "<b>Error!</b> " & customErrorMessage
divFail.Visible = True
Return Return 'This line should cause the calling function to return.
End Function
或者我应该只使用布尔值并在所有验证之后做一个 if 语句,因为这是不可能的吗?
我建议您查看使用 try/catch 和例外情况。支持 C# 和我能想到的几乎所有其他语言。
基于 Steve 的想法...在每个验证中调用失败函数。你知道你要去 return,所以像这样:
If fn Is Nothing Then
Call Fail("<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename is blank, please press the <kbd>Browse</kbd> button first to select the file, then press <kbd>Upload</kbd>.")
Return
ElseIf ...
不,无法从 VB 中的被调用方法中退出调用方法。但是,如果您重新考虑您的问题,通常会有其他方法来构建您的逻辑以避免重复。例如,在这种情况下,一种选择是简单地使用错误消息变量,然后在最后执行一次 displaying/returning,如果设置了变量:
Dim error As String = Nothing
If fn Is Nothing Then
error = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename is blank, please press the <kbd>Browse</kbd> button first to select the file, then press <kbd>Upload</kbd>."
ElseIf Path.GetExtension(fn).ToLower() <> ".csv" Then
error = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename extension must be <kbd>.csv</kbd>."
Else
For Each badChar As Char In System.IO.Path.GetInvalidFileNameChars
If InStr(fn, badChar) > 0 Then
error = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename contains invalid character(s)."
Exit For
End If
Next
End If
If error IsNot Nothing Then
lblDataError.Text = error
divFail.Visible = True
Return
End If
在这种情况下,考虑制作一个函数委托列表通常也很有帮助,然后您可以遍历所有委托,调用每个委托并在一个地方处理结果,对所有委托都采用相同的方式代表们。或者,对于更复杂的事情,您可以将每个规则放入单独的 class,存储它们的列表,并在循环中检查每个规则,类似于委托方法。但是,在您提供的代码中,类似的任何东西都可能是矫枉过正。
附带说明一下,无效文件名字符检查可以简化为:
ElseIf Path.GetInvalidFileNameChars().Any(Function(c) fn.Contains(c)) Then
error = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename contains invalid character(s)."
End If
我有一个大型 function/sub,它在进入 ETL 设计模式之前尽早进行大量验证和错误检查。所以我在你想“提前失败”的地方使用“C/C++”-esque 错误检查,即如果验证失败,我会尽快调用 Return
(与 Exit Sub
相同)并且没有进一步的代码被执行。
注意下面 VB.net 代码中的重复代码:
If fn Is Nothing Then
lblDataError.Text = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename is blank, please press the <kbd>Browse</kbd> button first to select the file, then press <kbd>Upload</kbd>."
divFail.Visible = True
Return
ElseIf Path.GetExtension(fn).ToLower() <> ".csv" Then
lblDataError.Text = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename extension must be <kbd>.csv</kbd>."
divFail.Visible = True
Return
Else
For Each badChar As Char In System.IO.Path.GetInvalidFileNameChars
If InStr(fn, badChar) > 0 Then
lblDataError.Text = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename contains invalid character(s)."
divFail.Visible = True
Return
End If
Next
End If
是否可以制作一个辅助函数,使调用函数变为 return?如果这在 .NET 中不可行,是否有任何语言支持它?
Public Function Fail(customErrorMessage as String)
lblDataError.Text = "<b>Error!</b> " & customErrorMessage
divFail.Visible = True
Return Return 'This line should cause the calling function to return.
End Function
或者我应该只使用布尔值并在所有验证之后做一个 if 语句,因为这是不可能的吗?
我建议您查看使用 try/catch 和例外情况。支持 C# 和我能想到的几乎所有其他语言。
基于 Steve 的想法...在每个验证中调用失败函数。你知道你要去 return,所以像这样:
If fn Is Nothing Then
Call Fail("<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename is blank, please press the <kbd>Browse</kbd> button first to select the file, then press <kbd>Upload</kbd>.")
Return
ElseIf ...
不,无法从 VB 中的被调用方法中退出调用方法。但是,如果您重新考虑您的问题,通常会有其他方法来构建您的逻辑以避免重复。例如,在这种情况下,一种选择是简单地使用错误消息变量,然后在最后执行一次 displaying/returning,如果设置了变量:
Dim error As String = Nothing
If fn Is Nothing Then
error = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename is blank, please press the <kbd>Browse</kbd> button first to select the file, then press <kbd>Upload</kbd>."
ElseIf Path.GetExtension(fn).ToLower() <> ".csv" Then
error = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename extension must be <kbd>.csv</kbd>."
Else
For Each badChar As Char In System.IO.Path.GetInvalidFileNameChars
If InStr(fn, badChar) > 0 Then
error = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename contains invalid character(s)."
Exit For
End If
Next
End If
If error IsNot Nothing Then
lblDataError.Text = error
divFail.Visible = True
Return
End If
在这种情况下,考虑制作一个函数委托列表通常也很有帮助,然后您可以遍历所有委托,调用每个委托并在一个地方处理结果,对所有委托都采用相同的方式代表们。或者,对于更复杂的事情,您可以将每个规则放入单独的 class,存储它们的列表,并在循环中检查每个规则,类似于委托方法。但是,在您提供的代码中,类似的任何东西都可能是矫枉过正。
附带说明一下,无效文件名字符检查可以简化为:
ElseIf Path.GetInvalidFileNameChars().Any(Function(c) fn.Contains(c)) Then
error = "<b>Error!</b> Could not upload your file. <b>Reason:</b> Filename contains invalid character(s)."
End If