School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,433 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,519 people online right now. Registration is fast and FREE... Join Now!




Introduction to Generic Collections - Lists

 
Reply to this topicStart new topic

> Introduction to Generic Collections - Lists, Introduction to Lists

SixOfEleven
Group Icon



post 13 Apr, 2009 - 10:28 AM
Post #1


Why use a List?

I when I say method I mean Sub/Function.

Lists are very good collections. When you create your applications there are many reasons to use a list to store objects.

If you do not know when you design an applcation how many objects of one class you need to keep track of, a list would be a very good choice for storing your objects because it is very easy to add another object to your list. If you used an array you would have to make a lot more changes to your code.

If you think, as your application grows or you upgrade your application, you will need to store more objects of one class, a list would be a good idea for storing your objects. Same reason as above. You can write your code so that it is easy to just add another object to your list.

How do you use a List?

Using Lists is very simple.

First you must declare your list. You do it this like this:

CODE

Private MyList As List(Of Class)


After declaring your list you need to create a new list. You would do it like this:

CODE

MyList = New List(Of Class)


There are two ways you can add a new object into your list. The first one is using the Add method of the List class like this:

CODE

MyList.Add(object)


You can also add an object at a specifc point in the list using the Insert method like this:

CODE

MyList.Insert(index, object)


If you use the Insert method there are two things you will need to remeber. First is that the index value is zero based. That means the first object in the list is 0, just like in arrays. The second is if you try to insert an object into a list that is outside the list you will get an exception. Say you have 10 objects in your list. If you try to insert and object at 20 you will get an exception.

The List class has a property called Count. You can use it to tell how many items are in your list. You use it like this:

CODE

Dim number As Integer

number = MyList.Count;


To remove an object from your list there are a few ways you can do that. The first is to use the Remove method like this:

CODE

MyList.Remove(object);


This will remove the first occurance of the object in a list.

You can also chose a specific element to remove like this:

CODE

MyList.RemoveAt(index);


Again, like in the Insert method you have to make sure you pass a correct index or your program will generate an exception.

There is one more method that I want to talk about the Clear method. The Clear method removes all entries of the list. You use it like this:

CODE

MyList.Clear();


There are two simple ways that you can go through each object in the list. First you can use a foreach loop. You would do it like this:

CODE

For Each object As Class In MyList
    Console.WriteLine(object.ToString())
Next


You can also use a for loop to go through the list. This is how you would do it:

CODE

For I As Integer = 0 to MyList.Count - 1
    Console.WriteLine(MyList(I).ToString())
Next


Lists have a lot more members, this is just meant to be a breif introduction.

Here is a simple program that demonstates how to use a list of strings:

CODE

Module Module1

    Private MyList As List(Of String)

    Sub Main()
        MyList = New List(Of String)

        MyList.Add("Jimmy")
        MyList.Add("Robby")
        MyList.Add("Wendy")
        MyList.Add("Rhonda")

        Console.WriteLine("This is the list:")
        For Each name As String In MyList
            Console.WriteLine(name)
        Next
        Console.WriteLine()

        Console.WriteLine("Adding Harry at position 2")
        MyList.Insert(2, "Harry")

        Console.WriteLine("This is the list:")
        For Each name As String In MyList
            Console.WriteLine(name)
        Next
        Console.WriteLine()

        Console.WriteLine("Removing Robby")
        MyList.Remove("Robby")

        Console.WriteLine("This is the list:")
        For Each name As String In MyList
            Console.WriteLine(name)
        Next
        Console.WriteLine()

        Console.WriteLine("Removing number 1")
        MyList.RemoveAt(1)

        Console.WriteLine("This is the list:")
        For I As Integer = 0 To MyList.Count - 1
            Console.WriteLine(MyList(I))
        Next
        Console.WriteLine()

        Console.ReadLine()

    End Sub

End Module
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 12:46AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month