Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

I want to implement two interfaces to one class and use only one definition for function like Microsoft Collection< T > interface Add function . Is it possible

I attach a link to the project Link

and image attached link is Image link

With regards ,
Jophy
Posted
Updated 30-Jun-12 0:51am
v2

This code might help you....

C#
// Declare the English units interface:
interface IEnglishDimensions
{
    float Length();
    float Width();
}

// Declare the metric units interface:
interface IMetricDimensions
{
    float Length();
    float Width();
}

// Declare the Box class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions
{
    float lengthInches;
    float widthInches;

    public Box(float length, float width)
    {
        lengthInches = length;
        widthInches = width;
    }

    // Explicitly implement the members of IEnglishDimensions:
    float IEnglishDimensions.Length()
    {
        return lengthInches;
    }

    float IEnglishDimensions.Width()
    {
        return widthInches;
    }

    // Explicitly implement the members of IMetricDimensions:
    float IMetricDimensions.Length()
    {
        return lengthInches * 2.54f;
    }

    float IMetricDimensions.Width()
    {
        return widthInches * 2.54f;
    }

    static void Main()
    {
        // Declare a class instance box1:
        Box box1 = new Box(30.0f, 20.0f);

        // Declare an instance of the English units interface:
        IEnglishDimensions eDimensions = (IEnglishDimensions)box1;

        // Declare an instance of the metric units interface:
        IMetricDimensions mDimensions = (IMetricDimensions)box1;

        // Print dimensions in English units:
        System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
        System.Console.WriteLine("Width (in): {0}", eDimensions.Width());

        // Print dimensions in metric units:
        System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
        System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
    }
}
/* Output:
    Length(in): 30
    Width (in): 20
    Length(cm): 76.2
    Width (cm): 50.8
*/
 
Share this answer
 
Comments
Rahul Rajat Singh 30-Jun-12 4:36am    
+5 for working code and good answer. I am not sure what the OP is really asking for but still good effort. even i am trying to answer based on my understanding. the only difference between my answer and yours is that i moved some checking inside the class rather than from the caller end.
1. I am not sure I understood why you might want to do this.
2. If you really NEED to do something like this then perhaps you need to rethink design.
3. code from Member 8070578 seems correct from logic standpoint but you will have to see if it helps you. (nevertheless +5 to that answer for giving some working code.)
4. I have a small example in which you can implement all interface functions with same name in a class and then expose a single function outside. inside this single function you will have to see which actual implementation needs to be called. (although this is working code, i would not recommend this at all. as if we are doing something like this their is some serious design flaw)

C#
namespace C1L1
{
    interface one
    {
        int Add(int a, int b);
    }

    interface Two
    {
        int Add(int a, int b);
    }

    public enum TYPE
        {
            SOME_TYPE,
            SOME_OTHER_TYPE
        }

    class dummy : one, Two
    {
        TYPE type_;
        public dummy(TYPE type)
        {
            type_ = type;
        }
        #region Two Members

        int Two.Add(int a, int b)
        {
            Console.WriteLine("Two Add called");
            return a + b;
        }

        #endregion

        #region one Members

        int one.Add(int a, int b)
        {
            Console.WriteLine("One Add called");
            return a + b;
        }

        #endregion

        public int Add(int a, int b)
        {
            Console.WriteLine("common Add called");
            int result;
            if (type_ == TYPE.SOME_TYPE)
            {
                result = ((one)this).Add(a, b);
            }
            else
            {
                result = ((Two)this).Add(a, b);
            }
            
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            dummy d1 = new dummy(TYPE.SOME_TYPE);
            Console.WriteLine(d1.Add(2, 3).ToString());

            dummy d2 = new dummy(TYPE.SOME_OTHER_TYPE);
            Console.WriteLine(d2.Add(2, 3).ToString());
           
        }       
    }
}
 
Share this answer
 
v2
Comments
jophyjob 30-Jun-12 6:39am    
I like to do is that different type of parameters in function
like this
interface IBase
{
void add(object o);
}

interface IBase2< T >
{
void add(T o);
}
and use like

class AA<t> : IBase2<t> , IBase
{
public void add(T o) { } // Is it posible to work only by this
// implimentation
}
Microsoft "Collection < T > " class use this way in Add(T item)
Hello All,
I get it .... the code of ICollection<T> See is
C#
public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
    {

really the code have the implementation of two Add functions
public void Add(T item){}

And 
void IList.Add(object item) { this.Add((T) item); } // explicit implementation


If you see with DeCompiler, you can see this implementation under List<t> like this,
XML
private int System.Collections.IList.Add(object item)
   {
       List<T>.VerifyValueType(item);
       this.Add((T)item);
       return this.Count - 1;
   }

since this scope is private for IList.Add So you are not able to see in object browser.
Thank you All
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900