VB Classes ISYS 512.

Slides:



Advertisements
Similar presentations
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Advertisements

VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Creating Custom Controls ISYS 512/812. Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface.
VB Classes - 2 ISYS 573. Creating an Array of Objects Dim emps(2) As emp Dim i As Integer For i = 0 To emps.GetUpperBound(0) emps(i) = New emp() Next.
Authenticating Users in an ASP.NET Application. Web Site Administration Tool From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration.
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart.
ASP.NET and ADO.NET. ASP.NET Server Controls Intrinsic Controls: These controls correspond to their HTML counterparts. –Ex. Textbox, listbox, button,
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
Coding ADO.NET Objects: Connection, Command, DataReader.
Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows.
ASP.Net AJAX. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net, etc.
ASP.NET and ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
Web Site Security ISYS 512/812. Authentication Authentication is the process that determines the identity of a user. Web.config file – node Options: –Windows:
Coding ADO.NET Objects: Connection, Command, DataReader.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Working with Session and Application Objects. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to Web Application Development with.Net and Web Service ISYS 350.
ASP.NET and ADO.NET. Bind the DataReader to a DataGrid Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn.
VB Classes BICS546. Adding a Class to a Project Project/Add Class Class will be saved in a.CLS file Steps: –Adding properties Declare Public variables.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
Coding ADO.NET Objects: Connection, Command, DataReader.
Inheritance and User-Defined Controls. Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface.
VB.NET Classes ISYS 812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Working with Session. Postback and Variables Variables declared in a web page including ADO.Net objects may be reinitialized and lose their values.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
VB.NET Classes ISYS 546. Classes A class is program structure that defines a user-defined data type that are used to create objects. An instance of a.
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
Introduction to Web Service ISYS 512. Web Service XML Web Service Web services are classes that are stored on the web which can instantiate and use in.
Introduction to ADO.NET. ADO.NET Objects Data Set.NET Applications Data Reader Command Object Connection Object Managed Data Provider (OLEDB) Database.
ASP and HTML. Anchor Tag testAnchorTag Demo: ASPNet/ASPNETProdListSelf.ASPX.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Introduction to XML and Web Service ISYS 464. HTML vs XML HTML is a language specifically designed for displaying information in browser. It doesn’t carry.
Java Classes ISYS 350. Introduction to Classes Two basic uses of class: – 1. A class is a way to organize functions (methods) to perform calculations.
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Database Handling Classes ISYS 475. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies.
ASP.Net and HTML. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data.
Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Command Object’s ExecuteNonQuery Method ISYS 512.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Coding ADO.NET Objects: Connection, Command, DataReader.
Objects andVB Classes ISYS 350. What Is an Object? Objects are key to understanding object-oriented technology. There are many examples of real-world.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
C# Classes ISYS 512. Introduction to Classes A class is the blueprint for an object. –It describes a particular type of object. –It specifies the properties.
Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out.
Coding ADO.NET Objects: Connection, Command, DataReader.
Object-Oriented Programming: Classes and Objects.
Coding ADO.NET Objects: Connection, Command, DataReader
ASP.NET and ADO.NET.
Object-Oriented Programming: Classes and Objects
CIS16 Application Development and Programming using Visual Basic.net
Coding ADO.NET Objects: Connection, Command, DataReader
Database Handling Class and Service
ASP.NET and ADO.NET.
Working with Session and Application Objects
Introduction to ADO.NET
Command Object’s ExecuteNonQuery Method
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

VB Classes ISYS 512

Adding a Class to a Project Project/Add Class *** MyClass is a VB keyword. Steps: Adding properties Declare Public variables in the General Declaration section Property procedures: Set / Get Adding methods Adding events, exceptions

Anatomy of a Class Module Exposed Part Hidden Part Public Variables & Property Procedures Private Variables Public Procedures & Functions Private Procedures & Functions Private variables and procedures can be created for internal use. Encapsulation

Class Code Example Public Eid As String Public Ename As String Public salary As Double Public Function tax() As Double tax = salary * 0.1 End Function

Using a Class Define a class variable using New Example: Dim MyEmp As New Emp

Creating Property with Property Procedures Implementing a property with a public variable the property value cannot be validated by the class. We can create read-only, write-only, or write-once properties with property procedure. Steps: Declaring a private class variable to hold the property value. Writing a property procedure to provide the interface to the property value.

