Structure in Visual Basic .NET

August 24, 2017

Structure in Visual Basic .NET is user defined data type. You can have different types of data used in Structures

Structure in Visual Basic is similar to Class. But they have different features too. I will discuss them in following section

This is how you define the Student structure

 Public Structure Student
        Public FirstName As String
        Public LastName As String
        Public DateOfBirth As Date
 End Structure

 

Create a new Student

Dim grad_student As Student
grad_student.FirstName = "John"
grad_student.LastName = "Doe"
grad_student.DateOfBirth = "01-03-1980"

You can use Dim to create new student and you can assign to values too .This is same like what you work with other data type

Add properties to structure

Public ReadOnly Property GetName() As String
   Get
      Return FirstName + " " + LastName
   End Get
End Property

 

Similarities

Structures and classes are similar in the following respects:

  • Both can have other data types as members
  • Both have members, which can include constructors, methods, properties, fields, constants, enumerations, events, and event handlers
  • Members can have access level Private and Public

Differences

  • Structures and classes differ in the following particulars:
  • Structures are value types; classes are reference types.
  • Structures are value types; classes are reference types.
  • Structures are not inheritable; classes are
  • A structure does not require a constructor; a class does