|
-
Jun 2nd, 2005, 05:49 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Input Error Checking
when performing input error checking on forms which is best to use?
VB Code:
If txtbox1.text = vbnullstring then
lblErrorMsg.text = "Box 1 Empty"
End IF
or
VB Code:
If Request.Form.Item("txtbox1") = vbnullstring Then
lblErrorMsg.text = "Box 1 Empty"
End If
basically what is the difference
Barry
Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
.NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0
SQL Server 2005/2000/SQL Server CE 2.0
If you like, rate this post
Compact Framework for Beginners
-
Jun 2nd, 2005, 08:15 AM
#2
Re: Input Error Checking
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.
-
Jun 2nd, 2005, 09:36 AM
#3
Thread Starter
Fanatic Member
Re: Input Error Checking
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
Barry
Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
.NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0
SQL Server 2005/2000/SQL Server CE 2.0
If you like, rate this post
Compact Framework for Beginners
-
Jun 2nd, 2005, 03:59 PM
#4
I wonder how many charact
Re: Input Error Checking
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:
VB Code:
If txtbox1.text.Length = 0
lblErrorMsg.Text = "Textbox1 is empty"
End If
If it was a string parameter passed into a method:
VB Code:
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
Last edited by nemaroller; Jun 2nd, 2005 at 04:05 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|