method-overloading

How to use Reflection to Invoke an Overloaded Method in .NET

Is there a way to Invoke an overloaded method using reflection in .NET (2.0). I have an application that dynamically instantiates classes that have been derived from a common base class. For compatibility purposes, this base class contains 2 methods of the same name, one with parameters, and one without. I need to call the parameterle...

Representing overloaded methods in UML

I am attempting to create a UML diagram representative of some Java code. In a class I have a method that is overloaded. As far as I know, parameters for methods aren't shown in UML diagrams. How do I represent method overloading in UML? Thanks. ...

Work around Java's static method dispatching without Double Dispatch/Visitor patterns

I am using a class Foo that provides these methods: String overloadedMethod(Object) String overloadedMethod(Goo) Since Java statically dispatches on the non-receiver argument, I cannot just pass my value (which is an Object, but might have dynamic type Goo) and rely on the JVM to dynamically choose the "correct" method. This is my cu...

Function overloading by return type?

Why don't more mainstream statically typed languages support function/method overloading by return type? I can't think of any that do. It seems no less useful or reasonable than supporting overload by parameter type. How come it's so much less popular? ...

How does Java method dispatch work with Generics and abstract classes?

I ran into a situation today where Java was not invoking the method I expected -- Here is the minimal test case: (I'm sorry this seems contrived -- the 'real world' scenario is substantially more complex, and makes much more sense from a "why the hell would you do that?" standpoint.) I'm specifically interested in why this happens, I do...

Function overloading in Javascript - Best practices

What is the best way (or ways) to fake function overloading in Javascript? I know it is not possible to overload functions in Javascript as in other languages. If i needed a function with two uses foo(x) and foo(x,y,z) which is the best / preffered way: Using different names in the first place Using optional arguments like y = y || ...

Something like overloading in PHP?

I'd like to accomplish something like this: Call a method, say "turn", and then have "turn" applied differently to different data types, e.g., calling "turn" with a "screwdriver" object/param uses the "turnScrewdriver" method, calling "turn" with a "steeringWheel" object/param uses the "turnSteeringWheel" method, etc. -- different things...

Overloading methods in C# .NET

A variable of the type Int32 won't be threated as Int32 if we cast it to "Object" before passing to the overloaded methods below: public static void MethodName(int a) { Console.WriteLine("int"); } public static void MethodName(object a) { Console.ReadLine(); } To handle it as an Int32 even if it is cast to "Object" can be achieved ...

When to use method overloads VS "request" object

What is the best "rule of thumb" to determine when to use method overloads and when to use a separate "request" class? for example: MakePancakes(int size) MakePancakes(int size, bool addBlueBerries) MakePancakes(int size, bool addBlueBerries, ...) As opposed to: MakePancakes(PancakeOptions options) Is it best to stick to one way o...

Java dynamic binding

I am practicing for an exam, and found a sample problem that gets me totally lost. For the following code, find what the output is: class Moe { public void print(Moe p) { System.out.println("Moe 1\n"); } } class Larry extends Moe { public void print(Moe p) { System.out.println("Larry 1\n"); } public v...

Java Overloaded Methods

I am trying to use one file to create a menu in the command window. The user selects from those menu options. They are prompted to enter a number. The number is passed to two overloaded methods which determine if the number is an integer or a float. After the calculation is done the result is printed to the screen and the menu reappears....

C#: Passing null to overloaded method - which method is called?

Say I have two overloaded versions of a C# method: void Method( TypeA a ) { } void Method( TypeB b ) { } I call the method with: Method( null ); Which overload of the method is called? What can I do to ensure that a particular overload is called? ...

Can you declare a variable length Generics type declaration?

I have an overloaded utility method called CheckDuration with following function signatures. private static Action<int> CheckDuration(Action action) private static Action<int> CheckDuration<T>(Action<T> action, T arg) Basically CheckDuration prints on console how long it took to run a method. Now, I would like to check the du...

Overload Resolution in C# 4.0 using dynamic types

I don't have access to the C# 4.0 preview yet. But I am curious, what does the C# 4.0 runtime do when invoking an overloaded method in the following case. Does it resolve to the generic overload ... or the specialized overload. public class Foo<T> { protected string BarImpl( T value ) { return "Bar(T) says: " + value.ToString(); } ...

method with same name and different parameters in Ruby

I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity (vel) ...... end vel is a Command2D class that has 3 attributes, is Comparable and defines + , basically is a convenient class for me to manage those 3 attributes, so I want to use it internally in my library (dont want to make the...

How to reference proper non-generic overload in c# ?

What do i write instead of ??????? to select proper overload? using System; using System.Collections.Generic; namespace ConsoleApplication2 { class A {} class B : A {} class C : A {} class Program { static void Main(string[] args) { var l1 = new List<C>(); var l2 = new List<C>(); Compa...

Methods overloading

When I call the EntryPoint with a parameter of type TemplateA, I always receive an exception, since the first overload is always called. What I expected to happen is that the most specific method (second overload) will be called due to dynamic binding. Any ideas why? private object _obj; public void EntryPoint(object p) { ...

C#, XmlDoc: How to reference method overloads

If I have these two methods public Foo Get(string bar) { ... } public Foo Get(int bar) { ... } And write this piece of xml documentation on a different method /// <summary> /// Has a close relation to the <see cref="Get"/> methods. /// </summary> I get a blue squiggly under Get, saying that it is an Ambiguous reference 'Get'. which...

How do I unit test overloaded functions?

So I have a class that looks something like the following: public class MyClass { DatabaseDependency _depend; public MyClass(DatabaseDependency depend) { _depend = depend; } public string DoSomething(DBParameter database) { var result = _depend.GetResults(database, ...); string response...

Why does this function overloading is not working?

Hy, i know it sounds a very stupid question. Here's what i found: public static List<SomeDTO> GetData(Guid userId, int languageId) { // Do something here } public static List<int> GetData(Guid userId ,int iNumberOfItems) { var result = GetData(userID,0); return (from r in result select c.id).Take(iNumberOfItems)...