NEWBIE: Using same function for different objects

  • Thread starter Thread starter Magennis Weate
  • Start date Start date
M

Magennis Weate

I am really missing something here:

·I Have two unrelated objects X and Y
·These two objects share some of the same method calls

I want to write just one function that I can pass either X or Y to
which will then call methods on the object. I don't want to use
polymorphism as the handling of the object will be exactly the same –
so I should be able to use just one function for both objects.

I have tried declaring the parameter of the function as "object" but
then when I call the methods on this parameter variable the language
complains that "object" does not have this method.

Thanks.
 
Try declaring an interface and have both classes implemen the interface.
Have the interface as the parameter type to the method that needs to handle
either object.

example:

public interface IMyInterface
{
void MethodA();
}

public class ObjectX : IMyInterface
{
public void MethodA()
{
//code
}
}

public class ObjectY : IMyInterface
{
public void MethodA()
{
//code
}
}


Now...you mentioned that you have a method that needs to accept either
object type.

public FunctionExample(IMyInterface genObject)
{
//code
genObject.MethodA();
}

HTH
 
Magennis,
I used notepad to type the code....serves me right. Anyway, there may be
other typos but there is one I found at the end. I forgot to declare the
method with a void.

public void FunctionExample(IMyInterface genObject)
{
//code
genObject.MethodA();
}
 
As Lateralus pointed out, any time you have one or more method or property
signatures in objects that aren't related by inheritance, an interface
should be considered. However, there are downsides. One is that the
interface implementation can't be inherited. That doesn't sound like a
problem in your scenario, but it might be if you intend to subclass X or Y
later on. In that case a possible work-around is to have virtual methods do
the actual implementation and the interface implementation would simply call
those virtual methods.

The other way around this is what you attempted, but you will have to test
for the object type and cast as appropriate, for example:

public virtual void TheMethod(object o) {

if (o is X) {
X xInstance = (X)o;
xInstance.DesiredMethod();
} else if (o ix Y) {
Y yInstance = (Y)o;
yInstance.DesiredMethod();
} else {
throw new ArgumentException("TheMethod() received an unsupported
type.");
}

}

The obvious problem is that due to strong typing / early binding, you will
likely end up cutting and pasting code just to do the same or similar things
with each different type supported.

Another possibility is to use Reflection to late-bind based on type. One of
those things I've done months ago but couldn't tell you how I did it. You
just hit the books and figure it out and hope you never have to look at the
resulting ugly code again.

This is one of those times where Ruby or FoxPro -- something loosely
typed -- gets in your way less, because they don't care about the type, just
whether the method you want to call exists on the target instance or not.
Of course, you lose the benefits of type safety that way, too.

--Bob
 
Mageniss... As suggested, you can declare an interface MyInterface and
write
an abstract class that implements the interface, AbstractMyInterface. X
and Y
can then inherit from the abstract class. If necessary, you can use
containment and delegation to simulate multiple inheritance of
implementation.

Regards,
Jeff
I want to write just one function that I can pass either X or Y to
which will then call methods on the object. I don't want to use
polymorphism as the handling of the object will be exactly the same –
so I should be able to use just one function for both objects<
 
I should say that declaring the class abstract simply means that you
cannot create an instance of the class. The concrete implementation does
not need to be virtual.
using System;

namespace TestAbstract1
{
interface MyInterface
{
void MyMethod();
}
abstract class AbstractMyInterface
{
public void MyMethod() {System.Console.WriteLine("Out");}
}
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1: AbstractMyInterface
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Class1 c= new Class1();
c.MyMethod();
System.Console.ReadLine();
}
}
}
Regards,
Jeff
 
Did you try casting the object parameter back to the original type? This
could be the problem.

Check out this sample code - you could easily add a swich case verifying the
type of the argument and perform the required operations.

static void Main(string[] args)
{
DirectoryInfo info = new DirectoryInfo("C:\\");
DirectoryInfo[] subFolders = info.GetDirectories();
Print(subFolders);
}

public static void Print(object args)
{
foreach(DirectoryInfo info in (DirectoryInfo[])args)
{
Console.WriteLine(info.Name);
}
}
 
Back
Top