5,317,598 members and growing! (27,630 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate

Using Structures in VB.NET

By PrasadGVL

Through this article, I wanted to introduce you to the structures in VB.NET. Also, I compared and contrasted Structures to Classes.
VBWindows, .NET, .NET 1.0, .NET 1.1, NT4, Win2K, WinXP, Win2003VS.NET2002, VS.NET2003, Visual Studio, Dev

Posted: 19 Oct 2004
Updated: 19 Oct 2004
Views: 70,785
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 2.53 Rating: 2.27 out of 5
5 votes, 38.5%
1
4 votes, 30.8%
2
1 vote, 7.7%
3
3 votes, 23.1%
4
0 votes, 0.0%
5

Introduction

Structures are complex data types that encapsulate small pieces of tightly related data items. As with Classes, Structures can contain data members as well as member methods to work with data inside a structure. Since the in-memory representation of Structure is 'value type', memory management is handled efficiently.

Structures Vs Classes

Structures Classes
Value type Reference type
Supports data members, code members (methods) and events Supports data members, code members (methods) and events
Can not inherit Supports inheritance
Preferable when you perform large number of operations on each instance Preferable when you need to initialize one or more members upon creation
Can not control initialization using structure variable Can have parameterized constructors
Less flexible, limited event handling support More flexible, unlimited event handling support

Declaration

Structures are declared using Structure ... End Structure statements. Between these two statements, there must be at least one member declared. This member can be of any data type, non-shared and non-event. The following example shows how to encapsulate product info into a structure called Product.

Private Structure Product
    'Declare data members

    Public ProductID as Integer
    Public ProductName as String
    Private UnitPrice as Single

    'Declare code members

    Public Function GetPrice() as Single
        GetPrice = UnitPrice
    End Function
End Structure

In the above example, the Product structure is declared as Private. That means you can not access it from the object of the class where you defined it. As you can see, the data member UnitPrice is declared as Private, yes, you are right, you can't access it outside the structure.

Note that there is a function GetPrice defined within the Structure. Using this code member, one can get UnitPrice of a product. You can also define Sub procedures, properties and events. You can define a property as a default property, but it must accept at least one argument. You can declare events using Shared Sub procedure. To cause an event to actually happen, you have to use RaiseEvent statement.

The default accessibility for a structure is Public. You can use Private, Protected and Friend keywords also. You must specify accessibility for every member you declare inside a Structure. If you declare them with Dim statement, they are considered as Public. Remember that you can not initialize a Structure's data members at the time of instantiation. You have to access and initialize them individually after creation of a Structure variable or through a Structure code member. If you assign one Structure variable to another, a new copy is created in the other variable. Hence the changes to the values of first variable does not happen to the second. On the other hand, if you assign a Class object to another, changes to the data of one object reflects in the other. This is obvious in the case of Classes because the two objects in the above situation share the same instance.

Conclusion

  • Use Structures only if you want to use tiny pieces of related data as a single composite data type that resembles a built-in data type.
  • Use Classes if you can't decide which one to use among Classes and Structures. Because, Classes are more flexible. Though there are storage and performance advantages with Structures, often they are negligible.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

PrasadGVL


CTO for Abasys Technologies, Singapore chapter. Has more than 8yrs of programming experience using Microsoft's products. Loves working with .Net, especially ASP.Net.
Occupation: Web Developer
Location: Malaysia Malaysia

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
NewsAnother Differencememberdodiggitydag4:42 27 Jul '06  
GeneralStructures-How are they used?membervaltech10:47 15 Nov '04  
GeneralRe: Structures-How are they used?sussAnonymous1:14 29 Jul '05  
GeneralConstuctorsmemberSteven Campbell8:46 20 Oct '04  
GeneralRe: ConstuctorssussTony Phillips10:52 21 Jun '05  
GeneralRe: ConstuctorsmemberSteven Campbell3:12 22 Jun '05  
GeneralRe: ConstuctorsmemberHypnotron18:43 6 Nov '05  
GeneralRe: ConstuctorsmemberLuca Crisi, MCP3:05 5 Jun '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Oct 2004
Editor: Smitha Vijayan
Copyright 2004 by PrasadGVL
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project