Structure in VB.NET
This article shows STRUCTURE in VB.NET and define the difference between Class and Structure.
This Article shows how to use structure in VB.NET.
Structures:-
-
A user defined data type .They are user-defined and provide a method for packing together data of different types.
-
Use structure keyword to declare the structure
-
It is value type and store on Stack
-
Cannot have explicit default constructor but can have parameterized constructors
-
Cannot have destructor
-
Cannot participate in inheritance
-
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
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.