Posts Tagged ‘ WP ’

Namespaces

I am still at the very beginning with my attempt to learn C# and ultimately writing apps for Windows Phone 7. That’s the reason why I have to learn so much more. And by writing stuff down, I can memorize it better.

So, if any of my readers think, that is boring what I am writing about, then you probably already know way more than I do. For those who don’t think this is boring stuff: You might be a beginner as well, or – in the best case – you like the way I am writing. Anyway, if you are a beginner, then you might want to write down everything new you learn.

Having said that, I happen to see the word “namespace” in each and every project in Visual Studio, so I thought it might make sense to have a closer look.

A namespace is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols. An identifier defined in a namespace is associated with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace.

For example, Tom works for company X and his employee ID is 123. Jane works for company Y and her employee ID is also 123. The reason Bill and Jane can be identified by the same ID number is because they work for different companies. The different companies in this case would symbolize different namespaces. There would be serious confusion if the two people worked for the same company, and still had the same employee ID.

Data storage devices and many modern programming languages support namespaces. Storage devices use directories (or folders) as namespaces. This allows two files with the same name to be stored on the device as long as they are stored in different directories.

In the .NET Framework Base Class Library all the classes are organized into namespaces. You might want to look at it as a last name for classes. For example, my first name is “Andrea” and there are thousands more with that name. So with adding a namespace, my last name, I can specify that name.

Let’s have a look at some code:

The class I want to create a new instance of is IsolatedStorageSettings. Everything before that are the namespaces. As you can see they are embedded. The “System” namespace has an “IO” child namespace, which again has an “IsolatedStorage” child namespace. And that “owns” my class, the “IsolatedStorageSettings”.

Many of the classes and namespaces in the .NET Framework Class Library belong to “System”. The “IO” namespace contains namespaces that deal with in/out, or rather getting data into or out of an application. The “IsolatedStorage” namespace further separates out those classes that deal with isolated storage which allows me to save data to the phone’s flash memory.

Now, my line of code is so long, it doesn’t even fit in one line if I want to have a look at it all at the same time. Either I have to scroll through it, or as I did here, I separate it into two lines.

To shorten the amount of code that I have to write, I can rely on the “using” statement. A “using” statement provides access to all namespaces and classes that belong to it. So I don’t have to type all of that out. I am telling the compiler: Hey, I am using those namespaces, so before you complain, go, check the namespaces first to ensure that the class that I am referencing in my code belongs to one of them. If you don’t see the class in of of the namespaces, then go ahead and complain, and I’ll fix the problem.

In my little example there are a series of using statements right in the beginning of my MainPage.xaml.cs file. The new xaml page template adds them by default whenever I create a new xaml page:

Very convenient, so I don’t have to add them myself.

Some of them are used immediately, some are not.

I could remove using statements, that I know I won’t need or use in my application, or I could add more to it. Removing them is not necessary, because unused using statements are just ignored by the compiler.

Going back to my example: I commented out that immense line of code, and typed in this little line:

By using the key combination “Ctrl” and period a window pops up, offering me the using statement. Selecting that, the color changes from black to blue, indicating that I am working with a class name. Also what happens is that Visual Studio adds that using statement to that series of using statements I pointed out before:

That simple trick cuts my long line of code almost in half:

So far for now.

To be continued…

 

 

 

Advertisement

Creating and calling simple helper methods

Methods are named blocks of code. That means, you can call methods by name and their code blocks will execute. But how can I call them? And can I pass values into a method or retrieve values from a method? Why would I even want more than one block of code?

First thought coming into my mind is that if I find myself repeating some code, I want to make that code easier. I don’t want to type the same code twice or even more. So I put that in a block of code, a method. Having less code makes it easier to find mistakes (assuming I make mistakes), because it is well-arranged.

An example for a simple helper method is a button click event handler, that triggers the code inside that block whenever a button is clicked.

As you can see I have my Click Event Handler, in which I reference and call my method (superFormula). It only executes after calling it..

Let’s say, you want to be addressed directly, instead of reading just the words “Hello World”. By putting a certain variable, a parameter in your method, that can be achieved. In my example that parameter would be “myName”. I am assigning the value of that variable to ”Andrea”:

Of course, this doesn’t look too exciting, but think about it. You could set that value to any name, and no additional coding would be necessary.

To be continued…

Iteration Statements

Let me dig right into some code:

 

Obviously I want some code being executed when I click my Button. But what? Well, first of all I created a temporary variable, that is used to iterate through this code block a couple of times. In this case I have an integer “i” that starts with the value of “0”. The next thought is as long as the value of “i” is less than 10 the code beneath is executed again. The “i++” simply says that each time the value of “i” is increased by “1”. For each new value a new message is created, using the ToString method.

The string “System.Environment.NewLine” is called a Property, in which “System” is the namespace, “Environment” is a class name. NewLine is responsible for starting a new line for each number in the message. Finally, when the conditions are no longer true, meaning, when I iterated through all possible conditions, I move out of that “ for loop” and get the final message:

Not very exciting so far. But when I want to find a certain value, say “7”, then I can iterate through this code block until I have found that value:

Once that condition is met, that code breaks out of that “for loop” and gives the final message.

I start really having fun with C#. Can’t wait to actually develop a real application on my own. For that to happen, I will have to learn more, so be sure that this blog is…

… to be continued…

%d bloggers like this: