Implementing OOP to Develop Database Oriented Applications using VB.NET 2005 - Developing a class which holds an entire row in a table (Page 2 of 6 )
Every row in a table (especially for lookup tables) must be available in the form of an object, according to the OOP concept. To hold these types of objects, we need to have a collection class to store all those objects.
Before creating the collection class, let us go through the main class which can hold a row of data. The following is the class which holds employee-related information.
PublicClass Employee
Private _empno As Integer
Private _ename As String
Private _sal As Double
Private _deptno As Integer
Public Property Empno() As Integer
Get
Return _empno
End Get
Set(ByVal value As Integer)
If _empno < 0 Then
Throw New Exception("Invalid empno")
End If
_empno = value
End Set
End Property
Public Property Ename() As String
Get
Return _ename
End Get
Set(ByVal value As String)
_ename = value.ToUpper
End Set
End Property
Public Property Sal() As Double
Get
Return _sal
End Get
Set(ByVal value As Double)
If _sal < 0 Then
Throw New Exception("Invalid salary")
End If
_sal = value
End Set
End Property
Public Property Deptno() As Integer
Get
Return _deptno
End Get
Set(ByVal value As Integer)
If _deptno < 0 Then
Throw New Exception("Invalid deptno")
End If
_deptno = value
End Set
End Property
EndClass
For convenience, I even added few constructors (as follows) to the above class:
PublicSub New()
End Sub
Public Sub New(ByVal empno As Integer, ByVal ename As String, ByVal sal As Double, ByVal deptno As Integer)
_empno = empno
_ename = ename
_sal = sal
_deptno = deptno
End Sub
Public Sub New(ByVal e As Employee)
_empno = e.Empno
_ename = e.Ename
_sal = e.Sal
_deptno = e.Deptno
End Sub
Next: Developing a class which holds a set of rows belonging to the same table >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee