try Catch for empty textfield

J

John Devlon

Hi,

I would like to check if a text field is empty; I'm using this code ...

Dim strTitle As String = String.Empty

Try
strTitle = Trim(txtTitle.Text)
Catch ex As Exception When strTitle = String.Empty
MessageBox.Show("error")
End Try


unfortunately, the exception doesn't work...

Anyone any idea's ?

Thanx

john
 
H

Herfried K. Wagner [MVP]

John Devlon said:
I would like to check if a text field is empty; I'm using this code ...

Dim strTitle As String = String.Empty

Try
strTitle = Trim(txtTitle.Text)
Catch ex As Exception When strTitle = String.Empty
MessageBox.Show("error")
End Try


unfortunately, the exception doesn't work...

'Trim' does not throw an exception.

\\\
If Len(Trim(Me.TextBoxTitle.Text)) = 0 Then
...
End If
///
 
O

Oenone

John said:
I would like to check if a text field is empty; I'm using this code [...]
unfortunately, the exception doesn't work...

That's because reading and trimming an empty string from a textbox doesn't
throw an exception.

Try this:

\\\
Dim strTitle As String = String.Empty

strTitle = Trim(txtTitle.Text)
If Len(strTitle) = 0 Then
MessageBox.Show("error")
End If
///
 
J

Jim Wooley

John Devlon said:
'Trim' does not throw an exception.

\\\
If Len(Trim(Me.TextBoxTitle.Text)) = 0 Then
...
End If
///

Actuall, String.Trim can throw an exception. Consider the following:
Dim foo As String
Console.WriteLine(foo.Trim)

In this case foo is not instanced and thus we try to Trim Nothing. Since
string is an object not a value type, it is not initiated by default and
thus can be null/nothing. Because of this, I always check for nothing on
my strings passed into to methods as parameters before processing them. (if
value = nothing then value = string.Empty). This is particularly noticable
when binding a Combobox's SelectedValue to string property of an object.

You are correct that the TextBox.Text can not pass back nothing, thus in
the OP's code, it will not throw an exception. Furthermore, relying on exceptions
when you can pre-test for a condition is akin to peeing your pants to see
if the fly is unzipped. Thus, checking to see if txtTitle.Text.Trim=String.Empty
is much better than using exceptions and try..catch blocks anyway.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.asp
 
H

Herfried K. Wagner [MVP]

Jim Wooley said:
Actuall, String.Trim can throw an exception. Consider the following:
Dim foo As String
Console.WriteLine(foo.Trim)

That's true, but I am using 'Microsoft.VisualBasic.Strings.Trim' instead of
'String.Trim'. Instead of performing the check if the variable containing
the string to be trimmed is 'Nothing' I delegate this check to 'Trim'.
 
G

Greg

John Devlon said:
Hi,

I would like to check if a text field is empty; I'm using this code ...

Dim strTitle As String = String.Empty

Try
strTitle = Trim(txtTitle.Text)
Catch ex As Exception When strTitle = String.Empty
MessageBox.Show("error")
End Try


unfortunately, the exception doesn't work...

Anyone any idea's ?

Thanx

john

If Trim(txtTitle.Text) = "" Then MsgBox ("error")

Cheers.
 
G

Guest

You are correct but whenever I check for a string's length, etc., I always
check for nothing first...I think it's good practice to get in this habit and
avoid unexpected errors since a lot of softwre doesn't check for boundaries
like variables set to nothing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top