C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Arrays in VB.NET


Posted Date: 03 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: ManojRajanMember Level: Gold    
Rating: 1 out of 5Points: 7




Single dimensional Arrays

Arrays are used to store values of similar type. With arrays you can store a sequence of values and refer to them with an index. In .net several inbuilt function makes it easy to manage array values. Let us see the arrays in detail,


Declaring and Initializing single dimensional arrays

Like any other variable you can declare and initialize arrays in same statement. When you declare an array in this way you must specify the type and count of the array elements. Unlike in Visual Basic 6.0, Arrays in .net are zero based. That is by default the first array element have an index 0. So the last element will have an index equal to the total number of elements minus one.

e.g. Dim MyArray(9) as string
MyArray(0) = “Peter”
MyArray(1) = “Tom”
MyArray(2) = “Fred”
MyArray(3) = “Barbara”

The above line will create an array of size 10. Like in Visual Studio 6 you cannot specify a lower bound in an array declaration.

e.g. Dim MyArray() = {“ Peter”,” Tom”,”Nancy”}

The above line will create an array of size 3 with values provided. You can use the Ubound() function to determine the upper bound of the array.


Declaring and Initializing Multi-dimensional arrays

You can declare a multi-dimensional array like this,

Dim MyArray(4,2) as integer
Dim MyArray( , ) as integer = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}







READ VALUES

Dim Counter1 as integer
Dim Counter2 as integer

For counter1 = 0 to ubound(MyArray)
For counter2 = 0 to ubound(MyArray,2)
Debug.writeline MyArray(counter1,counter2).ToString
Next counter2
Next counter1


Dynamic Arrays

Some time we require to populate an array dynamically at runtime. So declaring an array with the limit specified doesn’t help. We need to create an array with the maximum limit specified on the fly. Like in Visual Studio 6.0 you can make use of the Redim statement to declare an array dynamically. But simply using redim will clear the previous values in the array. For that we can use preserve keyword along with the redim statement.

Dim MyArray() as integer

You can make use of the array now as the maximum limit is not specified, If you use a statement like this, it would raise an exception. So before making use of this array, declare it like this

‘Write Values
Dim inc as integer
Inc = 1
For counter = 1 to 100
Redim preserve MyArray(counter) as integer
MyArray(Counter) = inc
Inc += 1
Next

‘Read Values
For counter = 0 to ubound(MyArray)
Debug.writeline MyArray(counter).ToString
Next





The disadvantage with the arrays is that you cannot store different types of values in an array. For this refer the Collections in .net.











Responses

Author: raja gopal    27 Jun 2004Member Level: Bronze   Points : 0
Article is very good and it gives good knowledge about ARRAYs. I like the way he presented with examples. My intention in giving feed back is to encourage this type of articles.

thanks
raj


Author: sridhar venkataramanan    08 Feb 2005Member Level: Bronze   Points : 0
could you spell the short cut way to scroll thru the properties window
of vb.net as that of vb. [ctrl+shift+first character of the property )


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Programmable Tab Strip using C#
Previous Resource: Regular expression for matching text between specific delimiters
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use