Tuesday 2 August 2011

Data Validation Class

After having several problems regarding null I have decided to do the right thing and create a Data Validation Class. What this class does is check each data before I use it for null.

If this is the first time you hear about null there is some basic things to learn:

  • A null value is a value that doesn't refer to any object. It's the default value of reference-type variables (e.g., class, interface, and delegate).
  • The null keyword is a literal that represents a null reference with the .NET environment. 
  • There is a difference between null and '0' the value.
  • Null is nothing where '0' is assigned as a value. 
To start out create a new class in your project, in my case my class is called DataVal.vb
Working with a database I prefered to assign '0' to all my null values if they are numeric.
I later on run a query that searches my tables for the '0' records and then remove them.
"DELETE * From Table WHERE Field = 0 "
This however depends on the function of your tables and program.

This is the code used in my class:

Public Class DataVal
    Public Function Validate(ByVal input)
        If input = Nothing Then
            input = "0"
            Return input
        End If
        Return input
    End Function
End Class

DV = DataValidate class....
Use this function to set the value of your object, eg. txtName.Text = (DV.Validate(txtName.Text))
It works for integers, doubles basically all data types.

Please comment your solution to validate data.


4 comments:

  1. The way you have expressed your thoughts in this blog was nice with clear explanation. Thanks for sharing.
    web designing institute
    web designing training

    ReplyDelete
  2. I am always searching online for articles that can help. There is obviously a lot to know about this. I think you made some good points.
    PHP Training in Chennai
    PHP Course in Chennai

    ReplyDelete
  3. Subscription boxes are a type of boxes which are delivered to the regular customers in order to build goodwill of the brand. They are also a part of the product distribution strategy. As a woman, you should subscribe to these boxes to bless yourself with a new and astonishing box of happiness each month. visit mysubscriptionsboxes

    ReplyDelete
  4. I was extremely pleased to find this great site. I need to to thank you for your time for this wonderful read!! I definitely loved every part of it and i also have you saved to fav to look at new information on your blog.
    tech gadget

    ReplyDelete