Reader Level:
ARTICLE

Understanding Structures in VB.NET

Posted by Rajesh VS Articles | Visual Basic .NET November 10, 2012
A structure in VB.NET is simply a composite data type consisting of a number elements of other types.
  • 0
  • 0
  • 44294

A structure in VB.NET is simply a composite data type consisting of a number elements of other types. A VB.NET structure is a value type and the instances or objects of a structure are created in stack. The structure in VB.NET can contain fields, methods, constants, constructors, properties, indexers, operators and even other structure types.

Structure Declaration & Object Creation.

The keyword struct can be used to declare a structure. The general form of a structure declaration in VB.NET is as follows.

<modifiers> struct <struct_name>
If (True) Then
End If 'Structure members

Where the modifier can be private, public, internal or public. The struct is the required keyword.

For example

Structure

MyStruct
Public x As Integer
Public
y As Integer
End
Structure 'MyStruct

The objects of a strcut can be created by using the new operator as follows.

Dim msAs New MyStruct()

The individual members of a struct can be accessed by using the dot (.) operator as showing below.

ms.x = 10
ms.y = 20

Remember that unlike classes, the strcut object can also be created without using the new operator.

Dim msAs MyStruct

But in this case all fields of the struct will remain unassigned and the object can't be used until all of the fields are initialized.

Structs & Fields.

A struct in VB.NET can contain fields. These fields can be declared as private, public, internal. Remember that inside a struct, we can only declare a field. We can't initialize a field inside a struct. However we can use constructor to initialize the structure fields.

The following is not a valid VB.NET struct and the code will not compile, since the fields inside the structure are trying to initialize.

Structure

MyStruct
Private x As Integer = 20
Private y As Integer = 20
End Structure 'MyStruct

A valid VB.NET structure is showing below.

Imports System
Structure MyStruct
Public xAs Integer
Public
y As Integer
End
Structure 'MyStruct
Class MyClient
Public Shared Sub Main()
Dim msAs New MyStruct
ms.x = 10
ms.y = 20
Dim sumAs Integer = ms.x + ms.y
Console.WriteLine("The sum is {0}", sum)
End Sub 'Main
End Class 'MyClient

However a struct can contain static fields, which can be initialized inside the struct. The following example shows the use of static fields inside a struct.

Imports System
Structure MyStruct
Public Shared xAs Integer = 25
Public Shared yAs Integer = 50
End Structure 'MyStruct
Class MyClient
Public Shared Sub Main()
Dim sumAs Integer = MyStruct.x + MyStruct.y
Console.WriteLine("The sum is {0}", sum)
End Sub 'Main
End Class 'MyClient

Remember that static fields can't be accessed by an instance of a struct. We can access them only by using the struct names.

Struct & Methods.

A VB.NET struct can also contain methods. The methods can be either static or non-static. But static methods can access only other static members and they can't invoke by using an object of the structure. They can invoke only by using the struct name.

An example is shown below.

Imports System
Structure MyStruct
Private Shared xAs Integer = 25
Private Shared yAs Integer = 50
Public Sub SetXY(ByVal i As Integer,ByVal jAs Integer)
x = i
y = j
End Sub 'SetXY
Public Shared Sub ShowSum()
Dim sumAs Integer = x + y
Console.WriteLine("The sum is {0}", sum)
End Sub 'ShowSum
End Structure 'MyStruct
Class MyClient
Public Shared Sub Main()
Dim msAs New MyStruct
ms.SetXY(100, 200)
MyStruct.ShowSum()
End Sub 'Main
End Class 'MyClient

The methods inside a struct can also be overloaded as like inside a class. For example

Imports System
Structure MyStruct
Private Shared xAs Integer = 25
Private Shared yAs Integer = 50
Public Overloads Sub SetXY(ByVal i As Integer,ByVal jAs Integer)
x = i
y = j
End Sub 'SetXY
Public Overloads Sub SetXY(ByVal i As Integer)
x = i
y = i
End Sub 'SetXY
End Structure 'MyStruct
Class MyClient
Public Shared Sub Main()
Dim ms1As New MyStruct
Dim ms2As New MyStruct
ms1.SetXY(100, 200)
ms2.SetXY(500)
End Sub 'Main
End Class 'MyClient

Structs & Constructors.

A VB.NET struct can declare constrcutor, but they must take parameters. A default constructor (constructor without any parameters) are always provided to initialize the struct fields to their default values. The parameterized constructors inside a struct can also be overloaded.

Imports System
Structure MyStruct
Private xAs Integer
Private
y As Integer
If
(True)Then
x = i
y = j
End If
Public
Sub New(ByVal i As Integer)
x =(y <<= i) 'ToDo: Unsupported feature: assignment within expression. "=" changed to "<="
End Sub 'New
Public Sub ShowXY()
Console.WriteLine("The field values are {0} & {1}", x, y)
End Sub 'ShowXY
Class MyClient
Public Shared Sub Main()
Dim ms1As New MyStruct(10, 20)
Dim ms2As New MyStruct(30)
ms1.ShowXY()
ms2.ShowXY()
End Sub 'Main
End Class 'MyClient

