Connecting Tech Pros Worldwide Help | Site Map

try Catch for empty textfield

John Devlon
Guest
 
Posts: n/a
#1: Aug 29 '06
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



Stuart Nathan
Guest
 
Posts: n/a
#2: Aug 29 '06

re: try Catch for empty textfield


try =""


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#3: Aug 29 '06

re: try Catch for empty textfield


"John Devlon" <johndevlon@hotmail.comschrieb:
Quote:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Oenone
Guest
 
Posts: n/a
#4: Aug 29 '06

re: try Catch for empty textfield


John Devlon wrote:
Quote:
I would like to check if a text field is empty; I'm using this code
[...]
Quote:
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
///

--

(O)enone


Jim Wooley
Guest
 
Posts: n/a
#5: Aug 29 '06

re: try Catch for empty textfield


"John Devlon" <johndevlon@hotmail.comschrieb:
Quote:
>
Quote:
>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
///
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.aspx


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Aug 29 '06

re: try Catch for empty textfield


"Jim Wooley" <jimNOSPAMwooley@hotmail.comschrieb:
Quote:
Quote:
Quote:
>>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
>///
>
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'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Greg
Guest
 
Posts: n/a
#7: Aug 30 '06

re: try Catch for empty textfield


"John Devlon" <johndevlon@hotmail.comwrote in message
news:7OUIg.47633$a6.711718@phobos.telenet-ops.be...
Quote:
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.


Dennis
Guest
 
Posts: n/a
#8: Aug 31 '06

re: try Catch for empty textfield


I would use:

If not txtTitle.Text is nothing andalso txtTitle.Text.Trim.Length<=0 then
'do something if true
end if
--
Dennis in Houston


"Greg" wrote:
Quote:
"John Devlon" <johndevlon@hotmail.comwrote in message
news:7OUIg.47633$a6.711718@phobos.telenet-ops.be...
Quote:
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.
>
>
>
Al Reid
Guest
 
Posts: n/a
#9: Aug 31 '06

re: try Catch for empty textfield


"Dennis" <Dennis@discussions.microsoft.comwrote in message news:243F095E-8052-4AD9-B591-C8C9862C4D3F@microsoft.com...
Quote:
I would use:
>
If not txtTitle.Text is nothing andalso txtTitle.Text.Trim.Length<=0 then
'do something if true
end if
--
Dennis in Houston
>
>
Dennis,

Can you show me a scenario where the .Text property of a TextBox can contain Nothing. I can't find one.

I have put the following in a button click event:

If TextBox1.Text Is Nothing Then

MsgBox("Textbox1.Text is Nothing")

Else

TextBox1.Text = Nothing

End If



I can click on the TextBox as many times as I want and I never get the message box.



BTW, I'm using VB.Net 2005.



--

Al Reid


Dennis
Guest
 
Posts: n/a
#10: Aug 31 '06

re: try Catch for empty textfield


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.
--
Dennis in Houston


"Al Reid" wrote:
Quote:
"Dennis" <Dennis@discussions.microsoft.comwrote in message news:243F095E-8052-4AD9-B591-C8C9862C4D3F@microsoft.com...
Quote:
I would use:

If not txtTitle.Text is nothing andalso txtTitle.Text.Trim.Length<=0 then
'do something if true
end if
--
Dennis in Houston
>
Dennis,
>
Can you show me a scenario where the .Text property of a TextBox can contain Nothing. I can't find one.
>
I have put the following in a button click event:
>
If TextBox1.Text Is Nothing Then
>
MsgBox("Textbox1.Text is Nothing")
>
Else
>
TextBox1.Text = Nothing
>
End If
>
>
>
I can click on the TextBox as many times as I want and I never get the message box.
>
>
>
BTW, I'm using VB.Net 2005.
>
>
>
--
>
Al Reid
>
>
>
Closed Thread