user2354204
user2354204

Reputation:

Implementing a Generic C# class using method in common to others

I have the following problem in C#.

I have two classes exposing some common members and methods, for example:

Class myClass01{
   public int a;
   public int b;
   public void method_01(){...}
   public void method_A(){...}
}

and

Class myClass02{
   public int a;
   public int b;
   public void method_01(){...}
   public void method_B(){...}
}

I want to create a new generic class that can use the common attributes/methods of both of them. For example:

Class myGeneric<T>{
   public T element;

   public int sum(){
       return element.a + element.b;
   }

   public void methodGeneric(){
       element.method_01();
    }
}

But with this kind of implementation I receive errors because elements are not accessible. Is there a way to a create a generic class using methods/attributes from different classes but equally defined?

Upvotes: 1

Views: 87

Answers (3)

Ian
Ian

Reputation: 30813

The generic class you are making currently does not have a constraint to know from what class the generic type T must be derived from. Thus, it assumes object which does not recognized any customized public/protected fields, properties, and methods you implement.

If you want the generic T to recognize the public/protected fields, properties, and methods you have in common between two classes, you should create an interface which is implemented by both classes and you have the generic type T derived from that particular interface.

For example:

public interface ImyClass { //this is the common interface
    int a { get; set; }
    int b { get; set; }
    void method_01();
}

class myClass01 : ImyClass { //your special interface implementation for myClass01
    public int a { get; set; }
    public int b { get; set; }
    public void method_01() { }
    public void method_A() { }
}

class myClass02 : ImyClass {  //your special interface implementation for myClass02
    public int a { get; set; }
    public int b { get; set; }
    public void method_01() { }
    public void method_B() { }
}

Then in your generic, constraint the generic type with the interface

class myGeneric<T> where T : ImyClass {
    public T element;

    public int sum() {              
        return element.a + element.b;
    }

    public void methodGeneric() {
        element.method_01();
    }
}

You could also use abstract or simple base class if the implementation for both methods (both method_01) happen to be exactly the same, for example:

public abstract class myAbstractClass { //this is the common abstract class or base class
    public int a;
    public int b;
    public void method_01(){ } // all your common implementations
}

public class myClass01 : myAbstractClass {
    public void method_A() { } //your special implementation for myClass01
}

public class myClass02 : myAbstractClass {
    public void method_B() { } //your special implementation for myClass02
}

class myGeneric<T> where T : myAbstractClass { //generic specifically from myAbstractClass 
    public T element;

    public int sum() {              
        return element.a + element.b;
    }

    public void methodGeneric() {
        element.method_01();
    }
}

In any case, a common inheritance is required to recognize the public/protected fields, properties, and methods.

Upvotes: 4

pvill
pvill

Reputation: 185

Note that in addition to Ians answer when using an interface, the generic is not necessary anymore you could simple use

class myGeneric {
    public ImyClass element;

    public int sum() {              
        return element.a + element.b;
    }

    public void methodGeneric() {
        element.method_01();
    }
}

Although it was not the actual question

Upvotes: 0

Murray Foxcroft
Murray Foxcroft

Reputation: 13765

Use Interfaces and "where" to get the members accessible. E.g.:

public class SortedList<T> : GenericList<T> where T : System.IComparable<T>

https://msdn.microsoft.com/en-us/library/kwtft8ak.aspx

Upvotes: 0

Related Questions