|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
try Catch for empty textfieldI 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 "John Devlon" <johndev***@hotmail.com> schrieb: 'Trim' does not throw an exception.> 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... \\\ 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/>
Show quote
Hide quote
> "John Devlon" <johndev***@hotmail.com> schrieb: Actuall, String.Trim can throw an exception. Consider the following:> >> 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 > /// 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
Show quote
Hide quote
"Jim Wooley" <jimNOSPAMwooley@hotmail.com> schrieb: That's true, but I am using 'Microsoft.VisualBasic.Strings.Trim' instead of >>> 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) '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/> John Devlon wrote:
> I would like to check if a text field is empty; I'm using this code That's because reading and trimming an empty string from a textbox doesn't [...] > unfortunately, the exception doesn't work... 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
Show quote
Hide quote
"John Devlon" <johndev***@hotmail.com> wrote in message If Trim(txtTitle.Text) = "" Then MsgBox ("error")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 Cheers. I would use:
If not txtTitle.Text is nothing andalso txtTitle.Text.Trim.Length<=0 then 'do something if true end if -- Show quoteHide quoteDennis in Houston "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. > > > "Dennis" <Den***@discussions.microsoft.com> wrote in message news:243F095E-8052-4AD9-B591-C8C9862C4D3F@microsoft.com... 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 > > 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 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. -- Show quoteHide quoteDennis in Houston "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 > > >
Unicode conversion
Net.WebRequest - Close Connection Please explain the difference between close, dispose, = nothing on sqlClient. Returning an array from a function? Visual Studio beginner questions Passing System.Enum as a parameter type... Help! VB Express & Excel automation ? First Report in VB2005 - Print Preview moving controls with in an image Formato numerico |
|||||||||||||||||||||||