Understanding and Creating Classes: Windows Phone 7.5 Mango Apps Development – Part 11

In our previous tutorial we looked at DateTime data type and the various ways to manipulate date and time. In this part of the Windows Phone 7.5 Mango Apps Development tutorial series, we shall have a look at the basic building block of code, i.e. Class.

So let’s get started!

A class is an assembly that enables us to create custom types by grouping together different types of variables, methods, properties and events. Basically a class is a blue print. Objects are instances of a class with different values for attributes. So for example lets take an example of a car. All cars have a manufacturer name, a model number, a color, build year, etc. Cars can perform various actions like accelerating, braking, etc. Honda Civic, Toyota Corolla, etc are instances of the car class. Abstraction is an extremely important part of building a class. Abstraction refers to picking only the necessary contents in the given context.

Next let’s see how to create a custom class.

Just as our previous lesson we shall create a new project with a unique name except for this project will not involve any of the controls but rather we will be working with the C# code. Once the project is created and is displayed in the Design mode right-click the Project name and select Add from the menu displayed. Under the Add menu select Class. A new window opens up asking for the name of the class. Enter the name as Car.

Copy the following lines of code into your Car class

class Car


        public string Make

        public string Model

        public int Year

        public string Color 

        public void Accelerate()


            // White code here to make it accelerate


        public void Decelerate()


            // Write code here to make it decelerate



The keyword “class” tells the compiler that the following block of code is a class. Car is the name of the class. For our car class we have four attributes, viz. Make, Model, Year and Color. “Public” is the access specifier followed by the data type (string for Make, Model and Color and integer for Year). As mentioned earlier Classes contain methods that give the classes functionality. In our Car class we have two methods Accelerate and Decelerate. These methods will contain code that will enable the car to accelerate and decelerate.

Now in order to create instance of the Car class in our MainPage.cs file all we have to do is type the following code. This line of code can be added in the Button Click event or can be defined at the global level as soon as the public partial class MainPage:PhoneApplicationPage starts:

Car myNewCar = new Car();

Now in order to set the values to the variables you would write the following statements

myNewCar.Make = “Honda”;

myNewCar.Model = “Civic”;

myNewCar.Year = 2012;

myNewCar.Color = “Red”; 

In order to call the methods defined in the car class you would write the object name followed by a dot (.) followed by the name of the method.



That’s how easy making and implementing basic classes is. We will learn more about working with Classes in next part.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + 2 =