Mobile Zone is brought to you in partnership with:

I am a mother of four kids with a passion for languages, writing and programming. I try to cover everything a beginner should know. After all, I am a beginner. If there is anybody out there, thinking I missed something, or if somebody wants other topics, please feel free to contact me. Andrea is a DZone MVB and is not an employee of DZone and has posted 28 posts at DZone. You can read more from them at their website. View Full User Profile

Collections of Objects in Windows Phone

12.12.2011
| 3521 views |
  • submit to reddit

A Collection is simply an object that knows how to manage references to multiple instances of other classes. It keeps track of other objects. There are different collections to choose from. Each of them with their own “powers”. Some are created to keep objects sorted, some are created to allow for easy retrieval. The use of collections is quite common in the .NET Framework Class Library.

For now I am just focusing on the list collection, because it is the most simple one. To have an example to look at, I created a little project with a button called “myButton” and a textBox called “myTextBox”. First I add a class that I can work with:

coll2

Visual Studio nicely adds my class in a new page to my project:

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
     
    namespace Collections
    {
        public class Car
        {
     
        }
    }

I want to add two properties to my class:

    public class Car
    {
        public string Make { get; set; }
        public string Model { get; set; }
    }

So, let’s go back to my MainPage.xaml.cs file and add three instances of the car class inside the click event handler:

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        Car car1 = new Car();
        car1.Make = "Oldsmobile";
        car1.Model = "Cutles Supreme";
     
        Car car2 = new Car();
        car2.Make = "Geo";
        car2.Model = "Prizm";
     
        Car car3 = new Car();
        car3.Make = "Nissan";
        car3.Model = "Altima";
    }

Each instance has different properties.

Now I want to add each of these instances to a new collection, a list. Once I have them in the list I want to loop through each item in the list.

I am going to inspect the properties of each instance of a car class, adding it to a string that I then display in the textblock on the phone’s user interface:

    List<Car> myList = new List<Car>();
    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;

First I created a new instance of a new collection, a list collection. This new List<Car>(), which is a Generic List, can only work with my Car class. The List Object has a number of methods for managing the objects that it holds a reference to. The “Add” method allows me to add a reference to the list. The “foreach” statement works with collections. It iterates through my list and for each car in that list it displays the Make and the Model:

coll3

 

To be continued…

References
Published at DZone with permission of Andrea Haubner, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Ash Mughal replied on Thu, 2011/12/22 - 1:16pm

How we can store these collections. If we have a collectons of collectons as we may have in normal programming languages.

 

 

Andrea Haubner replied on Tue, 2011/12/27 - 4:45pm in response to: Ash Mughal

you can store everything in IsolatedStorage. Have a look at this:

http://andreahaubner.blog.com/2011/04/14/working-with-isolated-storage-listbox-and-datatemplates/

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.