Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Using Methods with Object Oriented Databas...
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 
Moblin 
JMSL Numerical Library 
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

Using Methods with Object Oriented Database Development with VB.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 16
    2006-07-19

    Table of Contents:
  • Using Methods with Object Oriented Database Development with VB.NET 2005
  • Developing methods with parameters having default values
  • Dealing with method overloading to attain more flexibility
  • Developing methods with parameters as objects
  • Developing methods which return objects

  • 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


    Using Methods with Object Oriented Database Development with VB.NET 2005 - Dealing with method overloading to attain more flexibility


    (Page 3 of 5 )

    Let us work with the method "add" now. It is defined as follows:

        Public Sub add()
            Dim cn As New SqlConnection("Data Source=.sql2k5;initial
    catalog=sample;user id=sa;password=eXpress2005"
    )
            Dim cmd As New SqlCommand
            Try
                With cmd
                    .CommandText = "insert into sample.dbo.emp values ('" &
    m_empno & "','" & m_ename & "," & m_sal & "," & m_deptno & ")"
                    .Connection = cn
                    .Connection.Open()
                    .ExecuteNonQuery()
                End With
            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 Sub

    The following is the code needed to execute the above method:

        Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnAdd.Click

            Me.lblErrMsg.Text = ""
            Try
                Dim ep As New Emp
                With ep
                    .m_empno = Me.txtEmpno.Text
                    .m_deptno = Me.txtDeptno.Text
                    .m_ename = Me.txtEname.Text
                    .m_sal = Me.txtSal.Text
                    .add()
                End With
            Catch ex As Exception
                Me.lblErrMsg.Text = ex.Message
            End Try
        End Sub

    From the above code, you can observe that I am assigning all the values to the fields and then only calling the method "add." But, let us consider that I would like to have one more method with the name "add" (for flexibility) which accepts all the fields as parameters.  The following would be another method named "add" (apart from the old method "add").

    PublicSub add(ByVal empno As String, ByVal ename As String, ByVal sal As
    Double, ByVal deptno As Integer)
            m_empno = empno
            m_ename = ename
            m_sal = sal
            m_deptno = deptno
            add()
        End Sub

    The following would be the code needed to execute the above method:

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

    Now, you can observe that I defined two methods with the same name ("add"), but with a difference in parameters.  You can call the two methods individually at any time. Depending on the parameters, it would automatically identify the method to execute. This is called "method overloading."

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello guys! this is my next contribution on database oriented OOPS with VB.NET 2005....
     

    VISUAL BASIC.NET ARTICLES

    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET (F...
    - 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 ...





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