This is a part of the Learn to Develop Windows Phone 7.5 Mango Applications. In our last tutorial we have seen that we can use “for statement” or better known as “for iterations” or “for loop”. In this tutorial we will learn how to use simple helper methods, which allow users to split up the code in multiple code blocks. A helper method is simply a block of code that has been given a name. We execute this block of code by calling it through another block of code. But a question might be hovering in your mind, why do we need to split up our code in multiple code blocks? Why not simply keep it in one file? Here are few reasons, why we use methods.
- To avoid writing same block of code twice
- Reduces possibility of errors
- If you need to correct some part of code, you have to do it at just one place rather than checking for the same block of code at every place
- It is easier to add new functionality to your program as you have to make changes at only once place
We have already worked with methods even though I never mentioned name “method” before. The event handlers like button_Click event is nothing but a simple method designed for helping the event mechanism.
Let us start with a new project and learn new ways of creating methods. We will keep our user interface simple, we will just have one button and one text block situated one after other respectively (see image). Now double-click on button to get into button_Click event.
Here we will write code for our helper method, just make sure you are writing the code inside the class code block and outside the button_Click event. Write following code:
textBlock1.Text = myMethod("bob");
Once you have done this, we will create one private method for returning value to this event. So after closing parentheses of button_Click event, paste following code and run this application:
private string myMethod(string myParameter)
return "hello world," + myParameter;
Upon running this application, you will get output as “hello world, bob” on the occurrence of button_Click event, which means our application is working fine.
Let us now understand this application. In our button_Click event, we just have called one method by its name ending with pair of parenthesis. Obviously it won’t work just by calling its name, so after the event box we have created the definition for that simple method.
The word “private” tells compiler that this method should not be revealed to anyone outside this class. After that word “strings” is written, this tells compiler what sort of value we would return back to event. In this case, we will return string. And finally we have provided parameter name in parenthesis.
So when we click on button, the button_Click event occurs, which sends string “bob” to our method. Then the method appends the word “bob” to “hello world” and returns it back to event.
So essentially we use methods when we want to cut down complexity of the code. It goes long way, if you are building your on application as you can make changed far more easily that you could have with one file of code. Once you master this, you can head over to our Day 1 – Homework assignment.