Friends, want to be lucid about what structures are in VB.Net and other various issues concerning them? This article will help you! Let's get started!
Structures in VB.Net are similar to structures in C and C++ but with several prominent differences. In VB.Net, structures are value types. The memory for structure variables is allocated on stack and there is no heap allocation. Structures can have constructors, methods and properties!
The following are the possible structure modifiers:
- new
- public
- protected
- internal and
- private
Same modifiers cannot appear more than once in a declaration. The modifiers namely abstract and sealed are not permitted.
The notable differences between structures in C, C++ and VB.Net are as follows:
- In C++, structure members are public by default while class members are private by default but in VB.Net it is not the case.
- In C, we cannot have constructors in a structure.
- In C++ it is possible to have constructors for a structure.
- In C++ partial initialization of structures is allowed whereas in VB.Net, it is not.
If you run through the following program, it will be much easier to follow
'structures in VB.Net
'structure_test
'This program contains a structure 'struct1', a class 'structinside' and the Main class 'class1'
Imports System
Namespace structure_test
'structure
Public Structure struct1
'public Structure struct1 structures cannot contain explicit parameterless constructors
Public Sub New(ByVal ival As Integer, ByVal hasdataval As Boolean, ByVal strval As String)
Me.i=ival
Me.hasdata=hasdataval
Me.str=strval
End Sub
Public Sub show()
Console.WriteLine("i {0},hasdata {1},str {2}",i,hasdata,str)
End Sub
'public int i As Integer cannot have instant field initializers in structs
Public i As Integer 'structure members are private by default
Public hasdata As Boolean
Public str As String
End Structure
'class
Friend Class structinside
Public a As Integer=5
Public structvar1 As struct1
' a class can have a structure variable as its member
End Class
'Main class
Friend Class Class1
<STAThread> _
Shared Sub Main(ByVal args As String())
'instantiating structinside class
Dim s As structinside = New structinside()
'instantiating the structure struct1
Dim structvar2 As struct1 = New struct1(1,True,"Saradha")
'displaying the structure instance structvar2
structvar2.show()
'initializing the members of structure variable structvar1 which is a member of the class structinside
s.structvar1.i=3
s.structvar1.hasdata=False
s.structvar1.str="Gnanavel"
s.structvar1.show()
'structure variables must be initialized before use
Dim structvar_without_new As struct1
'structvar_without_new.show() error: use of unassigned local variable
End Sub
End Class
End Namespace
As you can see, in VB.Net, structure members are private by default. Thus the structure fields should be declared public to get the default structure behavior.
We can't have instance field initializes for structure members. Structure members must be initialized using functions or through constructors.
We also cannot have explicit parameter less constructors (default constructors), since they are reserved. But we can have any number of custom parameterized constructors with different signatures.
While declaring a structure variable, if we do not use new operator to call a constructor, then the structure object will be created but the values will be unassigned. Remember in VB.Net, all value types must be initialized before use. Also note that instance variables and class variables need not necessarily be initialized before use.
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).