Visual Basic .NET - Classes and Object
In VB.NET, a class is a user-defined data type that encapsulates data and related functionality. An object is an instance of a class, which has access to the properties and methods defined in the class. Classes and objects are used extensively in object-oriented programming to represent real-world entities and to create reusable code.
To create a class in VB.NET, use the "Class" keyword followed by the name of the class. The properties and methods of the class are defined within the curly braces {}.
Here's an example of a simple class named "Person" that has two properties (Name and Age) and one method (PrintDetails):
vbnet
Copy code
Public Class Person
Private _name As String
Private _age As Integer
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
Public Sub PrintDetails()
Console.WriteLine("Name: " & Name)
Console.WriteLine("Age: " & Age)
End Sub
End Class
To create an object of this class, use the "New" keyword followed by the name of the class. Then, you can set the properties of the object and call its methods, like this:
Dim person1 As New Person()
person1.Name = "John"
person1.Age = 30
person1.PrintDetails()
This will create an object of the "Person" class with the name "person1", set its properties to "John" and 30, and then call its "PrintDetails" method, which will print out the person's name and age.