Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Collections of Objects in Windows Phone

DZone's Guide to

Collections of Objects in Windows Phone

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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…

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Andrea Haubner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}