Loading ...

Automatically implement interfaces in VS.NET | CodeAsp.Net

Automatically implement interfaces in VS.NET

 /5
0 (0votes)

When working with interfaces, it has happened to me many times to have several methods to implement and I needed to write them all manually.

There is one trick you can use to implement the interfaces automatically.

Lets assume we have the following interface:

    interface Product
    {
        void AddProduct();
        void DeleteProduct();
        void UpdateProduct();
        string GetProductId();
        string GetProductName(string productId);
        int CountProducts();
        List<string> GetProductNames(string productId);
    }


and we want to implement it in a class Computers

    public class Computers : Product
    { 
        
    }


So, instead of writing all the method declarations from the interface (otherwise the code wont execute), we can do this otherwise and create method declarations very easily.

 

FIRST WAY

Bring the cursor after the "Computers :" right before the Product and click 'CTRL + .' (dot), then you will have two options on how to implement the interface:

 

the first option will implement the interface normally, while the second, Explicit, option will implement the interface so that the Computers class methods will be accessible only through declaration an instance of the Product interface.

 

SECOND WAY

The second way is similar as the first one. We don't use keyboard shortcuts but just position your mouse arrow over the Product class (public class Computers : Product) and the down arrow will be shown. Click it and chose one of the options as in the first way method.

Once we chose, lets say "Implement interface 'Product'" (not Explicitly), here is the Computers class code:

    public class Computers : Product
    {

        public void AddProduct()
        {
            throw new NotImplementedException();
        }

        public void DeleteProduct()
        {
            throw new NotImplementedException();
        }

        public void UpdateProduct()
        {
            throw new NotImplementedException();
        }

        public string GetProductId()
        {
            throw new NotImplementedException();
        }

        public string GetProductName(string productId)
        {
            throw new NotImplementedException();
        }

        public int CountProducts()
        {
            throw new NotImplementedException();
        }

        public List<string> GetProductNames(string productId)
        {
            throw new NotImplementedException();
        }
    }

so, you see all that code is written for us automatically!

By default, you have an NotImplementedException() to each method. So if any of these method is called and if its not implemented, such exception will be thrown.

This trick can be especially useful when you inherit a class from an interface that is inside the .NET library, especially if the interface has lot of members declared in it.

Hope this was useful info.

Regards,
Hajan

Comments (3)

   
raghav_khunger
Nice!
10/27/2010
 · 
by
   
SumitArora
I'm also doing it like the 1st way as I'm working on WCF where I've to inherit the interface in class.
10/27/2010
 · 
by
   
Tungsten Carbide Rings
I prefer the 1st way
11/7/2010
 · 
by
  • :*
  • :*
  • :
 *

Top Posts