VB.NET Classes
Overview of Classes
Classes are the fundamental building blocks of object-oriented programming (OOP) in VB.NET. They serve as blueprints for creating objects, encapsulating data (fields or properties) and behavior (methods or functions) into a single unit.
A class defines the structure and behavior of objects. When you create an object from a class, it's called an instance of the class. Each instance can have its own unique set of data, but they all share the same behavior defined by the class.
Defining a Class
You define a class using the Class
keyword, followed by the class name, and end it with the End Class
statement.
Public Class Car
' Fields (member variables)
Public Make As String
Public Model As String
Public Year As Integer
' Constructor (special method for initializing objects)
Public Sub New(make As String, model As String, year As Integer)
Me.Make = make
Me.Model = model
Me.Year = year
End Sub
' Method (behavior)
Public Sub DisplayInfo()
Console.WriteLine($"This is a {Me.Year} {Me.Make} {Me.Model}.")
End Sub
' Property (controlled access to fields)
Private _color As String
Public Property Color() As String
Get
Return _color
End Get
Set(value As String)
_color = value
End Set
End Property
End Class
Class Members
Classes can contain various members:
- Fields: Variables that hold data for the object (e.g.,
Make
,Model
). - Properties: Special methods that provide controlled access to a class's fields, often used for validation or calculated values.
- Methods: Functions or Subroutines that define the actions an object can perform (e.g.,
DisplayInfo
). - Constructors: Special methods named
New
that are called when an object is created, used for initialization. - Events: Mechanisms for objects to signal that something has happened.
- Nested Classes: Classes defined within another class.
Creating Objects (Instantiating a Class)
You create an instance of a class using the New
keyword.
' Create an instance of the Car class
Dim myCar As New Car("Toyota", "Camry", 2023)
' Access members using the dot operator
myCar.Color = "Blue"
myCar.DisplayInfo() ' Output: This is a 2023 Toyota Camry.
Console.WriteLine($"The car's color is: {myCar.Color}") ' Output: The car's color is: Blue
Access Modifiers
Access modifiers control the visibility and accessibility of class members:
- Public: Accessible from anywhere.
- Private: Accessible only within the class.
- Protected: Accessible within the class and by derived classes.
- Friend: Accessible within the same assembly (project).
- Protected Friend: Accessible within the same assembly or by derived classes.
- Internal: Same as
Friend
.
Key Concepts
- Encapsulation: Bundling data and methods that operate on the data within a single unit (the class).
- Abstraction: Hiding complex implementation details and exposing only necessary features.
- Inheritance: Allowing a new class (derived class) to inherit properties and methods from an existing class (base class).
- Polymorphism: The ability of an object to take on many forms, often through inheritance and method overriding.