The Windows Club

Developing Windows Phone 7.5 Apps: Part 3; Variables, Data Types And Assigning Values

In the last chapter we wrote our first application for Windows Phone 7.5. In this tutorial we will see what variables and data types are and how to assign or retrieve values from variables. Plus we will also have a look at assigning value to textboxes.

Let us start with variables. Variable are those memory elements which store some value in it. So if we are to break it down, variables are like buckets, which hold some meaningful value or data in it. So whenever a user declares a variable, a bucket is created in memory to be filled with some value. The type of variable depends on user’s needs. Every C# variable and object must be declared with appropriate data type.

C# provides you several data types, which you can use. There are two categories of data types based on their way of passing data, viz. value types and reference types. While assigning value to another variable, actual value is copied to that variable in the case of value type variables; whereas if we are assigning reference type variables, only reference or address to the memory location where our original variable is stored, is passed.

You can learn more about data types and about their difference here, but as a beginner we will use few value types variables as listed below.

  1. int: Stands for integer. A data type for storing numbers without decimals.
  2. Char: It is used for storing a single character.
  3. String: It is used for storing text, that is, a number of chars
  4. Bool: It is one of the simplest data types. It can contain only 2 values – 0 and 1 or false or true.
  5. Float: It is used to store numbers which may or may not contain decimals.

We will use almost all of these data types in our future tutorials. If you want to learn more about these and other data types, you can refer Microsoft’s MSDN channel for the same.

Assigning values and retrieving input from textboxes:

This is a very basic step, which will enable you to get some value from users or show them something. Out of these two things, we have already performed the latter in Hello World application. In Hello world application, we displayed user a message “hello world” in button_click event. That means, you have already familiar with the showing output, so let us now learn to accept input. Taking input is just mirror process of providing output.

Open our test1 or helloworld project again. We will make slight modifications in that project to accept user’s name and then we will display some message. Now that we already have a textblock and a button, we will need just one extra textbox and another textblock. Drag a textbox (txt1) from toolbox and place it above previous textblock and drag another textblock (txtip) and put it besides the textbox. Change the text property of new textblock to “Enter your name:” and double click on button to go to button_click event. Now in curly bracket pair, paste this code:

 String name = “”;
name = txtinput.text;
txtop.text = “hello ” +name;

Now run the application and watch fun. Here we have asked user for his/her name and stored that name in string type variable “name”. So that’s all you have to do to accept user input. One caution you have to take while accepting input is matching variable type with input type. For example, we cannot store users name in “int” type variable, we can do so only with “string” type variable. Although you can convert these values in other types as well, not every time it is possible. You can learn more about type conversion here.

So this is it, if you have reached here, you have done nicely in this tutorial and you are ready to go to next tutorial.