OOP Primer

Definition:

Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance.

Example:

Assume that we want to keep track of all our contacts. In order to do that, we'll have to describe a contact object as a class that has certain properties (qualities) and methods (behaviors). For example, if we want to track each contact's name, phone number and birthday information, we should create a property for each of those attributes in our contact class (see below) and initialize them prior to instantiating the contact object:

Public Class contact

 Private pFirstName As String
 Private pLastName As String
 Private pPhone As String
 Private pDateOfBirth As DateTime
    Public Property FirstName() As String
        Get
            Return pFirstName
        End Get
        Set(ByVal value As String)
            pFirstName = value
        End Set
    End Property
 
    Public Property LastName() As String
        Get
            Return pLastName 
        End Get
        Set(ByVal value As String)
            pLastName = value
        End Set
    End Property
 
    Public Property PhoneNumber() As String
        Get
            Return pPhone 
        End Get
        Set(ByVal value As String)
            pPhone = value
        End Set
    End Property
 
    Public Property DateOfBirth() As DateTime
        Get
            Return
pDateOfBirth 
        End Get
        Set(ByVal value As DateTime)
            pDateOfBirth = value
        End Set
    End Property

 
 Public Sub New()
 
  LastName = String.Empty
  FirstName = String.Empty
  PhoneNumber = String.Empty
  DateOfBirth = DateTime.MinValue
 
 End Sub

End Class

Now that we have defined the different properties of our contact object, we can begin describing the different methods/functions that we will use with our contact object. One thing that we'll want to be able to do is to look up information of a specific contact. In order to do that, we'll have to add an additional property to our contact object that will uniquely identify that specific contact. We'll name that property ContactId and make it an Integer and initialize it to 0 in the constructor:

    Private pContactId As Integer
    Public Property ContactId() As Integer
        Get
            Return
pContactId 
        End Get
        Set(ByVal value As Integer)
            pContactId = value
        End Set
    End Property

 Public Sub New()
 
  LastName = String.Empty
  FirstName = String.Empty
  PhoneNumber = String.Empty
  DateOfBirth = DateTime.MinValue
  ContactId = 0

End Sub

Public Function GetContactInfo(ByVal ContactId As Integer) As contact

     'Code that retrieves the information and then returns the contact object goes here

End Function

 

Happy Coding!

No Comments