0

How would I prevent other developers from enter "" or " " into the following function/sub?

Public Sub MyFunction(MyString as String)

End Sub

' Call:
MyFunction("")

I want them to end up with a non compileable app.

2
  • 2
    I don't think that the content of a string is available at compile time. Commented Oct 10, 2013 at 13:02
  • Well, I'm not talking about string variables but rather hardcoded strings, e.g. "This is going in" Commented Oct 10, 2013 at 13:04

1 Answer 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
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, OK, I see I can't achieve what I want. This way the end user would get the message. I don't want this. Thank you anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.