Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Properties and Object Oriented Database De...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Properties and Object Oriented Database Development with VB.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    2006-07-26

    Table of Contents:
  • Properties and Object Oriented Database Development with VB.NET 2005
  • The class “Emp” rewritten with properties
  • Accessing the object properties from the form
  • Using read-only properties to deal with error messages

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Properties and Object Oriented Database Development with VB.NET 2005 - Accessing the object properties from the form


    (Page 3 of 4 )

    Modifying the class as in the previous section would cause errors to be thrown in your form code.  You need to modify the form code as follows to work with properties:

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
            Me.lblErrMsg.Text = ""
            Try
                Dim ep As New Emp
                Dim objReturned As Emp
                objReturned = ep.getEmployee(Me.txtEmpno.Text)
                Me.txtDeptno.Text = objReturned.deptno
                Me.txtEname.Text = objReturned.ename
                Me.txtSal.Text = objReturned.sal
            Catch ex As Exception
                Me.lblErrMsg.Text = ex.Message
            End Try
        End Sub

        Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            Me.lblErrMsg.Text = ""
            Try
                Dim objParam As New Emp
                With objParam
                    .deptno = Me.txtDeptno.Text
                    .empno = Me.txtEmpno.Text
                    .ename = Me.txtEname.Text
                    .sal = Me.txtSal.Text
                End With
                Dim ep As New Emp
                ep.add(objParam)
            Catch ex As Exception
                Me.lblErrMsg.Text = ex.Message
            End Try
        End Sub

    Developing read-only/write-only properties

    There will be times when you need only read-only/write-only properties.  The best example would be something like the following statement:

    a = Me.ListBox1.Items.Count

    If I try to write the same statement shown above as something like the following, it would give an error:

    Me.ListBox1.Items.Count = a

    That means we can only read from the property “Count,” but not assign any value to it, which is what is called a “read-only” property. 

    Let us now try to determine a read-only property called “EmployeeCount,” which returns the number of employees existing in the table.  The code would be something like the following:

    Public ReadOnly Property EmployeeCount() As Integer
            Get
                Dim cn As New SqlConnection("Data Source=.sql2k5;initial catalog=sample;user id=sa;password=eXpress2005")
                Dim cmd As New SqlCommand("select count(*) from sample.dbo.emp", cn)
                Try
                    Dim n As Integer
                    cmd.Connection.Open()
                    n = cmd.ExecuteScalar
                    Return n
                Catch ex As Exception
                    Throw New Exception(ex.Message)
                Finally
                    If cmd.Connection.State = ConnectionState.Open Then
                        cmd.Connection.Close()
                    End If
                    cmd.Dispose()
                    cn.Dispose()
                End Try
            End Get
        End Property

    You can observe that the above property doesn’t have any “set” part and the property declaration is given with “readonly.” To use this property, I added a simple label called “lblNoOfEmployees” to the form and added the following code:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Dim ep As New Emp
                Me.lblNoOfEmployees.Text = "No. of employees: " & ep.EmployeeCount
            Catch ex As Exception
                Me.lblErrMsg.Text = ex.Message
            End Try
        End Sub

    Very similar to the above read-only property, we also have a write-only property.  The write-only property is used only to assign a value, but not for retrieving a value.  In the case of the write-only property, we will have only a “set” part without a “get” part and the declaration is given with “writeonly.” 

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello guys. Now you can dip into developing properties in Visual Basic.NET....
     

    VISUAL BASIC.NET ARTICLES

    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...
    - Programmatically Controlling Windows Fax Ser...
    - Focusing on Forms and Menus in Visual Basic





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway