ARTICLE

Structure in VB.NET

Posted by Rohatash Kumar Articles | Visual Basic 2010 September 13, 2010
This article shows STRUCTURE in VB.NET and define the difference between Class and Structure.
 
Reader Level:

This Article shows how to use structure in VB.NET.

 

Structures:-

  1. A user defined data type .They are user-defined and provide a method for packing together data of different types.

  2. Use structure keyword to declare the structure

  3. It is value type and store on Stack

  4. Cannot have explicit default constructor but can have parameterized constructors

  5. Cannot have destructor

  6. Cannot participate in inheritance

  7. Structure members can be private as well

Code for creating structure:

 

The following code creates a structure named Customer.

 

Structure Customer

    Private custid As Integer

    Private name As String

    Private balance As Double

 

    Public Sub New(ByVal custid As Integer, ByVal name As String, ByVal opamt As Double)

        Me.custid = custid

        Me.name = name

        balance = opamt

    End Sub

    Public Sub Deposit(ByVal amount As Double)

        balance += amount

    End Sub

    Public Sub ShowAccount()

        Console.WriteLine("Balance of {0} is {1}", name, balance)

    End Sub

End Structure

Class StructTest

    Public Shared Sub Main()

        Dim c As New Customer(1234, "Rakesh Verma", 9000)

        c.Deposit(5000)

        c.ShowAccount()

    End Sub

End Class

 

OUTPUT of the above code:

 

structure.gif


Difference between Class and Structure:These are the main difference between classes and structures.


classes are Reference types and structures are Value  types.


structures are used for smaller lightweight objects that do notpersist for long and classes are used for larger objects that are expected to existin memory for long periods.


We declare a structure in Visual Basic .NET with the Structure keyword.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
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