Monday, May 12, 2008

Visual studio environment


In this lesson you will learn how to work with visual studio also you learn how to debug a code to find out issues with your code.

Solution Explorer and Class view

In visual studio we have 2 very important viewes of the project first is Solution explorer that show all the files that you have in your application also you may have different project in a solution. So this view show files like images, your files that have your classes. Second view is Class view where you have all your classes and data types. You may have one file but 2 different classes in it so you see one file in solution explorer however you will see 2 files in class view.

What is namespace?

Namespace is a space that classes or other data types or even other namespaces are defined. And to access these classes we need to tell c# compiler that we are going to use those classes by using keyword. So when you write using System; it means that you can access classes inside system. For example then we can write Console.WriteLine(“Hello world”); we can also write this code like: System.Console.WriteLine(“Hello World”); so we do not need to write Using System. But it does not make sense writing extra “system.” for each call. Namespaces are used to classify classes. For example system.Text or System.xml when you see these namespace you can understand that what sort of classes they have.

How to Debug?

Sometimes you need to debug your code to see what is wrong with them. to do that all you need go to the line that you want to stop the debugger then press f9 then you will see a red line then when you run the code the application will stop in that line then you can see variable by selecting them with mouse. alos you can add them to watch window. you can press f11 or f10 to continue running. f11 will continue running even inside a method. You will see a clip for that don't wory that much.

Sunday, May 11, 2008

Delegate -Part3

What is the usage of delegate? If you look into the previous part you will see that delegate can accept address of a method then when we call delegate that method will be called. Why we do not call the method directly? (you can see the clip of this lesson click here)


To answer these questions we need to see some samples of delegate and usage of delegate in .net Framework:
First sample:
namespace DelegateFirstSample
{
public class Test
{
public void f()
{
Console.WriteLine("this is a Test");
}
}
}

Let’s say we need to call a method of a class every 2 seconds and we need this functionality for other classes as well (to make it easier let’s say we need to call that method 2 times)

namespace DelegateFirstSample
{
public class Test
{
public void f()
{
Console.WriteLine("this is a Test");
}
}
}

To achieve our goal we better have a class that get a method and run it 2 times (or each 2 seconds)

namespace DelegateFirstSample
{
public delegate void NoifyCallback();
public class Helper
{
public static void Register(NotifyCallback nc,int num)
{
for (int i = 0; i <> num;i++)
nc();
}