How to convert javascript funtion with spread arg in differ types to c# style?

39 Views Asked by At

I am struggling with the strikt typing of c# by the function arguments.

For my RPN calculator, the needed arguments will be submitted by a function call with a comma separated argument list of differ types:

this.FxMath. RPN(this.Mt, this.L, 2, '*', this.F, '*')   // This.Mt are Variables as Objects)

The function itself will get the comma separated argument as ...seq:

RPN(ergebnis: VariableUnitBuilder, ...seq: any) {

as:

RPN(ergebnis: VariableUnitBuilder, ...seq: any) {

    let stack = [], i = 0;

    typeof seq[i] === 'object' ? seq[i] = seq[i].nominalValue : null;
    stack.push(seq[i]);
    i++;

    while (i <= seq.length) {

      typeof seq[i] === 'object' ? seq[i] = seq[i].nominalValue : null; // Zahlenwert extraieren oder nix.
      let item = seq[i];  // Number or operator

      if (isNaN(item)) {
        let a: number;
        let b: number;

        switch (item) {
          case '+':
            a = parseFloat(stack.pop());
            b = parseFloat(stack.pop());
            stack.push(a + b);

            break; 
...... }

Now I have to need some arguments with differ types and unknown length:

(this.Mt, this.L, 2.5, '*', this.F, '*') // Class, Class, Double , String, Class, String

1

There are 1 best solutions below

0
Guru Stron On BEST ANSWER

You can use the params modifier wich allows a method to accept a variable number of parameters and combine it with array of System.Object:

public void RPN(VariableUnitBuilder p, params object[] seq)
{
   // ...
}

And calls looking like:

someInstance.RPN(someVariableUnitBuilder, 1, "2");
someInstance.RPN(someVariableUnitBuilder, 1, "2", 3, new object());

Note that for value types this will result in boxing which can be undesirable.