Structures in Visual Basic .NET
In this article, I will explain you about Structures in Visual Basic .NET.
In this article, I will explain you about Structures in Visual Basic .NET.
Structures
Structures are complex data types that encapsulate group of logically related data items. Structures are user-defined. They are very similar to Classes. Just like Classes, Structures can contain data members as well as member methods. The main difference in Structures and Classes is that structures are value type and classes are reference type. We use Structure . . . End Structure statement to declare a structure in Visual Basic .NET. Between these two statements, there must be at least one member declared and that member can be of any data type, non-shared and non-event.
Difference Between Structures and Classes:
Structures
|
Classes
|
Structures are Value type.
|
Classes are Reference type.
|
Supports data members, methods and events.
|
Supports data members, methods and events.
|
Structures can not supports
Inherit.
|
Classes supports inheritance.
|
Structures are preferable when you perform large number of operations on each instance.
|
Classes are preferable when you need to initialize one or more members upon creation.
|
Structures can not control initialization using structure variable.
|
Classes can have parameterized constructors.
|
Structures have less flexible, limited event handling support.
|
Classes have more flexible, unlimited event handling support.
|
The following code creates a Structure named EmpDetails with four fields of different data types.
Imports System.Console
Module Module1
Structure EmpDetails
'declaring a structure named EmpDetails
Dim Name As String
Dim Address As String
Dim Salary As Double
Dim Id As Integer
'declaring Four fields of different data types in the structure
End Structure
Sub Main()
Dim Obj As New EmpDetails()
'creating an instance of EmpDetails
Obj.Name = "Atul"
Obj.Address = "Sec-37"
Obj.Salary = 40000
Obj.Id = 131791
'assigning values to member variables
WriteLine("Name :" + " " + Obj.Name)
WriteLine("Address :" + " " + Obj.Address)
WriteLine("Salary :" + " " + Obj.Salary.ToString)
WriteLine("ID :" + " " + Obj.Id.ToString)
'accessing member variables with the period/dot operator
Read()
End Sub
End Module
The output of this code is:

Summary
Hope this article help you to Understand about Structures in Visual Basic.