Inheritance design: How to make a method operate on base class (C#)

I hope the title of this question summarizes the problem accurately. Here are the details:

I have two classes that share several properties and related methods, let's MyFirstClass and MySecondClass. So, I put this intersecting set of properties into an abstract class that both my classes inherit from: MyAbstractBaseClass.

So far, so good. However, in a method elsewhere in my code, I am manipulating these shared properties and would like to be able to pass instances of both MyFirstClass and MySecondClass to this method by reference. Something like:

result1 = myMethod(ref MyFirstClass);
result2 = myMethod(ref MySecondClass);

I have tried using MyAbstractBaseClass as the parameter type for my method:

public bool myMethod(ref MyAbstractBaseClass anObject)

But this is not accepted by the compiler. I have also tried to extract the interface from MyAbstractBaseClass and have both MyFirstClass and MySecondClass inherit from MyAbstractBaseClass and implement the interface, like so:

public class MyFirstClass : MyAbstractBaseClass, IMyAbstractBaseClass 
public class MySecondClass : MyAbstractBaseClass, IMyAbstractBaseClass 

I was then expecting that the myMethod would be able to operate on both classes, if I make the parameter of the interface of type IMyAbstractBaseClass (after all, in OOP you're supposed to code against interface when possible).

public bool myMethod(ref IMyAbstractBaseClass anObject)

IMyAbstractBaseClass myObject = new MyFirstClass();
result1 = myMethod(ref myObject);

This isn't working either. The compiler says it is not possible to convert MyFirstClass to IMyAbstractBaseClass, which seems odd to me, because MyFirstClass implements the Interface and therefore I should be able to treat all objects of this class as type interface, no?

What am I missing here?

Answers


Do the parameters really need to be passed by ref?

If they are reference types (i.e. classes) (and they are), and you are only setting properties on them, then they do not need to be passed by reference. (Passing a reference by referene is having a pointer to a pointer)

Only in cases where you will be replacing the whole object do you need the ref:

public bool myMethod(ref IMyAbstractBaseClass anObject)
{
      myObject = new MyFirstClass();
      return true;
}

public bool myMethod2(MyAbstractBaseClass anObject)
{
      myObject.SomeProperty = 5;
      return true;
}

IMyAbstractBaseClass myObject = new MyFirstClass();
result1 = myMethod(ref myObject);
result1 = myMethod2(myObject);

myMethod2() will work exactly as you want.


Do not pass parameters by reference unless for some strange reason you need to assign the method parameter to a different object, which is usually not the way to go. "Ref" parameters do not support abstract classes.

C# is not C++. It is not going to create a copy of the whole object without the ref keyword. It will pass a copy of the reference (basically a pointer) to the object into the method.


Need Your Help

Removing Specific Line from Multiples files (.cpp /.h) in multiple directories

shell cygwin code-review

I have to send code to someone but before that I need to delete comments contains names of different developers who have worked on this project, I looked up online for results, I found a Shell script

UISplitViewController: how to get toolbar if details controller is UITableView?

cocoa-touch monotouch

I checked out Apple's example on how to exchange detail views in the UISplitViewController and it seems that they put the UIToolbar in every detail controller. Then, if the device is rotated, they ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.