Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 9th, 2007, 11:12 AM
Registered User
 
Join Date: Jul 2007
Location: Bicester, Oxfordshire, United Kingdom.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validation in Data Classes

I am in the midst of building a simple application that extracts data from / saves data to an SQL Server database.

I have created a class (in this case classCompany) for loading and saving of data.

My question is, what is the best method for performing validation? For example, the class has a property called CompanyName of type string. CompanyName is not allowed to be null.

What I have thought might be a good approach is to incorporate the data validation into the Set portion of the Property. Thus, my Property looks like:

    Public Property CompanyName() As String
        Get
            Return stringCompanyName
        End Get
        Set(ByVal value As String)
            If String.IsNullOrEmpty(value) Then
                'error, this is not allowed
                MsgBox("Error Message", MsgBoxStyle.Exclamation, "Value Out of Range")
            Else
                stringCompanyName = value
            End If
        End Set
    End Property

I thought to place it here, since then the validation need only occur once in my code. Is this an acceptable way of validating data, or is there a better / neater method?

Thanks in advance.
  #2 (permalink)  
Old July 9th, 2007, 04:18 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

It might be better to raise an error:
Code:
    Public Property CompanyName() As String
        Get
            Return stringCompanyName
        End Get

        Set(ByVal Value As String)
            If String.IsNullOrEmpty(value) Then
                Err.Raise . . .  ' This will cause termination of the pro-
            End If               ' perty procedure.  No ‘Else’ required.

            stringCompanyName = value

        End Set
    End Property
My feeling is that it is best to stop a disallowed action at the moment it it attempted—as you have done here.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Validation and format zone Excel VBA 2 August 7th, 2007 02:53 PM
Data Validation in textbox rhd110 .NET Framework 1.x 1 March 26th, 2007 06:51 AM
Data Validation RollingWoodFarm Excel VBA 1 November 20th, 2006 01:12 PM
Using classes in Data Access Pages jmischo Access VBA 0 December 14th, 2004 06:11 PM
Designing data access classes in ASP.NET shawnm ASP.NET 1.x and 2.0 Application Design 3 August 4th, 2004 02:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.