Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / August 2006

Tip: Looking for answers? Try searching our database.

try Catch for empty textfield

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Devlon - 29 Aug 2006 11:37 GMT
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 - 29 Aug 2006 12:22 GMT
Herfried K. Wagner [MVP] - 29 Aug 2006 13:15 GMT
"John Devlon" <johndevlon@hotmail.com> schrieb:
> I would like to check if a text field is empty; I'm using this code ...
>
[quoted text clipped - 7 lines]
>
> unfortunately, the exception doesn't work...

'Trim' does not throw an exception.

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

Signature

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

Jim Wooley - 29 Aug 2006 15:06 GMT
> "John Devlon" <johndevlon@hotmail.com> schrieb:
>
[quoted text clipped - 17 lines]
> 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
Herfried K. Wagner [MVP] - 29 Aug 2006 15:15 GMT
"Jim Wooley" <jimNOSPAMwooley@hotmail.com> schrieb:
>>> I would like to check if a text field is empty; I'm using this code
>>> ...
[quoted text clipped - 19 lines]
>  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'.

Signature

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

Oenone - 29 Aug 2006 13:19 GMT
> 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
///

Signature

(O)enone

Greg - 30 Aug 2006 02:24 GMT
> Hi,
>
[quoted text clipped - 15 lines]
>
> john

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

Cheers.
Dennis - 31 Aug 2006 01:01 GMT
I would use:

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

Dennis in Houston

> > Hi,
> >
[quoted text clipped - 19 lines]
>
> Cheers.
Al Reid - 31 Aug 2006 12:19 GMT
> I would use:
>
> If not txtTitle.Text is nothing andalso txtTitle.Text.Trim.Length<=0 then
>    'do something if true
> end if

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 - 01 Sep 2006 00:03 GMT
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.
Signature

Dennis in Houston

> > I would use:
> >
[quoted text clipped - 25 lines]
>
> Al Reid

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.