What’s up, gang?! Hope the festive season has made you really jumping off the jack kinda nerd! I am really excited about this new holiday season! Have lots in mind! Today, on this really good festive mood, I just wanna touch some really important topics without which we can’t continue working with our apps. It’s not like a do or die situation, but it’s definitely a do or stop situation! This new this we are gonna see today will make our code really neat and enable us to make them really stand out amongst all the codes we will be writing.
Suggested Reading :
- Beginner’s Guide to Windows Phone 7 App development
- Create Hello World Application in Windows Phone 7
- Different properties of Windows Phone 7 emulator
- Coding the Hello World Application in Windows Phone 7
- How files interact in Windows Phone 7 App development
- Declaring Variables in Windows Phone Application Development
- Accepting input & displaying it in Windows Phone Application Development
- Using If Else Statement in Windows Phone 7 App Development
- How to create a Calculator App for Windows Phone 7
- Switch statement in Windows Phone 7 App development
Call me first!!
1. We did come across the term, method in one of our previous tutorials and I had promised we will deal with it someday. Well, it seems we got our wish! Now first of all, let me give you a really easy outlook to what a method is. When you write a code, you kinda order the computer to do things. This ordering or supplying the computer with a ‘to-do list’ is what the method can accomplish. When a computer accesses the method body, it is supplied with a list of things that the computer has to accomplish. More like your wife telling you what to do!
2. In a method, we have two parts, the ‘method-header’ and the ‘method-body’. The method-header is like it gives the ‘things-to-do’ list a unique name. It’s like a header so that the computer knows where to find the set of jobs it is asked to do when the user tells it to do. The ‘method-body’ is more like the set of jobs that the user has given the computer to do. For example,
“A shopping list will comprise of the following things-a header like ‘shopping list’ and a body like ‘1 soap’, ‘2 bed sheets’, ‘5 computers’ kinda thing. Here when the shopper takes a look at the list header, he can know that he has to do shopping and when he looks at the list of things to buy, he knows where to go and what to buy. That’s what the method does for the user.”
3. Take a good look at this example. I have created only a simple preview of how the app is gonna look like-
We have used only a textBlock and a button for our app.
3. On double clicking the button, we get the coding pane where we will type a code something like this-
If you take a good look at the code, we have two parts in the code that we have written, first is the method which we have written ‘outside’ the scope of the button code. It is written in such a way that it can be accessed only by the button that is associated with it; this is enabled by the ‘private’ keyword. More about that later. Now we have a return type called, myname, and it must be of the variable type ‘string’. Inside the parenthesis we have used parameters, or values that have to be passed to the method, when being called from the class above. Then inside the method-header, we have a return statement which will return the value given inside the String.Format brackets.
Then we need to access the method by calling the method from a class, which is done by the line of code we have written inside the button scope. This value is passed on to the textBlock and displayed as output.
4. Let’s try to run the code now-
So we can see that the value passes around and it is displayed as an output on the textBlock we designed. Hope that gives you a good idea about methods! With that short note, I will take your leave and wish you all a very ‘coding’ holidays ahead!!