Click here to Skip to main content
15,892,768 members
Articles / Programming Languages / Visual Basic

Business object property validation

Rate me:
Please Sign up or sign in to vote.
3.43/5 (9 votes)
16 May 2008CPOL5 min read 44.2K   238   34  
How to set and validate properties in a single line of code.
'Copyright � 2006 Edward Steward

Option Strict Off
Option Explicit On 

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtName As System.Windows.Forms.TextBox
    Friend WithEvents lblResult As System.Windows.Forms.Label
    Friend WithEvents cmdTest As System.Windows.Forms.Button
    Friend WithEvents lblErrMsg As System.Windows.Forms.Label
    Friend WithEvents pnlStatus As System.Windows.Forms.Panel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.cmdTest = New System.Windows.Forms.Button
        Me.txtName = New System.Windows.Forms.TextBox
        Me.lblResult = New System.Windows.Forms.Label
        Me.lblErrMsg = New System.Windows.Forms.Label
        Me.pnlStatus = New System.Windows.Forms.Panel
        Me.pnlStatus.SuspendLayout()
        Me.SuspendLayout()
        '
        'cmdTest
        '
        Me.cmdTest.Location = New System.Drawing.Point(104, 80)
        Me.cmdTest.Name = "cmdTest"
        Me.cmdTest.TabIndex = 1
        Me.cmdTest.Text = "Test"
        '
        'txtName
        '
        Me.txtName.Location = New System.Drawing.Point(8, 8)
        Me.txtName.Name = "txtName"
        Me.txtName.Size = New System.Drawing.Size(256, 20)
        Me.txtName.TabIndex = 0
        Me.txtName.Text = ""
        '
        'lblResult
        '
        Me.lblResult.Location = New System.Drawing.Point(16, 112)
        Me.lblResult.Name = "lblResult"
        Me.lblResult.Size = New System.Drawing.Size(264, 144)
        Me.lblResult.TabIndex = 2
        '
        'lblErrMsg
        '
        Me.lblErrMsg.BackColor = System.Drawing.Color.Transparent
        Me.lblErrMsg.ForeColor = System.Drawing.Color.White
        Me.lblErrMsg.Location = New System.Drawing.Point(8, 40)
        Me.lblErrMsg.Name = "lblErrMsg"
        Me.lblErrMsg.Size = New System.Drawing.Size(256, 16)
        Me.lblErrMsg.TabIndex = 1
        '
        'pnlStatus
        '
        Me.pnlStatus.Controls.Add(Me.lblErrMsg)
        Me.pnlStatus.Controls.Add(Me.txtName)
        Me.pnlStatus.Location = New System.Drawing.Point(8, 8)
        Me.pnlStatus.Name = "pnlStatus"
        Me.pnlStatus.Size = New System.Drawing.Size(272, 64)
        Me.pnlStatus.TabIndex = 0
        '
        'Form1
        '
        Me.AcceptButton = Me.cmdTest
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.lblResult)
        Me.Controls.Add(Me.cmdTest)
        Me.Controls.Add(Me.pnlStatus)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.pnlStatus.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click

        'Declare and instantiate the BizO business object
        Dim o As BizO
        o = New BizO

        Try
            'Set the value of the Name property in the business object
            o.PROPS.SetValue("Name", txtName.Text)

            'No exceptions/errors were detected during the value set
            'so report the success to the user
            lblErrMsg.Text = "Input tested successfully!!"
            pnlStatus.BackColor = Color.Green

        Catch ex As Exception
            'An exception was thrown from within the PROPS object (inside
            'the business object
            'Report the error message to the user
            lblErrMsg.Text = ex.Message
            pnlStatus.BackColor = Color.Red
        End Try

        'Display the results to the user
        lblResult.Text = "PROPS.GetValue: " + o.PROPS.GetValue("Name")

        'Declare a local instance of structTEST structure
        Dim x As Structures.structTEST
        'and fill it with ALL the data from the PROPS object
        x = o.PROPS.GetAllData

        'Display the results to the user
        lblResult.Text += vbCrLf + vbCrLf + "Local structure: " + x.Name

    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions