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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - 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...





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