> 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...

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
> "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/>