Visual Basic .NET/Classes

From Wikibooks, open books for an open world
Jump to: navigation, search

Introduction[edit]

The Class concept is the main foundation of Object Oriented Programming. In a world of Graphical User Interfaces and more complex programs, classes have become a very important part of programming.

To create a class, in the Solutions explorer, after a right click on the application, Add, then Class. Or select "Add Class..." from the Project menu of the menu bar.

Terminology[edit]

object: A unit that has its own properties and methods for a user to use at his or her discretion.

encapsulation: Lets the user of the class control the data and operations of a class that can be seen from other classes.

property: Represents a data value associated with an instance.

method: An operation that can be performed by the class.

constructor: The method that is called when an object of the class has been instantiated.

field: A variable at the class level.

Fields[edit]

Fields are variables that are inside a class but not inside functions, sub routines and properties. These variables are also not allowed to be called outside a class. Declaring these variables are simple, as shown in this segment of a class:

Public Class customer
   Private Name As String
   Private Address As String
   Private Age As Integer
   ...

The class can call these variables up easily with "Me" calling. Look at this segment of a class to understand how:

...
Public Function GetName()
   Return Me.Name
End Function
...

Trying to call up a private field outside of a class will not work.

Constructors[edit]

When a new object of a class is declared, we can initialize the fields of a class. For example, look at this segment of a class:

Public Class customer
   Public Name As String
   Private Address As String
   Private Id_number As String
   ' Constructor with parameters
   Public Sub New(ByVal name As String)
      Me.Name = name
   End Sub
   ' Constructor with no parameters
   Public Sub New()
   End Sub
   ...

Technically, a constructor with no parameters is called an Empty Constructor or a Default Constructor. A Constructor that has parameters is called a Custom Constructor.

Properties[edit]

Properties are divided into two categories: getters and setters. A getter returns a value from a class, much like how a function works and a setter sets a value into the class.

  Public Property name() As String
    Get
        Return Me.Name
    End Get
    Set(ByVal value As String)
        Me.Name = value
    End Set
  End Property

Because this class property is "Public", we can access it from outside the class. If it was "Private", it would be the complete opposite.

Methods[edit]

Methods are pretty much sub routines specific to a class. These can be called up as many or as little times as the programmer desires.

Instantiation[edit]

We use the keyword "New":

  Dim customer1 = New customer("John Doe")
  MsgBox(customer1.name)

Example Class[edit]

This is just an example of a class using all the above techniques:

Public Class customer
   ' Fields
   Private Name As String
   Private Address As String
   Private Age As Integer
   Private Items_Bought As Integer
   ' Constructor with a parameter
   Public Sub New(ByVal value As String)
      Me.Name = value
   End Sub
   ' Default Constructor
   Public Sub New()
   End Sub
   ' Name Properties
   Public Property name() As String
      Get
         Return Me.Name
      End Get
      Set(ByVal value As String)
         Me.Name = value
      End Set
   End Property
   ' Address Properties
   Public Property Address() As String
      Get
         Return Me.Address
      End Get
      Set(ByVal value As String)
         Me.Address = value
      End Set
   End Property
   ' Age Properties
   Public Property Age() As Integer
      Get
         Return Me.Age
      End Get
      Set(ByVal value As String)
         Me.Age = value
      End Set
   End Property
   ' Items_Bought Properties
   Public Property Items_Bought() As Integer
      Get
         Return Me.Items_Bought
      End Get
      Set(ByVal value As String)
         Me.Items_Bought = value
      End Set
   End Property
End Class