ARTICLE

Digging Deeper - Structures in VB.Net

Posted by Saradha Gnanavel Articles | Visual Basic Language February 22, 2005
Structures in VB.Net are similar to structures in C and C++ but with several prominent differences. In VB.Net, structures are value types.
 
Reader Level:

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).

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor