If you a try to host a service in Windows Vista, you will receive an AddressAccessDeniedException. It is because of overly secured environment of the Windows Vista.

You will receive an error stating

HTTP could not register URL http://+:8000/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).

You get this error because the owner of the http publishing is the built in administrator and you will not be running with administrator privileges.

You have to add the HTTP namespace to your user account. For that run the Command Prompt as an Administrator and run this command:

netsh http add urlacl url=http://+:8000/ user=DOMAIN\UserName 

You can more details here

To start a thread with a method having parameters, one cannot use the traditional way of starting a thread.

The best way is to use a Anonymous Delegate to start a thread.

Here’s the sample:

MyParameteredMethod is my method which accepts 3 parameters. I wish to start this method in a new thread.

MyParameteredMethod is my paramet
ThreadStart ts = delegate() { MyParameteredMethod(param1, param2, param3); };
Thread t = new Thread(ts);
t.Start();

Well, I got couple of websites where there are good number of videos for learning Windows Communication Foundation (WCF).

You can download them here at Mike Taulty’s Blog.

If you want webcasts for WCF then you can have them here.

INTRO 

·         <%@ Page Language=”VB” %> Directives are used primarily to provide the compiler with the information it needs to compile the page. 

·         The next part of the page begins with the opening <script runat=”server”> tag and ends with the closing </script> tag. The <script> tag contains something called the code declaration block. The Code Declaration Block contains all the functions and methods of the page.

·         The final part of the page is called the page render block. The page render block contains everything that is rendered to the browser. The majority of the page render block consists of everyday HTML. For example, the page contains the standard HTML <head> and <body> tags.

·         You can add an <%@ Import %> directive to a page to import a particular namespace. For eg. <%@ Import Namespace=”System.Net.Mail” %> If you are using the Namespace in all the pages of your website you can add it in the web.config file.

·         A web configuration file is a special type of file that you can add to your application to configure your application. Be aware that the file is an XML file and, therefore, all the tags contained in the file are case sensitive. You can add a web configuration file to your application by selecting Website, Add New Item and selecting Web Configuration File.

·         An assembly is the actual .dll file on your hard drive where the classes in the .NET Framework are stored. For example, all the classes contained in the ASP.NET Framework are located in an assembly named System.Web.dll. More accurately, an assembly is the primary unit of deployment, security, and version control in the .NET Framework. Because an assembly can span multiple files, an assembly is often referred to as a “logical” dll.

 CONTROLS

·         The first part of the control declaration, the asp: prefix, indicates the namespace for the control. All the standard ASP.NET controls are contained in the System.Web.UI.WebControls namespace. The prefix asp: represents this namespace.

·         All ASP.NET control events happen on the server. For example, the Click event is not raised when you actually click a button. The Click event is not raised until the page containing the Button control is posted back to the server.

·         You can determine how much View State each control contained in a page is consuming by enabling tracing for a page. This includes a trace=”true” attribute in its <%@ Page %> directive, which enables tracing.

 DYNAMIC COMPILATION

·         When you create an ASP.NET page, you are actually creating the source code for a .NET class. You are creating a new instance of the System.Web.UI.Page class. The entire contents of an ASP.NET page, including all script and HTML content, are compiled into a .NET class.

·         When you request an ASP.NET page, the ASP.NET Framework checks for a .NET class that corresponds to the page. If a corresponding class does not exist, the Framework automatically compiles the page into a new class and stores the compiled class (the assembly) in the Temporary ASP.NET Files folder. The next time anyone requests the same page in the future, the page is not compiled again. The previously compiled class is executed and the results are returned to the browser. When the class is added to the Temporary ASP.NET Files folder, a file dependency is created between the class and the original ASP.NET page. If the ASP.NET page is modified in any way, the corresponding .NET class is automatically deleted. The next time someone requests the page, the Framework automatically compiles the modified page source into a new .NET class. This process is called dynamic compilation. Dynamic compilation enables ASP.NET applications to support thousands of simultaneous users. Unlike an ASP Classic page, for example, an ASP.NET page does not need to be parsed and compiled each and every time it is requested. An ASP.NET page is compiled only when an application is modified.

  CODE BEHIND FILE

·         A two-file ASP.NET page is normally referred to as a code-behind page. In a code-behind page, the page code is contained in a separate file.

·         Code-behind pages work in a different way in the ASP.NET 2.0 Framework than they did in the ASP.NET 1.x Framework. In ASP.NET 1.x, the two halves of a code-behind page were related by inheritance. In the ASP.NET 2.0 Framework, the two halves of a code-behind page are related by a combination of partial classes and inheritance.

·         The ASP.NET 2.0 Framework generates three classes whenever you create a code-behind page. The Presentation page and the Code behind file have Partial Classes. There is another class connected to Presentation Page which inherits from the Partial Class. Therefore the Presentation Page has two classes and The Code behind file has one class.

·         The page which contains the HTML controls is called the Presentation Page and the page which contains the Code i.e. the Load method, the event handlers, etc is called the Code behind file.  

·         The difference between these two events is that the Load event happens before any control events and the PreRender event happens after any control events.

·         ASP.NET pages support a feature named AutoEventWireUp, which is enabled by default. If you name a subroutine Page_Load(), the subroutine automatically handles the Page Load event; if you name a subroutine Page_PreRender(), the subroutine automatically handles the Page PreRender event, and so on. 

STANDARD CONTROLS

·         By default, a Label control renders its contents in an HTML <span> tag. Whatever value you assign to the Text property is rendered to the browser enclosed in a <span> tag.

·         The Label control includes a property named the AssociatedControlID property. You can set this property to point at an ASP.NET control that represents a form field. A side benefit of using the AssociatedControlID property is that clicking a label when this property is set automatically changes the form focus to the associated form input field.

·         Because the contents of a Literal control are not contained in a <span> tag, the Literal control does not support any of the formatting properties supported by the <span> tag. For example, the Literal control does not support either the CssClass or BackColor properties.

·         When the AutoPostBack property has the value true, the form containing the TextBox is automatically posted back to the server when the contents of the TextBox changes.

·         The button controls, like most ASP.NET controls, support expando attributes, you can handle other client-side events simply by adding an arbitrary attribute to the control. If the ASP.NET Framework does not recognize an attribute declared on a button control, the framework simply passes the attribute to the browser.

·         The Panel control enables you to work with a group of ASP.NET controls. You can use a Panel control to hide or show a group of ASP.NET controls.

·         Don’t use the values Horizontal or Vertical with the ScrollBars property when you want the scrollbars to appear in browsers other than Microsoft Internet Explorer. If you want the scrollbars to appear in FireFox and Opera, use either the value Auto or Both.

·         The HyperLink control enables you to create a link to a page. Unlike the LinkButton control, the HyperLink control does not submit a form to a server.      

What can be one of the most significant steps forward in .net framework deveopment is the availability of the source code to the .net class library.

You can know more about this in the post of Scott

The preview on ASP.NET 3.5 extensions has been posted here along with some cool links for much needed tutorials on MVC architecture. You can also download the Extensions which are compatible with VS2008 beta.

There are different modifiers which decide the visibility of the types or members.

They are:

Modifier

Applies To Description
public Any types or members The item is visible to any other code.
protected Any member of a type, also any nested type The item is visible only to any derived type.
internal Any member of a type, also any nested type The item is visible only within its containing assembly.
private Any types or members The item is visible only inside the type to which it belongs.
protected internal Any member of a type, also any nested type The item is visible to any code within its containing assembly and also to any code inside a derived type.

Some other modifiers are:

Modifier