The 'this' operator can also be used in constructors and parameterized constructors can be chained inside a C# constructor. An example is given below.

Imports System
Structure MyStruct
Private xAs Integer
Private
y As Integer
Public
Sub New(ByVal i As Integer,ByVal jAs Integer)
MyClass.New(i + j)
End Sub 'New
Public Sub New(ByVal i As Integer)
x =(y <<= i) 'ToDo: Unsupported feature: assignment within expression. "=" changed to "<="
End Sub 'New
Public Sub ShowXY()
Console.WriteLine("The field values are {0} & {1}", x, y)
End Sub 'ShowXY
End Structure 'MyStruct
Class MyClient
Public Shared Sub Main()
Dim ms1As New MyStruct(10, 20)
ms1.ShowXY()
End Sub 'Main
End Class 'MyClient

Structs & Properties.

The properties can be declared inside a struct as shown below.

'VB.NET: Property

Imports System
Class MyStruct
Private xAs Integer '
Public Property X()As Integer
Get
Return
x
End Get
Set
(ByVal Value As Integer)
x = value
End Set
End
Property
End
Class 'MyStruct
Class MyClient
Public Shared Sub Main()'
Dim msAs New MyStruct
ms.X = 10
Dim xValAs Integer = ms.X
Console.WriteLine(xVal) 'Displays 10
End Sub 'Main
End Class 'MyClient

Structs & Indexers.

The indexers can also be used with a VB.NET struct. An example is shown below.
Imports System
Imports System.Collections
Structure MyStruct
Public data()As String
Default
Public Property Item(ByVal index As Integer)As String
Get
Return
data(index)
End Get
Set
(ByVal Value As String)
data(index) = value
End Set
End
Property
End
Structure 'MyStruct
Class MyClient
Public Shared Sub Main()
Dim msAs New MyStruct
ms.data = New String(5) {}
ms(0) = "Rajesh"
ms(1) = "A3-126"
ms(2) = "Snehadara"
ms(3) = "Irla"
ms(4) = "Mumbai"
Console.WriteLine("{0},{1},{2},{3},{4}", ms(0), ms(1), ms(2), ms(3), ms(4))
End Sub 'Main
End Class 'MyClient

Structs & Operator Overloading.

The operators can be overloaded inside a VB.NET structure also. The same rules applicable with respect to a VB.NET class is also applicable here. Both unary and binary operators can be overloaded.

Imports System
Structure Complex
Private xAs Integer
Private
y As Integer
Public
Sub New(ByVal i As Integer,ByVal jAs Integer)
x = i
y = j
End Sub 'New
Public Sub ShowXY()
Console.WriteLine("{0} {1}", x, y)
End Sub 'ShowXY
Public Shared Function UnaryMinus(ByVal cAs Complex)As Complex
'References to operator will need to be replaced by calls to this method
Dim tempAs New Complex
temp.x = -c.x
temp.y = -c.y
Return temp
End Function 'UnaryMinus
End Structure 'Complex
Class MyClient
Public Shared Sub Main()
Dim c1As New Complex(10, 20)
c1.ShowXY() ' displays 10 & 20
Dim c2As New Complex
c2.ShowXY() ' displays 0 & 0
c2 = -c1
c2.ShowXY() ' diapls -10 & -20
End Sub 'Main
End Classs 'MyClient

Structs & Inheritance.

There is no inheritance for structs as there is for classes. A struct can't inherit from another struct or class and it can't be the base class for a class. But remember that in VB.NET all types are directly or indirectly inheriting from the super base class object and hence the structure also. Since structs doesn't support inheritance, we can't use the keywords virtual, override, new, abstract etc with a struct methods. VB.NET struct types are never abstract and are always implicitly sealed. The abstract or sealed modifiers are not permitted in a struct declaration.

Since inheritance is not supported for structs, the declared accessibility of a struct member can’t be protected or protected internal. Since all struct types are implicitly inherit from object class, it is possible to override the methods of the object class inside a struct by using the keyword override. Remember that this is special case in VB.NET structs.

Structs & Interfaces.

Just like classes, a VB.NET struct can also implement from an interface. For example

Imports System
Interface IInterface
Sub Method()
End Interface 'Interface
Structure Complex
Implements IInterface 'ToDo: Add Implements Clauses for implementation methods of these interface(s)
Public Sub Method()
Console.WriteLine("Struct Method")
End Sub 'Method
End Structure 'Complex
Class MyClient
Public Shared Sub Main()
Dim c1As New Complex
c1.Method()
End Sub 'Main
End Class 'MyClient

Structs & Classes.

The structs in VB.NET seems to similar to classes. But they are two entirely different aspects of the language. The classes are reference types while a struct is a value type in VB.NET. The objects of class types are always created on heal while the objects of struct types are always created on the stack. But VB.NET structs are useful for small data structures that have value semantics. Complex numbers, points in a co-ordinate systems etc are good examples for struct types.

 

COMMENT USING