Field in Visual Basic .NET

Find this useful? Support us: Star on GitHub 6
Category: Class | Language: Visual Basic .NET

In Visual Basic .NET, a field is a member of a class that holds data or a reference to an object. Fields can be declared at the class level, outside any method or property, and have a specific data type. Here's an example of how to declare and use fields in a class:

Public Class Student
    Public Name As String
    Private Age As Integer

Public Sub New(name As String, age As Integer)
Me.Name = name
Me.Age = age
End Sub

Public Sub GetAge()
Console.WriteLine("{0}'s age is {1}", Me.Name, Me.Age)
End Sub
End Class

In this example, we have a Student class with two fields: Name and Age. Name is declared as a Public field, meaning its value can be accessed by any code that has a reference to a Student object. Age is declared as a Private field, meaning only code within the Student class can access it.

The Sub New method is a constructor that initializes the Name and Age fields when a new Student object is created. We use the Me keyword to refer to the object being created.

The GetAge method is a public method that displays the name and age of the student. It can access both the Name and Age fields.

We can create a new Student object and set its Name and Age fields like this:

Dim s As New Student("John", 18)
s.Name = "Jane"

We can then call the GetAge method to display the student's age:

s.GetAge()

This would output "Jane's age is 18" to the console.