Now that we are really talking about developing applications on a full-flow, why not start learning some tools which are going to enable us to do so? Let’s start with dealing with tools which are going to help us declare ‘variables’ and use them. More about that in my post later on, but the question I wanna ask you today is, “What do you do when you are asked to do anything simple in your daily life?! Yes, I am asking you ‘anything’. It can range from a simple ‘cleaning of your desk’ by your mom or just as complicated ‘building a rocket’ by NASA!!
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
So the first thing you will realize is that in order to do that task, you need ‘something’. What is that ‘something’? That something may range from a cloth which you can use to clean your desk to a really complicated driller which you wanna use to build your rocket. In coding too, we need these ‘somethings’ which we can use to perform our task. That is what we are going to deal about today.
The visuals are integrated and important as ever.
Carry my bags please!
1. Well speaking about variables from a very stupid mind, they are carriers. Like when you carry bags for your girlfriend when you take her shopping. Yeah that’s pretty much it. Now in our case of coding the C# language, we have got lots of choices to act as variables. Now don’t think that your girlfriend has got other boys to carry bags for her! Let’s keep it out of harm’s way!
2. So to enlist a few of the most commonly-used types of variables in C# are int, char and String. These can be used whenever we want to represent a particular ‘item’ or a ‘value’ and they will carry that for us, until we want them to, to be precise. Every variable needs to be declared as either of the given three or even the other ones which we will encounter later on. The declaration part can be stated by using a syntactic form as,
The data-type is the type of data we are going to use, like if it is a number (simple number without the decimal part) we can use int.Similarly, if it is a single alphabet, we can use char. If we are not sure whether the result we are going to hold is either a number or a character, even group of them, then we can use String. This is the ‘declaration’ part of the variable.
3. Let us discuss this by dealing with a real-time example,
“A great man whom you admire very much as asked you to hold air in your hands. As ridiculous as the idea might seem to sound, you keep trying for days to try to hold air in your hands, but it is not possible. So, you go back to the great man (who is probably a great stupid man) and tell him that it’s not possible. So, the great man asked you to hold water in your hands. Well that’s a little easy than holding air, but after some time you can’t just stop the water from flowing outta your hands. Dejected, you go back and tell him again it’s not possible. The great man now asks you to hold a pencil in your hand. And sure enough, you can very easily do that! The concept I am trying to get into you is that until the thing that you wanna hold or the ‘value’ is appropriate to you, i.e. the ‘data-type’, you will not be able to hold onto it. Name of the great man? Some say he is called, ’Kennedy’. Well, who knows?”
That pretty much sums up the fact that if you wanna hold a number you are going to use an int and not a char. Vice-versa. Period.
4. Let’s talk about the other ‘assignment’ part with syntax,
So the variable-name is the variable which we have already declared in our previous step. Remember that these two steps are not interchangeable. You cannot declare a variable after you have assigned it! The ‘=’ symbol in between them is known as an ‘assignment’ operator. It’s job is to ‘assign’ or ‘embed’, whatever you want to say, the value on its right-hand side to the variable on its left-hand side. Now we can see an example.
Build the simple HelloKennedy app until the designing part. If you can’t recall how to do it, simply go back I will show you straight from the coding part.
We will start with our int data-type. I have declared a variable x as int. Then on the next part, I have assigned it a value of 23. Now when we write the code for display in the textBlock1 we created, we get an error. Can you see why? It is because the variable x has been declared as an int but the textBlock1 is assigned to display only ‘Text’ variables, hence they are not compatible. Remember the holding example again.
5. Lets rectify this error. Think of a data-type which can hold alphanumeric values. Aha!! We just have the right candidate for us. The ‘String’ data-type. So we can do one of two things-
(i) Either go back to the declaration part and declare the variable as a string or,
(ii) Change the variable to a string when we wanna display it.
The first option will take another type of assignment, so I will deal with that later. Now how do we do the second option? Check out the visual-
This new code has a new keyword, ‘ToString()’ which we are going to use to convert the variable x which is int to a string datatype. We will talk about the crazy looking keyword later on. But first let’s see if we can run the code and get the output. On running the code, we have-
Eureka! Once we click the button, we can see that the value of the variable we want to display has been displayed in the TextBlock.
6. Lets now try our hands with some other data-types. Let’s see how we can declare and use a char or character data-type. Take a look at the code-
See that the assignment type for the char has changed. We have used to assign the letter a to the variable y by using single quotations
or ‘ ‘. Now I have used // symbols to ‘comment’ out the previous codes I wrote. I will deal with that later. Just remember that when I use those two ‘forward slashes’ I am telling the computer to ignore the whole sentence. Back to topic.
Again char is not compatible with the String data-type and we have to convert it to String before we can use it. So we are using the ‘ToString()’ keyword again. Let’s try to run the code-
Sure enough, we can the emulator displays to us the character ‘a’ which was assigned to the y variable.
7. Let’s try the same thing with the String data-type. As I told you that the String data-type can hold alphanumeric values, lets test this property by giving a value which is alphanumeric-
As you would have noticed, we have used another type of assignment. I have assigned the value of z by using double quotation symbols or “ “. But also notice that the value I have given is alphanumeric. Now you would have understood why I haven’t used the ToString keyword. For those who don’t, well, we don’t need to convert a String into a String do we?! Run the code-
Nothing succeeds like success.
8. Now we can do another thing in order to optimize our code. By saying thus, I mean we can actually integrate both the ‘declaration’ and the ‘assignment’ part into a single line of code. This can be done with the following syntax-
<data-type><variable-name> = <value>;
This can be shown by an example-
On running the code, we get the same result-
9. Now a very important thing-Remember that C# is a ‘case-sensitive’ programming language. That means saying ‘s’ and ‘S’ in C# are totally different. Try working with other data-types like double, float and Boolean.
That is your food for thought for today. Any queries can be asked by posting in the comments section. I will reply as soon as possible. Till then, hold my breath!!