ARTICLE

Structures in Visual Basic .NET

Posted by Atul Chaudhary Articles | Visual Basic 2010 December 28, 2010
In this article, I will explain you about Structures in Visual Basic .NET.
 
Reader Level:

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:

Output.gif

Summary

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

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor