Visual Basic Structures

In visual basic, Structures are same as classes but the only difference is classes are the reference types and structures are the value types. As a value type, the structures directly contain their value so their object or instance will be stored on the stack and structures are faster than classes.

 

In visual basic, the structures can contain fields, properties, member functions, operators, constructors, events, indexers, constants and even other structure types.

Create Structures in Visual Basic

In visual basic, structures are declared by using Structure keyword. Following is the declaration of structure in visual basic programming language.

 

Public Structure Users

    ' Properties, Methods, Events, etc.

End Structure

If you observe the above syntax, we defined a structure “users” using Structure keyword with Public access modifier. Here, the Public access specifier will allow the users to create an object for this structure and inside of the body structure, we can create required fields, properties, methods and events to use it in our applications.

 

Following is the example of defining the structure in visual basic programming language.

 

Public Structure User

    Public name As String

    Public location As String

    Public age As Integer

End Structure

If you observe the above example, we defined a structure called “User” with the required fields and we can add required methods and properties based on our requirements.

Visual Basic Structure Initialization

In visual basic, structures can be instantiated with or without New keyword. Following is the example of assigning a values to the variables of structure.

 

Dim u As User = New User()

u.name = "Suresh Dasari"

u.location = "Hyderabad"

u.age = 32

To make use of fields, methods and events of structure, it’s mandatory to instantiate the structure with New keyword in visual basic programming language.

Visual Basic Structure with Constructor

In visual basic, the structures won’t allow us to declare default constructor or constructor without parameters and it won’t allow us to initialize the fields with values unless they are declared as const or shared.

 

Following is the example of defining the structure with parameterized constructor and initializing the fields in constructor in visual basic programming language.

 

Public Structure User

    Public name, location As String

    ' Parameterized Constructor

    Public Sub New(ByVal a As String, ByVal b As String)

        name = a

        location = b

    End Sub

End Structure

If you observe the above example, we defined the structure called “User” with required fields and parameterized constructor.

Visual Basic Structure with Default Constructor

As discussed, the structures will allow only parameterized constructors and fields cannot be initialized unless they are declared it as const or shared.

 

Following is the example of defining the structure with default constructor and initializing a fields with values.

 

Structure User

    ' Compiler Error

    Public name As String = "Suresh Dasari"

    Public location As String

    Public age As Integer

    ' Compiler Error

    Public Sub New()

        location = "Hyderabad"

        age = 32

    End Sub

End Structure

When we execute the above code, we will get a compile-time error because we declared a structure with the default constructor (parameter less) and initialized fields without defining it as const or shared.

 

Now, we will see how to create a structure with fields and parameterized constructor in visual basic programming language with example.

Visual Basic Structure Example

Following is the example of creating the structure with different type of fields and parameterized constructor in visual basic programming language with various data members and member functions.

 

Module Module1

    Structure User

        Public Const name As String = "Suresh Dasari"

        Public location As String

        Public age As Integer

        Public Sub New(ByVal a As String, ByVal b As Integer)

            location = a

            age = b

        End Sub

    End Structure

    Sub Main()

        ' Declare object with new keyword

        Dim u As User = New User("Hyderabad", 31)

        ' Declare object without new keyword

        Dim u1 As User

        Console.WriteLine("Name: {0}, Location: {1}, Age: {2}", User.name, u.location, u.age)

        ' Initialize Fields

        u1.location = "Guntur"

        u1.age = 32

        Console.WriteLine("Name: {0}, Location: {1}, Age: {2}", User.name, u1.location, u1.age)

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we defined a structure (User) by including the required fieldsparameterized constructor and created an instance of structure (User) with and without New keyword to initialize or get the field values based on our requirements.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Structures Example Result

 

In visual basic, by using structures we can implement interface but structures cannot inherit from another structure or class.

Visual Basic Structure Characteristics

Following are the important characteristics of structures in visual basic programming language.

 

  • In visual basic, structures are value types and those are defined by using Structure keyword.
  • During structure declaration, the fields cannot be initialized unless they are defined as const or shared.
  • Structures in visual basic can include fields, properties, member functions, operatorsconstructors, events, indexers, constants and even other structure types.
  • Structures cannot include default constructor (constructor without parameters) or destructor but it will allow us to declare constructors with parameters.
  • A structure cannot inherit from another structure or class.
  • In visual basic, the structure can implement interfaces.
  • A structure can instantiated with or without using a New keyword.

Visual Basic Structure vs Class

Following are the difference between structures and classes in visual basic programming language.

 

  • In visual basic, classes are the reference types and structures are the value types.
  • Classes can contain default constructor or destructor but structures will contain only constructors that have a parameters.
  • We can implement inheritance using classes but structures won’t support inheritance.
  • Unlike classes, structs can be instantiated with or without using a New operator.

Topics Covered