Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - 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 - Collections


    (Page 3 of 4 )

    Arrays are fine for some circumstances, but in other circumstances, such as when the length of a group is a variable or when more complex behavior (e.g. a key, value combination) is needed, arrays aren't enough for the job. That's where collections come into play. These are collections of objects more complex than arrays.

    There are two groups of collections in .NET. The first group of collections contains non-generic collections. They were implemented in Visual Basic first, but they are not type-safe, and so you can stick any type of object in one of these collections no matter what type the other elements are or how the collection is used. This isn't a good thing and we won't be using these collections. The second group of collections contain generic collections. When a collection is created, you must specify a type for the elements. This type is enforced and so you can't mix and match types. We'll be looking at generic collections.

    The most array-like collection is List(T) (the “T” in “(T)” stands for “type” and it indicates that we're using a generic class). It's created like this:


    Dim collection1 As   New List( Of   Integer )


    Notice how we specify the type name with the Of keyword. This is done with all generics and, as noted above, this type is enforced. We can only add Integer s to the collection, just as we can only add Integer s to an Integer array. Also notice how we don't specify an upper bound as we do with arrays. This is because collections are not confined to a fixed size. Rather, you can add and remove elements as needed. We can add elements to the collection using the Add procedure:


    collection1.Add(1)

    collection1.Add(2)

    collection1.Add(3)


    If we need to remove an element, we can use the Remove method, which accepts an object and then removes the first occurrence of that object. Let's remove the number 3 from our collection:


    collection1.Remove(3)


    We can also use RemoveAt, which removes the element at a given index. Let's remove the first element:


    collection1.RemoveAt(0)


    Or, if we want to remove every element, we can use Clear:


    collection1.Clear()


    The size of the collection can be obtained using the Count property:


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


    Another collection is the Dictionary (TKey, TValue) collection. It's similar to what other languages may call an associative array or a hash table. Instead of matching elements to consecutive zero-based indexes, a dictionary maps values to keys. For example, say we want to associate names and ages. We could use a dictionary for this where the name is the key and the age is the value. The name, of course, would be stored as a String, and the age would be stored as an Integer. Let's create a Dictionary that matches this situation:


    Dim people As   New Dictionary( Of   String , Integer )


    Key-value pairs are added using Add :


    people.Add("Bob", 35)

    people.Add("Henry", 40)


    We can remove elements using Remove, which removes an element with a given key:


    people.Remove("Henry")


    There are more collections out there, and even the ones that have been mentioned have more to them, but we'll worry about that later on if we need to. A full tutorial on even the basic collections and their members would take up quite a bit of space.

    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 6 hosted by Hostway
    Stay green...Green IT