Javascript error calling overloaded methods of a C# class

416 Views Asked by At

I'm writing a C# class library, and calling it from some Javascript code (technically Jscript.NET). I recently added some overloaded methods, and Javascript has trouble deciding which one to call, because it doesn't always know the types of its variables. I understand why this is happening in most cases, but I've got one example where I don't understand it.

Here are the overloaded method declarations in the C# class.

public virtual DeviceMessage RequestInUnits(
    Command command,
    int value,
    UnitOfMeasure unit)
public virtual DeviceMessage RequestInUnits(
    Command command, 
    Measurement measurement)

My application has a scripting feature that uses Jscript.NET. Here's some Javascript code that tries to call one of those methods on the C# class.

c.RequestInUnits(Command.MoveAbsolute, 0);

That's not a legal call, because the only method with two parameters expects a Measurement object as the second parameter. However, I would expect a type mismatch error. Instead, here's the compilation error I get.

More than one method or property matches this argument list at line 3 column 1

If I replace the 0 with "", then I get a type mismatch error. Why does Javascript think it can convert a number to an object? Why does it think it can coerce the types to more than one of those methods? Only one method takes two parameters.

This isn't a critical problem, but I don't like it when my library causes confusing error messages in calling code. I'd prefer to avoid that if I can.

0

There are 0 best solutions below