0

I have several mandatory parameters and some which aren't. So the basic builder I made is with all the parameters:

public MyObject(int field1, int field2)
    {
     this.field1 = field1;
     this.field2 = field2;
    }

But field2 is not mandatory. So I added another builder:

public MyObject(int field1)
    {
     new MyObject(field1, 0);
    }

So the problem here is the new part. However, without the new I get an error. I tried adding return, but still an error.

Basically, I don't want to write another builder, I want to use the same one as before. Simply, to call it with default values for the non-mandatory parameters. But I must add new, and then I am creating a new instance of the class, which is not what I wanted!

The reasoning behind calling the existing builder is that there are many function inside the builder, not just this.blahblah= blahblah, so I don't want to write the same code twice. How can I do this?

TheEmeritus
  • 429
  • 1
  • 6
  • 18

0 Answers0