0

如何防止其他开发人员在以下函数/子中输入“”或“”?

Public Sub MyFunction(MyString as String)

End Sub

' Call:
MyFunction("")

我希望他们最终得到一个不可编译的应用程序。

4

1 回答 1

1

There is no way to prevent compilation based on what is passed to the string. You can, however, simply prevent the method from executing, like this:

Public Sub MyFunction(myString as String)
    If Not String.IsNullOrWhitespace(myString) Then
        ' Do stuff here
    End If 
End Sub

Your other option is to throw an exception:

Public Sub MyFunction(myString as String)
    If String.IsNullOrWhitespace(myString) Then
        Throw New ApplicationException("No empty or whitespace strings allowed!")
    Else
        ' Do stuff here
    End If 
End Sub
于 2013-10-10T13:10:41.837 回答