Home All Groups Group Topic Archive Search About

try Catch for empty textfield

Author
29 Aug 2006 10:37 AM
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

Author
29 Aug 2006 11:22 AM
Stuart Nathan
try =""
Author
29 Aug 2006 12:15 PM
Herfried K. Wagner [MVP]
"John Devlon" <johndev***@hotmail.com> schrieb:
> 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/>
Author
29 Aug 2006 2:06 PM
Jim Wooley
Show quote Hide quote
> "John Devlon" <johndev***@hotmail.com> schrieb:
>
>> 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
Author
29 Aug 2006 2:15 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Jim Wooley" <jimNOSPAMwooley@hotmail.com> schrieb:
>>> 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/>
Author
29 Aug 2006 12:19 PM
Oenone
John Devlon wrote:
> 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
///

--

(O)enone
Author
30 Aug 2006 1:24 AM
Greg
Show quote Hide quote
"John Devlon" <johndev***@hotmail.com> wrote in message
news:7OUIg.47633$a6.711718@phobos.telenet-ops.be...
> 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.
Author
31 Aug 2006 12:01 AM
Dennis
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


Show quoteHide quote
"Greg" wrote:

> "John Devlon" <johndev***@hotmail.com> wrote in message
> news:7OUIg.47633$a6.711718@phobos.telenet-ops.be...
> > 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.
>
>
>
Author
31 Aug 2006 11:19 AM
Al Reid
"Dennis" <Den***@discussions.microsoft.com> wrote in message news:243F095E-8052-4AD9-B591-C8C9862C4D3F@microsoft.com...
> 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
Author
31 Aug 2006 11:03 PM
Dennis
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


Show quoteHide quote
"Al Reid" wrote:

> "Dennis" <Den***@discussions.microsoft.com> wrote in message news:243F095E-8052-4AD9-B591-C8C9862C4D3F@microsoft.com...
> > 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
>
>
>