Private pvEid As String Private pvEname As String Private pvSalary As Double Public Property eid() As String Get eid = pvEid End Get Set(ByVal Value As String) pvEid = Value End Set End Property Public Property eName() As String eName = pvEname pvEname = Value Public Property Salary() As Double Salary = pvSalary Set(ByVal Value As Double) pvSalary = Value

Property Procedure Code Example Public Class Emp2 Public SSN As String Public Ename As String Public DateHired As Date Private hiddenJobCode As Long Public Property JobCode() Set(ByVal Value) If Value < 1 Or Value > 4 Then hiddenJobCode = 1 Else hiddenJobCode = Value End If End Set Get JobCode = hiddenJobCode End Get End Property End Class

How the Property Procedure Works? When the program sets the property, the property procedure is called and the code between the Set and End Set statements is executed. The value assigned to the property is passed in the Value argument and is assigned to the hidden private variable. When the program reads the property, the property procedure is called and the code between the Get and End Get statements is executed.

Implementing a Read-Only Property Declare the property procedure as ReadOnly with only the Get block. Ex. Create a YearsEmployed property from the DateHired property: Public ReadOnly Property YearsEmployed() As Long Get YearsEmployed = Now.Year - DateHired.Year End Get End Property Note: It is similar to a calculated field in database.

Overloading A class may have more than one methods with the same name but a different argument list (with a different number of parameters or with parameters of different data type), different parameter signature.

Method Overloading Using the Overloads Keyword Public Overloads Function tax() As Double tax = salary * 0.1 End Function Public Overloads Function tax(ByVal sal As Double) As Double tax = sal * 0.1

Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface and behaviors. The original class is known as the base class, super class, or parent class. The inherited class is called a subclass, a derived class, or a child class.

Employee Super Class with Three SubClasses All employee subtypes will have emp nbr, name, address, and date-hired Each employee subtype will also have its own attributes

Inheritance Example Public Class Emp Public Eid As String Public Ename As String Public salary As Double Public Function tax() As Double tax = salary * 0.1 End Function End Class Public Class secretary Inherits Emp Public WordsPerMinute As Integer

Database Handling Classes ADO.Net Objects Database Classes Data Source Forms Reports

Single-Record-Handling Classes Retrieves a single record from the database and makes it available to your application in the form of an object. The fields in the record are exposed as the object’s properties. Any actions performed by the data (updates, calculations, etc.) are exposed as the object’s methods.

Single-Record-Handling Class Example Imports System.Data.OleDb Public Class Customer Public cid As String Public CName As String Public City As String Public Rating As String Private hiddenexist As Boolean Private cn As OleDb.OleDbConnection Public ReadOnly Property RecExist() As Boolean Get RecExist = hiddenexist End Get End Property

Dim objConn As New OleDbConnection(strConn) Public Sub getData(ByVal SearchID As String) Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where cid = '" & SearchID & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() If objDataReader.Read() = False Then hiddenexist = False Else hiddenexist = True cid = objDataReader("cid") CName = objDataReader("CName") City = objDataReader("City") Rating = objDataReader("Rating") End If objConn.Close() End Sub

Public Sub SaveNew() Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\SalesDB2007.accdb" Dim objConn As New OleDbConnection(strConn) objConn.Open() Dim strSQLInsert As String strSQLInsert = "Insert into Customer values ('" strSQLInsert = strSQLInsert & cid & "','" & CName & "','" strSQLInsert = strSQLInsert & City & "','" & Rating & "')" Dim objCommInsert As New OleDbCommand(strSQLInsert, objConn) objCommInsert.ExecuteNonQuery() objConn.Close() End Sub

Using the SaveNew Method to Add A New Customer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim newCust As New Customer() newCust.cid = TextBox1.Text newCust.CName = TextBox2.Text newCust.City = TextBox3.Text newCust.Rating = TextBox4.Text newCust.SaveNew() TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" End Sub

Modeling 1:M Relation with Classes Employee EID Ename Dependents Department DID Dname Employees Customer CID Cname Orders

ArrayList ArrayList is a data structure used to store a set of values. Its capacity is automatically expanded as needed. Values stored in an arraylist do not have to be the same data type. Flexibility when inserting/deleting elements.