Applies To Description
public Any types or members The item is visible to any other code.
protected Any member of a type, also any nested type The item is visible only to any derived type.
internal Any member of a type, also any nested type The item is visible only within its containing assembly.
private Any types or members The item is visible only inside the type to which it belongs.
protected internal Any member of a type, also any nested type The item is visible to any code within its containing assembly and also to any code inside a derived type.

There are two types of inheritence implemented in C#. Implementation Inheritence and Interface Inheritence.

Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions. With implementation inheritance, a derived type adopts the base type’s implementation of each function, unless it is indicated in the definition of the derived type that a function implementation is to be overridden. This type of inheritance is most useful when you need to add functionality to an existing type, or where a number of related types share a significant amount of common functionality.

Interface inheritance means that a type inherits only the signatures of the functions but does not inherit any implementations. This type of inheritance is most useful when you want to specify that a type makes certain features available. Interface inheritance is often regarded as providing a contract: By deriving from an interface, a type is guaranteed to provide certain functionality to clients.

Imp: C# doesnot implement Multiple Inheritence i.e. you cannot inherit a class from more than one class.

By declaring a base class function as virtual, you allow the function to be overridden in any derived classes.

class MyBaseClass
{
   public virtual string VirtualMethod()
   {
      return “This method is virtual and defined in MyBaseClass and can be overridden in                   derievd class”;
   }
}

You use the override keyword when you write a new implementation of the virtual method in the derieved class.

public override string VirtualMethod() //this is the method which is overriden.

Neither member fields nor static functions can be declared as virtual.

Hiding Methods

If a method with the same signature is declared in both base and derived classes, but the methods are not declared as virtual and override, respectively, then the derived class version is said to hide the base class version.In these situations, C# generates a compilation warning. That reminds you to use the new keyword to declare that you intend to hide a method.

you can use the base.<MethodName>() syntax to call any method in the base class .

C# allows both classes and functions to be declared as abstract. An abstract class cannot be instantiated, whereas an abstract function does not have an implementation, and must be overridden in any non-abstract derived class. Obviously, an abstract function is automatically virtual (although you don’t need to supply the virtual keyword; doing so results in a syntax error).

C# allows classes and methods to be declared as sealed. In the case of a class, this means that you can’t inherit from that class. In the case of a method, this means that you can’t override that method.

Constructors in the hierarchy

If all the classes in the hierarchy have only default constructors then when u instantiate a class the control goes to the constructor of its immediate base class from there it goes to its immediate base class…like that till it reaches System.Object. Since System.Object doesnot have any base class…its constructor is executed…then its next derieved class’ cons is exec…till it reaches the cons of the class to be instantiated.

When you write your own constructors…stick to this principle.The base and this keywords are the only keywords allowed in the line that calls another constructor. Anything else causes a compilation error. Also note that only one other constructor can be specified.

We’ll take an example to understand this:

We have class called Employee it just has a field indicating the EmpId

class Employee
{
      private string empId;
      public Employee()
     {
      }
}

Then you have a class called ContractEmployee which is inherited from Employee and has an extra field of number of contract months.
class ContractEmployee: Employee
{
       private int contractMonths;
       public ContractEmployee():base()
      {
      }
}

In the above case when the class ContractEmployee is instantiated then the default cons of its base class is called and goes upto Object class. If you want to instantiate the employee field with employee id i.e if the user has to produce the employee id of the new emp while creating the class itself then the constructor of the Employee class takes a parameter and takes the following form

class Employee
{
      private string empId;
      public Employee(string id)
     {
          this.empId = id;
     }
}

Now when you instantiate the derieved class i.e the ContractEmployee class then you have to supply name to the emp…but that field is a private member of the base class. So what you have is sth like this:

class ContractEmployee: Employee
{
       private int contractMonths;
       public ContractEmployee(string id):base(id)
      {
      }
}

The derieved class has no way to implement this but the base class can do that so what the former does is to pass on the parameter to the base class cons.

