0

I am trying to create a one line empty string validation Public Function but am yet to achieve this aim.

The deal is to use a Public Function to test the string and exit the sub-routine that called on the function but so far, I can test for empty string but not exit the sub-routine.

Codes for far...

 Public Class Validator Public Function isEmpty(ByVal fieldData, ByVal fieldName) If fieldData = "" Then MsgBox("Enter " & fieldName & "!") Exit Function Else Return fieldData End If End Function End Class 

And to use it, I want something like this...

Dim surname as string surname= isEmpty(txtSurname.text, "Surname") 

This checks correctly, but how can I exit the sub-routine?

Thanks in advance

2
  • Do you want to continue processing if it returns an empty string? Or stop and get the user to do something about it? Commented Jul 7, 2015 at 8:06
  • stop processing and let the user Enter a Surname. Commented Jul 7, 2015 at 8:06

2 Answers 2

2

I would suggest your function returns a Boolean value indicating whether the validation passed or failed.

you could pass a variable to the function ByRef and populate this with the validated string if your validation passes. Something like this:

Public Function IsEmpty(ByVal fieldData As String, ByVal fieldName As String, ByRef outField As String) As Boolean outField = "" If String.IsNullOrEmpty(fieldData) Then MsgBox("Enter " & fieldName & "!") Return False Else outField = fieldData Return True End If End Function 

Usage:

 Dim surname As String If Not IsEmpty(txtsurname.text, "Surname", surname) Then Exit Sub 

Having said that there are built in methods for validation that you should definately have a look at: WinForm UI Validation

Note: You should turn Option Strict On as this will pick up things you have missed like return types and variable types

Sign up to request clarification or add additional context in comments.

2 Comments

Nice but will prefer a one line thingy if possible.
? Apart from declaring the variable - that is a one line "thingy"
1

How about :

Private Sub SaveButton_Click(sender As System.Object, e As System.EventArgs) Handles SaveButton.Click Try If isScreenInfoValid() Then fetchDataFromUI() End If Catch ex As Exception MessageBox.Show(String.Concat("An error occurred: ", ex.Message)) End Try End Sub Private Function isScreenInfoValid() As Boolean If isEmpty(txtSurname.Text, "Surname") Then txtSurname.Focus() Return False End If ' If isEmpty(txtFirstName.Text, "First Name") Then txtFirstName.Focus() Return False End If Return True End Function Private Sub fetchDataFromUI() Dim surname As String = txtSurname.Text Dim firstName As String = txtFirstName.Text 'update the database or whatever... End Sub 

Ideally, I think you would want to do all your validation up front in a separate function and then collect data from the form if all the validation passes.

3 Comments

This is what I am trying to avoid altogether, having to write the If ... Then ... End If... every time
The point I was trying to get across was that you have two methods. 1 for validation, 1 for data assignment. So all the isEmptys would be in the validation (why would you want to assign if the user gets half way through and fails validation)? If the validation passes, then all the assignments are in another method.
Ok, I get you properly now. Thanks for the head up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.