Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Working with Loops, Arrays, and Collection...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Working with Loops, Arrays, and Collections in VBNET Game Development
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-06-25

    Table of Contents:
  • Working with Loops, Arrays, and Collections in VBNET Game Development
  • For Each...Next
  • Collections
  • For Each...Next (For Real This Time)

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with Loops, Arrays, and Collections in VBNET Game Development - For Each...Next


    (Page 2 of 4 )

    The final loop we'll be working with is the For Each loop. It's extraordinarily useful and implementations of it can be found in many other languages. The loop iterates over every element in a given group. So, we could iterate over all of the items in an array or in a collection. However, we haven't yet covered either arrays or collections, and so before we delve into the For Each loop, let's take a short detour (our last before we begin, I promise!).

    We'll start with arrays. As in any other language, they aren't too complex and so we won't need to spend a lot of time on them. After all, an array is simply a series of objects of the same type. There are two ways of declaring an array in Visual Basic, though declaring an array is very similar to declaring a regular variable. Using these two methods, let's create two arrays of Integer s:


    Dim array1() As   Integer

    Dim array2 As   Integer ()


    As you can see, the two methods of array declaration differ only by placement of the parentheses. However, the first method is the preferred method because we can specify array bounds within the parentheses. Let's give the first array an upper bound of five:


    Dim array1(5) As   Integer


    If you were to try this with the other method, you'd be given an error. Also, note that upper bound is not length. Instead, it's the highest index we can work with.

    We can now initialize our arrays. Since we've assigned our first array an upper bound, we can assign each element individually using the element's zero-based index:


    array1(0) = 1

    array1(1) = 2

    array1(2) = 3

    array1(3) = 4

    array1(4) = 5

    array1(5) = 6


    We can also make the assignment on a single line, and we can do this without having first specified an upper bound. So, let's populate the second array:


    array2 = New  Integer () {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}


    As with other variables, though, we can both create and initialize an array in the same step:


    Dim array3() As   Integer = New   Integer () {1, 2, 3}


    We can shorten this further still. Notice how Integer appears twice in the above line. This is a bit redundant, and we can cut out the second appearance entirely:


    Dim array3() As   Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}


    If we need to change the dimensions of an array at any time, we can use the ReDim statement:


    ReDim array2(3)


    By default, though, this will delete the existing elements of the array. In order to preserve the existing elements, we need to use the Preserve keyword:


    ReDim Preserve array2(3)


    And, of course, we're not limited to arrays of Integer s:


    Dim names() As   String = { "Bob" , "Henry" , "Tom" }


    In order to get the length of an array, we can use the Length property:


    Console.WriteLine("Length: {0}", array1.Length)


    We can also get the upper bound of the array using the GetUpperBound method. This method takes one argument, which is an Integer representing the dimension whose upper bound you want to determine. This starts at zero. Since all of our arrays only have one dimension, we can pass zero:


    Console.WriteLine("Upper bound (first dimension): {0}", array1.GetUpperBound(0))


    That, of course, brings us to multidimensional arrays. Arrays can have more than one dimension. We simply need to specify this when declaring an array. Inside of the parentheses, where we set the upper bound, we actually set the upper bound for each of the array's dimensions. Let's create a two-dimensional array with two elements in each dimension:


    Dim twoByTwo(1, 1) As   Integer


    When we assign to it, we just need to consider both dimensions:


    twoByTwo(0, 1) = 3


    If we don't want to specify the upper bound of each dimension, then we omit each, but we still need the proper amount of commas. Here, we create a three-dimensional array, but we don't specify any upper bounds:


    Dim threeDimensional(,,) As   Integer

    More Visual Basic.NET Articles
    More By Peyton McCullough


       · Hello, all,This is the fifth part of my series introducting Visual Basic .NET....
     

    VISUAL BASIC.NET ARTICLES

    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT