Classes
Classes:
In VB.NET, a class is a blueprint or template for creating objects that encapsulate data and behavior. To create a class in VB.NET, we use the Class keyword, followed by the class name, and enclosed within curly braces. A class can have fields, properties, methods, events, and other members.
Example:
vb.netCopy codeClass Employee
Public Property FirstName As String
Public Property LastName As String
Public Property Salary As Double
Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal salary As Double)
Me.FirstName = firstName
Me.LastName = lastName
Me.Salary = salary
End Sub
Public Function GetFullName() As String
Return Me.FirstName & " " & Me.LastName
End Function
Public Function GetSalary() As Double
Return Me.Salary
End Function
End Class
' Create an object of the Employee class and set its properties
Dim john As New Employee("John", "Doe", 50000)
' Access the object's properties and methods
Console.WriteLine(john.GetFullName())
Console.WriteLine(john.GetSalary())
In the above example, we create a class named Employee that has three properties: FirstName, LastName, and Salary. We also define a constructor that initializes these properties, and two methods named GetFullName and GetSalary. We then create an object of the Employee class and set its properties. Finally, we access the object’s properties and methods using the dot notation.