Collection of Objects: Windows Phone Apps Development Turorial – Part 14

So we are now in the 14 part of our Windows Phone 7.5 Mango Apps Development tutorial series. In our previous tutorial we looked at Namespaces. In this part we shall have a look at Collections of objects. So let’s get started!

A Collection is simply an object that knows how to manage references to multiple instances of other classes. In short it keeps track of other objects. There are different types of collections available under the .NET framework. Some collections enable you to keep objects sorted while others might help in easy retrieval of objects. Collections are used all the time when handling a number of objects.

We will now take a look at one type of collection called the List collection.

Create a new project with a unique name, something like ‘CollectionsDemo’. In the design mode, drag and drop a button in the top left corner of the emulator (see image).  Name the button properly and set its Content value to ‘Click Me’. Follow the same process for a text block and set its text property to blank. Set the text wrap property of the text block to wrap.

Application_layout

Now create a class Car with two attributes, viz., the Make and Model of string data type. Refer to Understanding and Creating Classes for class creation. Now double-click the Click Me button in the design window of MainPage.xaml file to open up the C# code for the button’s click event. Copy and Paste the following code in your button’s click event.

            Car car1 = new Car();

            car1.Make = “Oldsmobile”;

            car1.Model = “Cutlas Supreme”; 

            Car car2 = new Car();

            car2.Make = “Geo”;

            car2.Model = “Prism”; 

            Car car3 = new Car();

            car3.Make = “Nissan”;

            car3.Model = “Altima”; 

            List myList = new List();

            myList.Add(car1);

            myList.Add(car2);

            myList.Add(car3);           

     string myCars = “”;

            foreach (Car car in myList)

            {

                myCars += car.Make + “-” + car.Model + Environment.NewLine;

            } 

            myTextBlock.Text = myCars; 

So in the first nine lines of code we create three different objects of the Car class by the names car1, car2 and car3 and set their attributes respectively.

The following line of code creates a list called myList that can hold references to objects of the Car class only.

List<Car> myList = new List<Car>();

Once the list is created we add objects to the list by using the Add() method of the List class. The following lines of code add the three objects to the list one object at a time.

            myList.Add(car1);

            myList.Add(car2);

            myList.Add(car3); 

We then use the foreach iterator to iterate through each object in myList list. Each object’s attributes are stored in a string variable myCars. The Enviornment.NewLine inserts a new line.

foreach (Car car in myList)

            {

                myCars += car.Make + “-” + car.Model + Environment.NewLine;

            }

Finally we display the list of cars using the following statement.

myTextBlock.Text = myCars;

Application_output

You will get output as shown in figure. So this is how Collections are used. It is easy concept but some practice may need to master it.

See you in next tutorial.

Posted by on , in Category Phone with Tags
Gadget freak, Apple lover, Windows Phone lover; well that’s me! I have been in this wonderful field of freelance writing for quite some time and looking forward to developing more illuminating content related to gadgets and technology, as I go along.