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

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
BlackPearl
  • 2,532
  • 3
  • 33
  • 49

2 Answers2

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

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
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.

Andrew Mortimer
  • 2,380
  • 7
  • 31
  • 33
  • This is what I am trying to avoid altogether, having to write the `If ... Then ... End If...` every time – BlackPearl Jul 07 '15 at 08:28
  • 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. – Andrew Mortimer Jul 07 '15 at 08:31
  • Ok, I get you properly now. Thanks for the head up. – BlackPearl Jul 07 '15 at 08:34