A Class statement defines a new data type. A class is a fundamental building block of object-oriented programming (OOP).
Class
only at namespace or module level.Implements
Statement, you must implement every member defined by every interface you specify in interface names. An exception to this is the reimplementation of a base class member.A class is nothing but a group of different data members or objects with the same properties, processes, events of an object, and general relationships to other member functions. It is defined by the keyword class
, followed by an identifier (name) of the class and a set of data members and methods in a separate code block.
Public Class Customer
Public Property Id As Integer
Public Property Name As String
Public Property Address As String
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String)
Me.Id = id
Me.Name = name
Me.Address = address
End Sub
Public Sub Print()
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", Me.Id, Me.Name, Me.Address)
End Sub
End Class
The class Customer
defines the properties Id
, Name
, and Address
. The two constructors are defined for creating instances of the class Customer
, respectively with and without parameters, and a method of the class Print()
.
An object is a combination of code and data that can be treated as a unit. An object can be a piece of an application, like control or a form. An entire application can also be an object.
Let's consider the following example where we create an object of the Customer
class using the parameterless constructor, initializes the object, and print the object values by calling the Print()
method.
Dim customer As Customer = New Customer()
customer.Id = 1
customer.Name = "Andy"
customer.Address = "22 wall street"
customer.Print()
In the above example, you can see that we have created a customer
object for the Customer
class. Now you can use the customer
object to access all the data members and functions of the Customer
class.
You can also use the parameterized constructor for object initialization as shown below.
Dim customer As Customer = New Customer(2, "John", "11 wall street")
customer.Print()
Let's run the above code, and it will print the following output on the console window.
Id: 1, Name: Andy, Address: 22 wall street