0

Decompiler (dotPeek) output:

class Base {
    public Base(int d, int e, int f) {
        // logic
        Console.WriteLine ("Base constructor");
    }
}

class Child : Base {
    public Child(string a, string b, string c) {
        // logic before explicit call, calculating d, e, f

        // ISSUE: explicit constructor call
        base.\u002Ector(d, e, f);

        // logic after explicit call
    }
}

Trying to convert it to normal c# base constructor call, but can't figure out how to unpack tuple to pass it as args to parent constructor:

class Base {
    public Base(int d, int e, int f) {
    // logic
        Console.WriteLine ("Base constructor");
    }
}

class Child : Base {
    public Child(string a, string b, string c) : base(DoWorkBeforeBaseConstructor(a, b, c)) {
        // logic after explicit call
    }

    private static (int, int, int) DoWorkBeforeBaseConstructor(string a, string b, string c) {
        // logic generating response based on params
        
        // logic before explicit call
        return (1, 2, 3);
    }
}
Jedi Knight
  • 367
  • 2
  • 10
  • Don't use a constructor. Create a method with code so you can return results including errors. – jdweng Aug 30 '22 at 12:37
  • @jdweng I still need to pass data from child constructor to parent constructor – Jedi Knight Aug 30 '22 at 12:44
  • Constructors do not return values. One alternative is to create methods that return values. – jdweng Aug 30 '22 at 12:49
  • There are no tuples in your original code anywhere. Nothing much would prevent you from writing `CalculateD(a, b, c)`, `CalculateE(a, b, c)` and `CalculateF(a, b, c)` methods, even though the calculations might end up a bit clunky and redundant. – Jeroen Mostert Aug 30 '22 at 12:57

1 Answers1

1

There isn't a way to do this with a tuple. If you have access to the base class, add another constructor that takes a tuple:

public Base((int e, int f, int g) tuple) : this(tuple.e, tuple.f, tuple.g)
{
}

However, this all looks rather hacky to me and I'm not sure what you are doing follows good SOLID principles. I would consider moving the DoWorkBeforeBaseConstructor method somewhere else and just passing in the new values into the Child class.

DavidG
  • 113,891
  • 12
  • 217
  • 223