Structure in VB.NET

This article shows STRUCTURE in VB.NET and define the difference between Class and Structure.
  • 5722

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.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.