ArrayList Properties & Methods Define an arraylist: Dim myArrayList As New ArrayList() Properties:Count, Item, etc. myArrayList.Item(0) 0-based index Methods: Clear, Add, Insert, Remove, RemoveAt, Contains, IndexOf, etc.

ArrayList Demo Dim testArrayList As New ArrayList() Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim f2 As New Form2() testArrayList.Add("David") testArrayList.Add(20) TextBox1.Text = testArrayList.Item(0) TextBox2.Text = testArrayList.Item(1).ToString End Sub

For Each Loop with ArrayList Dim testArrayList As New ArrayList() testArrayList.Add("David") testArrayList.Add(20) testArrayList.Add(Fruits) Dim myObj As Object For Each myObj In testArrayList MessageBox.Show(myObj.GetType.ToString) Next

Implementing a 1:M Relationship With ArrayList Class Customer Public cid As String Public cname As String Public city As String Public rating As String Public orders As New ArrayList Methods: GetData GetOrders Class Order Public OID As String Public Odate As Date Public SalesPerson As String

Customer Class Public Class Customer Public cid As String Public cname As String Public city As String Public rating As String Public orders As New ArrayList Private hiddenexist As Boolean Public Sub getData(ByVal SearchID As String) Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from customer where cid = '" & SearchID & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() If objDataReader.Read() = False Then hiddenexist = False MessageBox.Show("Record does not exist") Else hiddenexist = True cid = objDataReader("cid") cname = objDataReader("CName") city = objDataReader("City") rating = objDataReader("Rating") End If objConn.Close() End Sub

GetOrders Method Public Sub getOrders(ByVal SearchID As String) Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select * from orders where cid = '" & SearchID & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Do While objDataReader.Read() = True Dim ord As New Order() ord.oid = objDataReader("oid") ord.CID = objDataReader("cid") ord.Odate = objDataReader("odate") ord.SalesPerson = objDataReader("salesPerson") orders.Add(ord) Loop If orders.Count = 0 Then MessageBox.Show("customer has no order") End If objConn.Close() End Sub

Order Class Public Class Order Private pvOID As String Private pvCID As String Private pvOdate As Date Private pvSalesPerson As String Public Property OID() As String Get oid = pvOID End Get Set(ByVal value As String) pvOID = value End Set End Property Public Property CID() As String CID = pvCID pvCID = value Public Property Odate() As Date Odate = pvOdate Set(ByVal value As Date) pvOdate = value Public Property Salesperson() As String Salesperson = pvSalesPerson pvSalesPerson = value End Class

Binding Datagrid to an ArrayList DataGridView1.DataSource = myCustomer.orders Members of the arraylist are instances of same class. The properties of the class are defined by property procedure.

Example

Code Example Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "select cid from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Do While objDataReader.Read() = True ListBox1.Items.Add(objDataReader("cid")) Loop objConn.Close() End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim myCustomer As New Customer myCustomer.getData(ListBox1.SelectedItem) TextBox1.Text = myCustomer.cname TextBox2.Text = myCustomer.rating myCustomer.getOrders(myCustomer.cid) DataGridView1.DataSource = myCustomer.orders

Difference between Assembly and Class A class defined in a project is available to that project only. Once a class is compiled in an assembly it can be used by any projects. To create an assembly: Start a Class Library project

Steps to Create An Assembly Start a Class Library project Create classes You can also use existing classes defined in other projects by Project/Add Existing Item Save project Select Build/Build to compile the code. When the class library is compiled successfully, an assembly is created and stored in the project’s Bin/Release folder. Example: A testClassLib project is created in C:\VS2008Examples, then the assembly is found in: C:\VS2008Examples\testClassLib\testClassLib\bin\Release

Using the Assembly Reference the assembly: Project/Add Reference and use the Browse button to select the assembly. Import the assembly. Global import: Project property windows/References Local import Using the Imports statement

Code Using Assembly Imports MyClassDemo Public Class Form1 Dim myButton As New Button Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myCls As New DemoCls1 MessageBox.Show(myCls.myName) End Sub

Changes to Assembly Old projects referencing the assembly will get the latest version of the assembly. Compatible changes: Changes to assembly that will not break the older projects. Examples: Adding a property, adding a method Incompatible changes Changes to assembly that will break the older projects. Deleting or renaming a property or a method