Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Win7 VS2010
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 29-Jun-12 23:06pm
Edited 30-Jun-12 1:51am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This code might help you....
 
// 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
*/
  Permalink  
Comments
Rahul Rajat Singh at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)
 
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());
           
        }       
    }
}
  Permalink  
v2
Comments
jophyjob at 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 : IBase2 , 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)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello All,
I get it .... the code of ICollection<T> See is
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 like this,
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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100