Now the req is that the emp who referred to the conractemp should be saved in a field of referrer.

class ContractEmployee: Employee
{
       private int contractMonths;
       private string referrer;
       public ContractEmployee(string id,string refr):base(id)
      {
            this.referrer = refr
      }
}

But there may be some contract emp who have no referrers for them there would be no referrers. For them you should have a one parameter cons.Note how this is implemented….the derieved class now has two constructors with two parameters.

class ContractEmployee: Employee
{
       private int contractMonths;
       private string referrer;
       public ContractEmployee(string id,string refr):base(id)
      {
            this.referrer = refr
      }
     public ContractEmployee(string id):this(id,”none”)
      {      
      }

}

thi is how more than one constructors are implemented.When a contract emp with no ref is instantiated then cons with one param is called which in turns call the cons with 2 params giving the referrer as “none” then the base classes single param cons is called.

The syntax for a constructor is tht you declare a method that has the same name as the containing class and that does not have any return type:
public class MyClass
{
    public MyClass()
  {
  }
  // rest of class definition

if you don’t supply any constructor, the compiler will just make up a default one for you behind the scenes. It’ll be a very basic constructor that just initializes all the member fields by zeroing them out (null reference for reference types, zero for numeric data types, and false for bools).you can provide as many overloads to the constructor as you want, provided they are clearly different in signature.however, that if you supply any constructors that take parameters, the compiler will not automatically supply a default one. This is done only if you have not defined any constructors at all.

You can specify access specifiers for constructors. If you specify a constructor as private then it is impossible for that class to get instantiated. It is useful in couple of occasions such as:

  • If your class serves only as a container for some static members or properties and therefore should never be instantiated

  • If you want the class to only ever be instantiated by calling some static member function (this is the so-called class factory approach to object instantiation)

  • Static Constructor

    Static Constructor is the one that can be executed only once. It is exec only one time  unlike instance constructors which are exec for every instance.One reason for writing a static constructor is if your class has some static fields or properties that need to be initialized from an external source before the class is first used.

    The .NET runtime makes no guarantees about when a static constructor will be executed, so you should not place any code in it that relies on it being executed at a particular time (for example, when an assembly is loaded). Nor is it possible to predict in what order static constructors of different classes will execute. However, what is guaranteed is that the static constructor will run at most once, and that it will be invoked before your code makes any reference to the class. In C#, the static constructor usually seems to be executed immediately before the first call to any member of the class.

    the static constructor cannot ever take any parameters, and there can only be one static constructor for a class. It should also be obvious that a static constructor can only access static members, not instance members, of the class.It can’t hav any access modifiers.Note that it is possible to have a static constructor and a zero-parameter instance constructor defined in the same class.Note that if you have more than one class that has a static constructor, the static constructor that will be executed first is undefined.

    public MyClass(string description) : this(model, 4)
       {
       }

    the this keyword allows us to call the constructor in the same class with the matchin signature.It will be the first constructor to get execueted and then the remaining code of the callin constructor is executed. If you use base keyword then it will call the matchin constructor of the base class.

    Syntax for the properties is
    [modifier] return_type PropertyName
    {
             get
            {
                  //return value
            }
             set
           {
             //do set the val here…
           }
    }

    The get accessor takes no parameters and must return the same type as the declared property. You should not specify any explicit parameters for the set accessor either, but the compiler assumes it takes one parameter, which is of the same type again, and which is referred to as value.
    It is possible to create a read-only property by simply omitting the set accessor from the property definition.
    It is similarly possible to create a write-only property by omitting the get accessor.

    The get and set accessor can have different access modifiers

    public string ExProperty
    {
             get
            {
                  //return value
            }
             private set
           {
             //do set the val here…
           }
    }

    Here the set accessor has private accessibility. Atleast one should have access modifier of the Property. Here if the scope of get were to be Protected then it would have resulted in a compile error.

    « Previous PageNext Page »