Overloading methods...


O

O.o

I have a method that should accept an input argument of one of several
types (or classes). One way of doing this is to overload the method.
That's fine, but what I've realized is that every version of the
method is identical (except for the type or class of input argument).
This is because the different input types or classes are already
handled by overloaded operators (when necessary). My question then is:
do I really need to copy and paste the exact same code for each type
or class of input argument?

A simple example might be a method called "Add", that takes two
numbers as input and returns a double value representing their sum:

public static double Add( int arg1, int arg2 )
{
return (double)(arg1+arg2);
}

public static double Add( float arg1, float arg2 )
{
return (double)(arg1+arg2);
}

and so on for short, long, decimal, etc..., not to mention any other
types or classes that have an overloaded "+" operator that returns a
double.

OK, so my method is a bit more complicated, but it seems to me that
there ought to be a way for a method to accept arguments from a list
of types. I don't want to use the Object "type", because then I have
to add a bunch of code to check and see if it contains a "legal" type.
I also don't want to re-cast the input data to another type and call a
"default" method, which would increase computational cost and memory
for "small" input types. Is there another way?
 
Ad

Advertisements

M

miher

O.o said:
I have a method that should accept an input argument of one of several
types (or classes). One way of doing this is to overload the method.
That's fine, but what I've realized is that every version of the
method is identical (except for the type or class of input argument).
This is because the different input types or classes are already
handled by overloaded operators (when necessary). My question then is:
do I really need to copy and paste the exact same code for each type
or class of input argument?

A simple example might be a method called "Add", that takes two
numbers as input and returns a double value representing their sum:

public static double Add( int arg1, int arg2 )
{
return (double)(arg1+arg2);
}

public static double Add( float arg1, float arg2 )
{
return (double)(arg1+arg2);
}

and so on for short, long, decimal, etc..., not to mention any other
types or classes that have an overloaded "+" operator that returns a
double.

OK, so my method is a bit more complicated, but it seems to me that
there ought to be a way for a method to accept arguments from a list
of types. I don't want to use the Object "type", because then I have
to add a bunch of code to check and see if it contains a "legal" type.
I also don't want to re-cast the input data to another type and call a
"default" method, which would increase computational cost and memory
for "small" input types. Is there another way?

Hi,
Since i have not seen You code i cannot be sure, but i think a generic
method is what You are looking for.
( http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx )

Hope You find this useful.
-Zsolt
 
J

jehugaleahsa

I have a method that should accept an input argument of one of several
types (or classes). One way of doing this is to overload the method.
That's fine, but what I've realized is that every version of the
method is identical (except for the type or class of input argument).
This is because the different input types or classes are already
handled by overloaded operators (when necessary). My question then is:
do I really need to copy and paste the exact same code for each type
or class of input argument?

A simple example might be a method called "Add", that takes two
numbers as input and returns a double value representing their sum:

public static double Add( int arg1, int arg2 )
{
    return (double)(arg1+arg2);

}

public static double Add( float arg1, float arg2 )
{
    return (double)(arg1+arg2);

}

and so on for short, long, decimal, etc..., not to mention any other
types or classes that have an overloaded "+" operator that returns a
double.

OK, so my method is a bit more complicated, but it seems to me that
there ought to be a way for a method to accept arguments from a list
of types. I don't want to use the Object "type", because then I have
to add a bunch of code to check and see if it contains a "legal" type.
I also don't want to re-cast the input data to another type and call a
"default" method, which would increase computational cost and memory
for "small" input types. Is there another way?

I know this probably won't help, but what you were describing made one
of my alerts go off. When you see yourself moving the same code into a
ton of similar natured classes, think about moving the methods around.

public abstract class Adder<T>
{
public abstract T Add(T arg1, T arg2);
}

For each type, you create a new derived class implementing the
specific Add. You could pass the args to the ctor, and create a
property for the result; this way you could create an

public interface IAdder
{
void Add();
}

public abstract class Adder<T> : IAdder
{
protected Adder(T arg1, T arg2) { ... }
T Result { ... }
public abstract void Add();
}

Okay, maybe that's a little too weird and it violates variable life
times. It is a great example of the Command design pattern, though!

I've got more solutions in my toolset, if I know exactly what I'm
aiming at.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top