Performing basic array operations in VB.NET
Takeaway: VB.NET offers a simple way of grouping data into the array structures similarly to other languages. This article explores array declaration and usage.
The ability to work with arrays is important in any programming language. VB.NET offers a simple way of grouping data into the array structures similarly to other languages. In this article, I will look at array declaration and usage.
Purpose of the arrays
Arrays are generally used for storing similar types of values or objects. They allow grouping variables together and allow referring to them by using an index. Arrays have an upper bound and a lower bound, which simply refer to the starting index and the ending index of a given array structure. Additionally, the data in the array can be sorted. Simple arrays are one-dimensional; however, you can also use multi-dimensional arrays in VB.NET. You can loop through an array to determine and to modify the values stored in the array.
Declaring and initializing arrays
There are two ways of initializing the arrays: to declare and initialize them in one statement, or to declare an array and choose to initialize it later.
When declaring and initializing arrays in the same statement, you must specify the type of the array elements and the number of the elements the array will hold. In VB.NET, arrays are zero based, which means that the index of the first element is zero. The elements are numbered sequentially. You must specify the number of array elements by indicating the upper bound of the array. The upper bound is the number that specifies the index of the last element in the array. Listing A shows the declaration and initialization of an array of integers.
Another way to declare and initialize arrays is to perform these operations in two separate steps. If you declare an array without specifying a number of elements on one line, you have to provide the values for each item of the array when you initialize it. The initial values are provided enclosed in the {} braces, using a comma as a separator. Listing B shows the declaration and initialization of an array in two separate steps.
Once an array is declared and initialized, it's possible to change the size of an array in run time by redefining it. You can use the ReDim statement to change the number of items in an array structure. Listing C shows declaration, initialization, and then re-sizing of an array structure.
By default, the data stored in an array is lost whenever an array is re-initialized. However, you can use the ReDim statement with the Preserve keyword in order to keep the existing data in the array when it's being re-initialized. Listing D re-initializes an array structure using the Preserve statement to keep the data already stored in the array.
There are two types of multidimensional arrays: rectangular or jagged. In rectangular arrays, every member of each dimension is extended into the other dimensions by the same length. In jagged arrays, individual members of one dimension can be extended into other dimensions by different lengths. The more dimensions an array has, the more complex it becomes to work with it.
Miss a tip?
Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!
Print/View all Posts Comments on this article
|
|
|
|
|
|
White Papers, Webcasts, and Downloads
- Volume Activation Technical Reference Guide Microsoft This reference guide is for information technology (IT) implementers whose ... Download Now
- Unlocking Hidden Value from Investments in SAP NetWeaver Business Warehouse IBM Organizations that have made strategic investments in SAP technology do so ... Download Now
- Recession Proofing Your Organization with Electronic Forms IBM Corp. The current economy is forcing organizations of all sizes to look more ... Download Now
- Webinar: Best Practices for Windows 7 Application Compatibility Flexera Software Are your business-critical applications compatible with Windows? 7? Join ... Download Now
- Twelve Ways to Reduce Costs with Microsoft(r) SQL Server(r) 2008 Microsoft Many organizations are finding themselves having to deal with difficult ... Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
