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.
In visual basic, structures are declared by using Structure
keyword. Following is the declaration of structure in a 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.
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 a visual basic programming language.
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 a 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.
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 a default constructor and initializing 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 (parameterless) 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 a visual basic programming language with example.
Following is the example of creating the structure with a different type of fields and parameterized constructor in a 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 fields, parameterized 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 as shown below.
In visual basic, by using structures we can implement interface but structures cannot inherit from another structure or class.
Following are the important characteristics of structures in visual basic programming language.
Structure
keyword.New
keyword.The following are the difference between structures and classes in a visual basic programming language.
New
operator.