Click to See Complete Forum and Search --> : [RESOLVED] Input Error Checking
Strider
Jun 2nd, 2005, 05:49 AM
when performing input error checking on forms which is best to use?
If txtbox1.text = vbnullstring then
lblErrorMsg.text = "Box 1 Empty"
End IF
or
If Request.Form.Item("txtbox1") = vbnullstring Then
lblErrorMsg.text = "Box 1 Empty"
End If
basically what is the difference
mendhak
Jun 2nd, 2005, 08:15 AM
The second method is something you'd use from the Classic ASP 3.0 days. Now, in every postback, you are basically submitting to the server, so the first method is what you can do. It makes programming your logic much easier instead of having to worry about the request field.
I say go for method 1.
Strider
Jun 2nd, 2005, 09:36 AM
again its like the sub and functions in vb.net both are there and function can do everything a sub can do. so why have subs!!!
but you still use request when wanting to get values from the previous page
nemaroller
Jun 2nd, 2005, 03:59 PM
A Sub in VB means Void is returned upon exit.
A function can return a null value, but its not the same as returning void.
The compiler can then optimize and enable restrictions on users of the method by knowing if anything is returned by the method - whether it be a function (something is returned even if its an empty object reference) or a Sub (nothing is EVER returned).
Curiously, all Subs and functions are identical other than their return type (void or something). Both can use the return keyword.
As far as your above code, you're right, its confusing. Here's why:
You are accessing a property called Text of a TextBox. The Text property will always return an empty string "" - it can never be null as long as the TextBox itself exists. The reason is the TextBox class initializes that property to an empty string - as should it should be.
Now... if you have a method where you pass a string parameter... that parameter could be null, and so you need to check for null first before testing for an empty string. That's because its never initialized like a property of a class usually is - inside the construction of the class, a private member exposed through a property is usually initialized to a string.empty ("").
Additionally, VB's String.Empty equals Nothing (Null). Which is vastly different from C#'s implementation of string.empty - in C#, string.empty does not equal null, its equals an empty string "".
VB: string.empty == nothing
C# string.empty != null
The proper way to implement your logic in VB would be:
If txtbox1.text.Length = 0
lblErrorMsg.Text = "Textbox1 is empty"
End If
If it was a string parameter passed into a method:
Private Sub DoSomething(someTextBox As TextBox)
If Not someTextBox = Nothing
'then we have an instance of a textbox, and we can be assured its Text property is initialized
If someTextBox.Text.Length = 0 lblErrorMsg.Text = "Textbox is empty"
End If
End Sub
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.