Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create method on different class types

Tags:

c#

types

class

I have two classes:

 public class MyClass1 
 {
    public string AAAAA { get; set; }
    public string BBBBB { get; set; }
    public string CCCCC { get; set; }
    public string DDDDD { get; set; }
 }

 public class MyClass2 
 {
     public string AAAAA { get; set; }  // same as MyClass1 
     public string BBBBB { get; set; }  // same as MyClass1 
     public string TTTTT { get; set; }  
 }

Now I want to create this method:

public string PrintMe( Class1or2 obj) {
    string message = "";
    message += obj.AAAAA ;  // this parameter is in both MyClass1 and MyClass2
    message += obj.BBBBB ;  // this parameter is in both MyClass1 and MyClass2
    return message;
}

The point is – that I can't touch MyClass1 and MyClass2 so I can't make them use the same base. They are totally separated.

Is any way to create this method somehow or I'll have do duplicate it for each class?

like image 290
TamarG Avatar asked Jun 11 '19 06:06

TamarG


2 Answers

There is an option to change the signature of the method to PrintMe(dynamic obj).

At compile time it will accept any object, and only on run time it will check if the obj instance actually has a property that matches. As you can feel, this is quite unsafe and often leads to bugs in production releases.

There isn't really another option. If you can't change the class, but if you can inherit it, you could implement an interface that shares those properties. That only works if you actually create the instances yourself.

Another option would to use a wrapper class:

public string PrintMe(Class1or2Wrapper obj)
{ ... }

Then you implement the logic of determining which property to take there:

public class Class1or2Wrapper
{
    private Class1 c1;
    private Class2 c2;

    public Class1or2Wrapper(Class1 c1)
    {
        this.c1 = c1;
    }

    public Class1or2Wrapper(Class2 c2)
    {
        this.c2 = c2;
    }

    public string AAAAA
    {
        get
        {
            if (this.c1 != null)
                return c1.AAAAA;

            if (this.c2 != null)
                return c2.AAAAA;

            return null;
        }
    }
}

This way you ensure type safety, while limiting the amount of work.

like image 97
Patrick Hofman Avatar answered Oct 20 '22 04:10

Patrick Hofman


Well, base class of every class is object, so you could hide common implementation as private method:

private string PrintMe( object obj) {
  var instance = obj is MyClass1 ? obj as MyClass1 : obj as MyClass2;

  if(instance == null)
    throw new ArgumentException("Invalid type!");

  string message = "";
  message += instance.AAAAA ;  // this parameter is in both MyClass1 and MyClass2
  message += instance.BBBBB ;  // this parameter is in both MyClass1 and MyClass2
  return message;
}

and expose public API, which will be compile time safe:

public string PrintMe(MyClass1 mc)
{
  return PrintMe(mc as object);
}
public string PrintMe(MyClass2 mc)
{
  return PrintMe(mc as object);
}
like image 24
Michał Turczyn Avatar answered Oct 20 '22 04:10

Michał Turczyn