Home > Mobile >  How to hand over different objects to a Method
How to hand over different objects to a Method

Time:02-18

I like to do something like this but have no idea how to solve it. Let's say I have different classes programmed against and Interface:

public class A : IMyInterface
{
    public string name = "NameOfA";
    public string description = "descriptiontextA";
}
public class B : IMyInterface
{
    public string name = "NameOfB";
    public string description = "descriptiontextB";
}

public class programm 
{
    void DoSmthSpecial(var class) 
    {
        Console.WriteLine($"This is {class.name}");    
    }
}

Where var class should be class A or class B. So basically I am calling similar methods for different classes. Can someone help here?

CodePudding user response:

Your interface should define any common properties:

public interface IMyInterface
{
    string Name {get;}
    string Description {get;}
}

Which are then implemented by your classes:

public class A : IMyInterface
{
    public string Name {get;} = "NameOfA";
    public string Description {get;} = "descriptiontextA";
}
public class B : IMyInterface
{
    public string Name {get;} = "NameOfB";
    public string Description {get;} = "descriptiontextB";
}

And now your DoSmthSpecial method can make use of this interface:

void DoSmthSpecial(IMyInterface myClass) 
{
    Console.WriteLine($"This is {myClass.Name}");    
}

You can call it like this:

IMyInterface myInst = new A();
DoSmthSpecial(myInst);

Or like this:

A myInst = new A();
DoSmthSpecial(myInst);

Try it online

CodePudding user response:

The idea of an interface is it states the shape of some implementing class to code that wants to interact with objects that have a specific shape/capable of behaving in some general way, but aren't bothered about what the implementing class actually does. This can take the form of properties and methods

public interface IMyInterface{
   string Name {get;set;}

   string GetSomethingCool();
}

public class A : IMyInterface
{
    public string Name {get;set;} = "NameOfA";

    public string GetSomethingCool() => "SomethingCool";
}

public class B : IMyInterface
{
    public string Name {get;set;} ;

    public string GetSomethingCool() => "S"   "OMETHING".ToLower()   string.Concat(new[] {'C', 'o', 'o', 'l' });

    public B(){
        Name = Guid.NewGuid().ToString();
    }
}

public class Program
{
    static void DoSmthSpecial(IMyInterface x) 
    {
        //here you can use anything that is available on IMyInterface, property or method
        Console.WriteLine(x.GetSomethingCool());

        Console.WriteLine(x.Name);

        //avoid the temptation to inspect the type of x and do subclass specific stuff
        if(x is A a) 
            a.SomethingOnlyOnA();    //avoid
    }

    static void Main(){

        //can pass an A or a B in as something that validly implements IMyInterface
        DoSmthSpecial(new A()); 
        DoSmthSpecial(new B()); 
       
    }
}

The two classes have very different approaches to returning the same string from the GetSomethingCool() method, and different strategies for setting the Name property - the calling code doesn't know or care how the value is generated; it just knows from the interface that there is a method/property with some name it can call and get/set the data/perform the actions of the method.

CodePudding user response:

Use Generics like this

void DoSmthSpecial<T>(T class) 
{
    // your code here    
}

And call this method like this.

B objB = new B();
DoSmthSpecial<B>(objB);

CodePudding user response:

Interfaces cannot have properties, but you can certainly group similar functionality of like-classes by making use of sub-classes and inheritance.

using System;

public class MyClass{
    public string name;
    public string description;
    public void MyClassBusiness(){
        Console.WriteLine($"This is {name} (from base class)`");
    }
}

public class A: MyClass
{
    public A(){
        name = "NameOfA";
        description = "descriptiontextA";
    }
}

public class B: MyClass
{
    public B(){
        name = "NameOfB";
        description = "descriptiontextB";
    }
}

public class Program
{
    private static void ExternalLogic(MyClass mc) 
    {
        Console.WriteLine($"This is {mc.name} (externally)");    
    }
    
    public static void Main()
    {
        var a = new A();
        var b = new B();
        ExternalLogic(a);
        ExternalLogic(b);
        a.MyClassBusiness();
        b.MyClassBusiness();
    }
}
  • Related