IRohitable | All posts by rohit

Accessing Interface methods when using explicit Interface implementation

by Rohit 18. December 2014 20:00

I came across a scenario where there are two Interfaces with same method name. For example:

Interface ILocalCustomer
{
       void CustomerDetails();
}

Interface IGlobalCustomer
{
        void CustomerDetails();
}

 

In this scenario there are some challenges:

  1. I need two separate implementations for CustomerDetails but the method name is same in both the Interface.
  2. I want to call the method from another class (other than the class which implements these Interfaces)

The first part of this problem does not involve much trick. You can have explicit Interface implementations like this:

public class Customer : ILocalCustomer,IGlobalCustomer
    {
        void ILocalCustomer.CustomerDetails()
        {
            Console.WriteLine("Local Customer");
        }

        void IGlobalCustomer.CustomerDetails()
        {
            Console.WriteLine("Global Customer");
        }
    }

To be able to call any of these methods from another class, the first thing comes to your mind is to make the methods Public like this:

public class Customer :ILocalCustomer,IGlobalCustomer
    {
        public void ILocalCustomer.CustomerDetails()
        {
            Console.WriteLine("Local Customer from Interface 1");
        }

        public void IGlobalCustomer.CustomerDetails()
        {
            Console.WriteLine("Global Customer from Interface 2");
        }
    }

The Visual Studio IDE immediately underlines it as red and shows error:

 

Now how to access these methods from another class without any access modifier like Public, Protected etc? This involves little trick.

static void Main(string[] args)
        {
            Customer customer = new Customer();

            ILocalCustomer local = (ILocalCustomer)customer;
            local.CustomerDetails();

            IGlobalCustomer global = (IGlobalCustomer)customer;
            global.CustomerDetails();
        }

Here we are casting instance with the Interface type to be able to access the method implementations in class Customer.

Here is the full example:

public interface ILocalCustomer
    {
        void CustomerDetails();
    }

    public interface IGlobalCustomer
    {
        void CustomerDetails();
    }
    class MainClass
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();

            ILocalCustomer local = (ILocalCustomer)customer;
            local.CustomerDetails();

            IGlobalCustomer global = (IGlobalCustomer)customer;
            global.CustomerDetails();
        }
    }

    public class Customer :ILocalCustomer,IGlobalCustomer
    {
        void ILocalCustomer.CustomerDetails()
        {
            Console.WriteLine("Local Customer from Interface 1");
        }

        void IGlobalCustomer.CustomerDetails()
        {
            Console.WriteLine("Global Customer from Interface 2");
        }
    }

 

Share your thoughts.

 

Tags:

.NET | C#

How to call a non-static method from a static method?

by Rohit 7. December 2014 22:12

To call a non-static method from a static method:

  1. Create an instance of the class
  2. Call the non-static method
class MyClass
    {
        static void Main(string[] args)
        {
            Method1();
        }

        static void Method1()
        {
            MyClass p = new MyClass();
            p.Method2();
        }

        void Method2()
        {
            Console.WriteLine("Called from Static Method");
            Console.Read();
        }
    }

Tags: ,

C# | OOP

Few cool features of ASP.NET 5

by Rohit 23. November 2014 22:53

Built from the ground up to provide optimized framework for application development, ASP.NET 5 includes several new features and enhancements. There are few cool features which are of interest to me:

1. Cross-platform runtime for Linux and Mac OS X: This runtime will enable developers to build .NET apps on Mac and Linux devices. We will see more development in the Mono platform.

2. Open-source and Transparent: Entire ASP.NET code is available on GitHub. The GitHub repository is actively used by the ASP.NET team for all the development where you can find latest code changes.

3. Cloud ready

4. Improved HTTP Performance

5. Agile development environment: You don't need to rebuild the project each time you do code changes. Now you can make changes in the code, save the changes, simply refresh the browser and see the new changes. The code can be in a class-library or in your web project.

Tags:

.NET | ASP.NET

About Rohit Prakash

Software Craftsman and Technology Enthusiast (not a Guru).

Technical Reviewer of a book on open-source programming IDE.

My day job keeps me engaged with Microsoft Technologies (ASP.NET, C# and SQL Server) hence most of the posts are related to these technologies. I, however, love to play with other open-source technologies. I also have interest in IT Security and therefore you will find posts on Malware Analysis, Cryptography and Anti-Virus programs.

Few people have contacted me for Guest Posts. You will find these posts as well.

 

You can reach me at:

rohit [at] irohitable.com

-----------------------------------------

LinkedIn:

in.linkedin.com/in/rohitpkhare

Twitter:

@BuzzRohit

-----------------------------------------
Google+

Search with this name: Rohit Prakash

Month List

Protected by Copyscape Web Plagiarism Software